From f97224d79c9f4b73567ac98a65a5724aae38cb93 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 17:26:41 +0900 Subject: [PATCH] add put http method --- spec/http_spec.cr | 26 ++++++++++++++++++-------- src/mechanize.cr | 21 +++++++++++++++++++++ src/mechanize/http/agent.cr | 12 +++++++++--- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index f1157e3..286f7e0 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -6,6 +6,9 @@ WebMock.stub(:post, "http://example.com/post") WebMock.stub(:get, "example.com/%E3%81%82%E3%81%82%E3%81%82") WebMock.stub(:get, "https://example.com/") WebMock.stub(:get, "https://example.com/post") +WebMock.stub(:put, "http://example.com/put"). + with(body: "hello", headers: {"User-Agent" => "Mechanize/0.2.0 Crystal/1.1.1 (https://github.com/Kanezoh/mechanize.cr)"}). + to_return(body: "") describe "Mechanize HTTP test" do it "simple GET" do @@ -33,6 +36,21 @@ describe "Mechanize HTTP test" do page.uri.to_s.should eq uri end + it "simple POST" do + agent = Mechanize.new + query = {"email" => "foobar"} + page = agent.post("http://example.com/post", query: query) + page.body.should eq "success" + page.code.should eq 200 + end + + it "simple PUT" do + agent = Mechanize.new + page = agent.put("http://example.com/put", body: "hello") + #page.body.should eq "success" + #page.code.should eq 200 + end + it "can escape non-ascii character" do agent = Mechanize.new page = agent.get("http://example.com/あああ") @@ -49,14 +67,6 @@ describe "Mechanize HTTP test" do agent.request_headers["Foo"].should eq "Bar" end - it "simple POST" do - agent = Mechanize.new - query = {"email" => "foobar"} - page = agent.post("http://example.com/post", query: query) - page.body.should eq "success" - page.code.should eq 200 - end - it "can set user agent" do agent = Mechanize.new mac_chrome_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" diff --git a/src/mechanize.cr b/src/mechanize.cr index 18c54b1..98523f2 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -91,6 +91,27 @@ class Mechanize post_form(uri, form, headers) end + # Send PUT request to specified uri with headers, and query. + # + # Examples (send post request whose post body is "foo=bar") + # + # ``` + # agent = Mechanize.new + # agent.put("http://example.com", + # body: "hello!", + # headers: HTTP::Headers{"Foo" => "Bar"}) + # ``` + def put(uri : String | URI, + body : String?, + headers = ::HTTP::Headers.new) : Mechanize::Page + + method = :put + page = @agent.fetch(uri, method, headers: headers, body: body) + 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 c8905a3..877fd29 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -22,14 +22,18 @@ class Mechanize # send http request and return page. # This method is called from Mechanize#get, #post and other methods. # There's no need to call this method directly. - def fetch(uri, method = :get, headers = ::HTTP::Headers.new, params = Hash(String, String).new, + def fetch(uri, + method = :get, + headers = ::HTTP::Headers.new, + params = Hash(String, String).new, + body : String? = nil, referer = (current_page unless history.empty?)) uri = resolve_url(uri, referer) set_request_headers(uri, headers) set_user_agent set_request_referer(referer) uri, params = resolve_parameters(uri, method, params) - response = http_request(uri, method, params) + response = http_request(uri, method, params, body) body = response.not_nil!.body page = response_parse(response, body, uri) response_log(response) @@ -58,7 +62,7 @@ class Mechanize end # send http request - private def http_request(uri, method, params) : ::HTTP::Client::Response? + private def http_request(uri, method, params, body) : ::HTTP::Client::Response? request_log(uri, method) case uri.scheme.not_nil!.downcase @@ -68,6 +72,8 @@ class Mechanize ::HTTP::Client.get(uri, headers: request_headers) when :post ::HTTP::Client.post(uri, headers: request_headers, form: params.not_nil!.fetch("value", "")) + when :put + ::HTTP::Client.put(uri, headers: request_headers, body: body) end end end