improve put test

master
Kanezoh 2021-11-21 18:21:59 +09:00
parent f97224d79c
commit 669360621e
2 changed files with 18 additions and 12 deletions

View File

@ -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, "example.com/%E3%81%82%E3%81%82%E3%81%82")
WebMock.stub(:get, "https://example.com/") WebMock.stub(:get, "https://example.com/")
WebMock.stub(:get, "https://example.com/post") WebMock.stub(:get, "https://example.com/post")
WebMock.stub(:put, "http://example.com/put"). 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)"}). .with(body: "hello")
to_return(body: "") .to_return(body: "success")
describe "Mechanize HTTP test" do describe "Mechanize HTTP test" do
it "simple GET" do it "simple GET" do
@ -47,8 +47,9 @@ describe "Mechanize HTTP test" do
it "simple PUT" do it "simple PUT" do
agent = Mechanize.new agent = Mechanize.new
page = agent.put("http://example.com/put", body: "hello") page = agent.put("http://example.com/put", body: "hello")
#page.body.should eq "success" agent.get("http://example.com/")
#page.code.should eq 200 page.body.should eq "success"
page.code.should eq 200
end end
it "can escape non-ascii character" do it "can escape non-ascii character" do

View File

@ -91,22 +91,27 @@ class Mechanize
post_form(uri, form, headers) post_form(uri, form, headers)
end 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 = Mechanize.new
# agent.put("http://example.com", # agent.put("http://example.com",
# body: "hello!", # body: "hello")
# headers: HTTP::Headers{"Foo" => "Bar"})
# ``` # ```
def put(uri : String | URI, def put(uri : String | URI,
body : String?, body : String?,
headers = ::HTTP::Headers.new) : Mechanize::Page headers = ::HTTP::Headers.new) : Mechanize::Page
method = :put method = :put
headers.merge!({
"Content-Type" => "application/octet-stream",
"Content-Length" => body.size.to_s,
})
page = @agent.fetch(uri, method, headers: headers, body: body) page = @agent.fetch(uri, method, headers: headers, body: body)
request_headers.delete("Content-Type")
request_headers.delete("Content-Length")
add_to_history(page) add_to_history(page)
# yield page if block_given? # yield page if block_given?
page page
@ -284,8 +289,8 @@ class Mechanize
# fetch the page # fetch the page
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data}, referer: cur_page) page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data}, referer: cur_page)
headers.delete("Content-Type") request_headers.delete("Content-Type")
headers.delete("Content-Length") request_headers.delete("Content-Length")
add_to_history(page) add_to_history(page)
page page
end end