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