add delete

master
Kanezoh 2021-11-21 18:48:25 +09:00
parent 669360621e
commit 95862e5543
3 changed files with 34 additions and 3 deletions

View File

@ -9,6 +9,9 @@ WebMock.stub(:get, "https://example.com/post")
WebMock.stub(:put, "http://example.com/put") WebMock.stub(:put, "http://example.com/put")
.with(body: "hello") .with(body: "hello")
.to_return(body: "success") .to_return(body: "success")
WebMock.stub(:delete, "http://example.com/delete")
.with(body: "hello")
.to_return(body: "success")
describe "Mechanize HTTP test" do describe "Mechanize HTTP test" do
it "simple GET" do it "simple GET" do
@ -44,10 +47,16 @@ describe "Mechanize HTTP test" do
page.code.should eq 200 page.code.should eq 200
end end
it "simple PUT" do it "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")
agent.get("http://example.com/") 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.body.should eq "success"
page.code.should eq 200 page.code.should eq 200
end end

View File

@ -93,7 +93,7 @@ class Mechanize
# Send PUT request to specified uri with headers, and body. # Send PUT request to specified uri with headers, and body.
# #
# Examples (send put request whose post body is "hello") # Examples (send put request whose body is "hello")
# #
# ``` # ```
# agent = Mechanize.new # agent = Mechanize.new
@ -117,6 +117,26 @@ class Mechanize
page page
end 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. # get the value of request headers.
# #
# ``` # ```

View File

@ -74,6 +74,8 @@ class Mechanize
::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 when :put
::HTTP::Client.put(uri, headers: request_headers, body: body) ::HTTP::Client.put(uri, headers: request_headers, body: body)
when :delete
::HTTP::Client.delete(uri, headers: request_headers, body: body)
end end
end end
end end