Merge pull request #19 from Kanezoh/add_put_delete

Add PUT DELETE method's
master
Kanezoh 2021-11-21 20:28:55 +09:00 committed by GitHub
commit 85297266a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 15 deletions

View File

@ -6,6 +6,12 @@ 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")
.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
@ -33,6 +39,28 @@ 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 "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 "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
it "can escape non-ascii character" do
agent = Mechanize.new
page = agent.get("http://example.com/あああ")
@ -49,14 +77,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"

View File

@ -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:
#
# ```
@ -91,6 +89,52 @@ class Mechanize
post_form(uri, form, headers)
end
# Send PUT request to specified uri with headers, and body.
#
# Examples (send put request whose body is "hello")
#
# ```
# agent = Mechanize.new
# agent.put("http://example.com",
# 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
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.
#
# ```
@ -263,8 +307,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

View File

@ -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,10 @@ 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)
when :delete
::HTTP::Client.delete(uri, headers: request_headers, body: body)
end
end
end