Merge pull request #20 from Kanezoh/add_head

add head method
master
Kanezoh 2021-11-22 10:11:27 +09:00 committed by GitHub
commit 9a2c0fbb33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -12,6 +12,7 @@ WebMock.stub(:put, "http://example.com/put")
WebMock.stub(:delete, "http://example.com/delete") WebMock.stub(:delete, "http://example.com/delete")
.with(body: "hello") .with(body: "hello")
.to_return(body: "success") .to_return(body: "success")
WebMock.stub(:head, "http://example.com/head")
describe "Mechanize HTTP test" do describe "Mechanize HTTP test" do
it "simple GET" do it "simple GET" do
@ -61,6 +62,13 @@ describe "Mechanize HTTP test" do
page.code.should eq 200 page.code.should eq 200
end end
it "HEAD" do
agent = Mechanize.new
page = agent.head("http://example.com/head")
page.body.should eq ""
page.code.should eq 200
end
it "can escape non-ascii character" do it "can escape non-ascii character" do
agent = Mechanize.new agent = Mechanize.new
page = agent.get("http://example.com/あああ") page = agent.get("http://example.com/あああ")

View File

@ -135,6 +135,23 @@ class Mechanize
page page
end end
# Send HEAD request to specified uri with headers.
#
# Examples (send HEAD request to http://example.com/)
#
# ```
# agent = Mechanize.new
# agent.head("http://example.com")
# ```
def head(uri : String | URI,
headers = ::HTTP::Headers.new) : Mechanize::Page
method = :head
page = @agent.fetch uri, method, headers
add_to_history(page)
# yield page if block_given?
page
end
# get the value of request headers. # get the value of request headers.
# #
# ``` # ```

View File

@ -76,6 +76,8 @@ class Mechanize
::HTTP::Client.put(uri, headers: request_headers, body: body) ::HTTP::Client.put(uri, headers: request_headers, body: body)
when :delete when :delete
::HTTP::Client.delete(uri, headers: request_headers, body: body) ::HTTP::Client.delete(uri, headers: request_headers, body: body)
when :head
::HTTP::Client.head(uri, headers: request_headers)
end end
end end
end end