From 95862e5543c91207719fcf89d0a3add13d12b168 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 21 Nov 2021 18:48:25 +0900 Subject: [PATCH] add delete --- spec/http_spec.cr | 13 +++++++++++-- src/mechanize.cr | 22 +++++++++++++++++++++- src/mechanize/http/agent.cr | 2 ++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 4d76d5e..728fb7a 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -9,6 +9,9 @@ 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 @@ -44,10 +47,16 @@ describe "Mechanize HTTP test" do page.code.should eq 200 end - it "simple PUT" do + it "PUT" do agent = Mechanize.new 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.code.should eq 200 end diff --git a/src/mechanize.cr b/src/mechanize.cr index 23220cc..ca5095b 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -93,7 +93,7 @@ class Mechanize # 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 @@ -117,6 +117,26 @@ class Mechanize 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. # # ``` diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 877fd29..33408d3 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -74,6 +74,8 @@ class Mechanize ::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