add head method

This commit is contained in:
Kanezoh 2021-11-22 10:07:32 +09:00
parent 85297266a8
commit 6fc0dc1c57
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")
.with(body: "hello")
.to_return(body: "success")
WebMock.stub(:head, "http://example.com/head")
describe "Mechanize HTTP test" do
it "simple GET" do
@ -61,6 +62,13 @@ describe "Mechanize HTTP test" do
page.code.should eq 200
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
agent = Mechanize.new
page = agent.get("http://example.com/あああ")

View File

@ -135,6 +135,23 @@ class Mechanize
page
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.
#
# ```

View File

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