From f97224d79c9f4b73567ac98a65a5724aae38cb93 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 17:26:41 +0900 Subject: [PATCH 1/4] 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 From 669360621e2d43b1a68ba9d809ead15a1dff3f15 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 18:21:59 +0900 Subject: [PATCH 2/4] improve put test --- spec/http_spec.cr | 11 ++++++----- src/mechanize.cr | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 286f7e0..4d76d5e 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -6,9 +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: "") +WebMock.stub(:put, "http://example.com/put") + .with(body: "hello") + .to_return(body: "success") describe "Mechanize HTTP test" do it "simple GET" do @@ -47,8 +47,9 @@ describe "Mechanize HTTP test" do 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 + agent.get("http://example.com/") + page.body.should eq "success" + page.code.should eq 200 end it "can escape non-ascii character" do diff --git a/src/mechanize.cr b/src/mechanize.cr index 98523f2..23220cc 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -91,22 +91,27 @@ class Mechanize post_form(uri, form, headers) end - # Send PUT request to specified uri with headers, and query. + # Send PUT request to specified uri with headers, and body. # - # Examples (send post request whose post body is "foo=bar") + # Examples (send put request whose post body is "hello") # # ``` # agent = Mechanize.new # agent.put("http://example.com", - # body: "hello!", - # headers: HTTP::Headers{"Foo" => "Bar"}) + # body: "hello") # ``` def put(uri : String | URI, body : String?, headers = ::HTTP::Headers.new) : Mechanize::Page - method = :put + headers.merge!({ + "Content-Type" => "application/octet-stream", + "Content-Length" => body.size.to_s, + }) + page = @agent.fetch(uri, method, headers: headers, body: body) + request_headers.delete("Content-Type") + request_headers.delete("Content-Length") add_to_history(page) # yield page if block_given? page @@ -284,8 +289,8 @@ class Mechanize # fetch the page page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data}, referer: cur_page) - headers.delete("Content-Type") - headers.delete("Content-Length") + request_headers.delete("Content-Type") + request_headers.delete("Content-Length") add_to_history(page) page end From 95862e5543c91207719fcf89d0a3add13d12b168 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 18:48:25 +0900 Subject: [PATCH 3/4] add delete --- spec/http_spec.cr | 13 +++++++++++-- src/mechanize.cr | 22 +++++++++++++++++++++- src/mechanize/http/agent.cr | 2 ++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 4d76d5e..728fb7a 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -9,6 +9,9 @@ WebMock.stub(:get, "https://example.com/post") WebMock.stub(:put, "http://example.com/put") .with(body: "hello") .to_return(body: "success") +WebMock.stub(:delete, "http://example.com/delete") + .with(body: "hello") + .to_return(body: "success") describe "Mechanize HTTP test" do it "simple GET" do @@ -44,10 +47,16 @@ describe "Mechanize HTTP test" do page.code.should eq 200 end - it "simple PUT" do + it "PUT" do agent = Mechanize.new page = agent.put("http://example.com/put", body: "hello") - agent.get("http://example.com/") + page.body.should eq "success" + page.code.should eq 200 + end + + it "DELETE" do + agent = Mechanize.new + page = agent.delete("http://example.com/delete", body: "hello") page.body.should eq "success" page.code.should eq 200 end diff --git a/src/mechanize.cr b/src/mechanize.cr index 23220cc..ca5095b 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -93,7 +93,7 @@ class Mechanize # Send PUT request to specified uri with headers, and body. # - # Examples (send put request whose post body is "hello") + # Examples (send put request whose body is "hello") # # ``` # agent = Mechanize.new @@ -117,6 +117,26 @@ class Mechanize page end + # Send DELETE request to specified uri with headers, and body. + # + # Examples (send delete request whose body is "hello") + # + # ``` + # agent = Mechanize.new + # agent.delete("http://example.com", + # body: "hello") + # ``` + def delete(uri : String | URI, + body : String?, + headers = ::HTTP::Headers.new) : Mechanize::Page + method = :delete + + 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 877fd29..33408d3 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -74,6 +74,8 @@ class Mechanize ::HTTP::Client.post(uri, headers: request_headers, form: params.not_nil!.fetch("value", "")) when :put ::HTTP::Client.put(uri, headers: request_headers, body: body) + when :delete + ::HTTP::Client.delete(uri, headers: request_headers, body: body) end end end From ef7f04195e487f832fbff9bcaf7bc7e647fcdd25 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 19:54:39 +0900 Subject: [PATCH 4/4] delete comment --- src/mechanize.cr | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mechanize.cr b/src/mechanize.cr index ca5095b..76b7417 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -11,8 +11,6 @@ require "./mechanize/errors/*" # This class is main class of Mechanize.cr, # using this class' instance to start web interaction. # -# now only supports GET, POST. other HTTP methods will be implemented soon... -# # Examples: # # ```