From 6fc0dc1c57c7b40fd7592dfa9f8b00a5625cff5b Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Mon, 22 Nov 2021 10:07:32 +0900 Subject: [PATCH] add head method --- spec/http_spec.cr | 8 ++++++++ src/mechanize.cr | 17 +++++++++++++++++ src/mechanize/http/agent.cr | 2 ++ 3 files changed, 27 insertions(+) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 728fb7a..37e2604 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -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/あああ") diff --git a/src/mechanize.cr b/src/mechanize.cr index 76b7417..cc9e047 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -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. # # ``` diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 33408d3..1852b39 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -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