improve put test

This commit is contained in:
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, "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

View File

@ -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