add put http method

This commit is contained in:
Kanezoh 2021-11-21 17:26:41 +09:00
parent a82f8f32d1
commit f97224d79c
3 changed files with 48 additions and 11 deletions

View File

@ -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, "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").
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 describe "Mechanize HTTP test" do
it "simple GET" do it "simple GET" do
@ -33,6 +36,21 @@ describe "Mechanize HTTP test" do
page.uri.to_s.should eq uri page.uri.to_s.should eq uri
end 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 it "can escape non-ascii character" do
agent = Mechanize.new agent = Mechanize.new
page = agent.get("http://example.com/あああ") page = agent.get("http://example.com/あああ")
@ -49,14 +67,6 @@ describe "Mechanize HTTP test" do
agent.request_headers["Foo"].should eq "Bar" agent.request_headers["Foo"].should eq "Bar"
end 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 it "can set user agent" do
agent = Mechanize.new 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" 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"

View File

@ -91,6 +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.
#
# 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. # get the value of request headers.
# #
# ``` # ```

View File

@ -22,14 +22,18 @@ class Mechanize
# send http request and return page. # send http request and return page.
# This method is called from Mechanize#get, #post and other methods. # This method is called from Mechanize#get, #post and other methods.
# There's no need to call this method directly. # 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?)) referer = (current_page unless history.empty?))
uri = resolve_url(uri, referer) uri = resolve_url(uri, referer)
set_request_headers(uri, headers) set_request_headers(uri, headers)
set_user_agent set_user_agent
set_request_referer(referer) set_request_referer(referer)
uri, params = resolve_parameters(uri, method, params) 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 body = response.not_nil!.body
page = response_parse(response, body, uri) page = response_parse(response, body, uri)
response_log(response) response_log(response)
@ -58,7 +62,7 @@ class Mechanize
end end
# send http request # 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) request_log(uri, method)
case uri.scheme.not_nil!.downcase case uri.scheme.not_nil!.downcase
@ -68,6 +72,8 @@ class Mechanize
::HTTP::Client.get(uri, headers: request_headers) ::HTTP::Client.get(uri, headers: request_headers)
when :post when :post
::HTTP::Client.post(uri, headers: request_headers, form: params.not_nil!.fetch("value", "")) ::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 end
end end