2021-04-19 06:50:12 +02:00
|
|
|
require "./spec_helper"
|
2021-06-10 10:09:36 +02:00
|
|
|
WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2")
|
2022-01-10 02:44:27 +01:00
|
|
|
WebMock.stub(:get, "http://example.com/path?foo=bar&foo1=bar2")
|
2021-08-17 18:30:01 +02:00
|
|
|
WebMock.stub(:post, "http://example.com/post")
|
|
|
|
.with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
|
|
|
|
.to_return(body: "success")
|
2021-08-09 10:49:06 +02:00
|
|
|
WebMock.stub(:get, "example.com/%E3%81%82%E3%81%82%E3%81%82")
|
2021-10-09 13:26:33 +02:00
|
|
|
WebMock.stub(:get, "https://example.com/")
|
|
|
|
WebMock.stub(:get, "https://example.com/post")
|
2021-11-21 10:21:59 +01:00
|
|
|
WebMock.stub(:put, "http://example.com/put")
|
|
|
|
.with(body: "hello")
|
|
|
|
.to_return(body: "success")
|
2021-11-21 10:48:25 +01:00
|
|
|
WebMock.stub(:delete, "http://example.com/delete")
|
|
|
|
.with(body: "hello")
|
|
|
|
.to_return(body: "success")
|
2021-11-22 02:07:32 +01:00
|
|
|
WebMock.stub(:head, "http://example.com/head")
|
2021-04-19 06:50:12 +02:00
|
|
|
|
2021-06-10 10:30:33 +02:00
|
|
|
describe "Mechanize HTTP test" do
|
2021-06-09 15:36:35 +02:00
|
|
|
it "simple GET" do
|
2021-06-09 14:44:54 +02:00
|
|
|
agent = Mechanize.new
|
|
|
|
uri = "http://example.com/"
|
|
|
|
page = agent.get(uri)
|
|
|
|
page.code.should eq 200
|
|
|
|
page.uri.to_s.should eq uri
|
2021-04-19 06:50:12 +02:00
|
|
|
end
|
2021-06-09 15:36:35 +02:00
|
|
|
|
|
|
|
it "GET with query parameter" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
params = {"foo" => "bar", "foo1" => "bar2"}
|
|
|
|
uri = "http://example.com/"
|
|
|
|
page = agent.get(uri, params: params)
|
|
|
|
page.code.should eq 200
|
|
|
|
page.uri.to_s.should eq "http://example.com/?foo=bar&foo1=bar2"
|
|
|
|
end
|
2021-06-10 10:09:36 +02:00
|
|
|
|
|
|
|
it "GET with query parameter as URL string" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
uri = "http://example.com/?foo=bar&foo1=bar2"
|
|
|
|
page = agent.get(uri)
|
|
|
|
page.code.should eq 200
|
|
|
|
page.uri.to_s.should eq uri
|
|
|
|
end
|
|
|
|
|
2022-01-10 02:44:27 +01:00
|
|
|
it "GET with query parameter with path" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
uri = "http://example.com/path?foo=bar&foo1=bar2"
|
|
|
|
page = agent.get(uri)
|
|
|
|
page.code.should eq 200
|
|
|
|
page.uri.to_s.should eq uri
|
|
|
|
end
|
|
|
|
|
2021-11-21 09:26:41 +01:00
|
|
|
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
|
|
|
|
|
2021-11-21 10:48:25 +01:00
|
|
|
it "PUT" do
|
2021-11-21 09:26:41 +01:00
|
|
|
agent = Mechanize.new
|
|
|
|
page = agent.put("http://example.com/put", body: "hello")
|
2021-11-21 10:48:25 +01:00
|
|
|
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")
|
2021-11-21 10:21:59 +01:00
|
|
|
page.body.should eq "success"
|
|
|
|
page.code.should eq 200
|
2021-11-21 09:26:41 +01:00
|
|
|
end
|
|
|
|
|
2021-11-22 02:07:32 +01:00
|
|
|
it "HEAD" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
page = agent.head("http://example.com/head")
|
|
|
|
page.body.should eq ""
|
|
|
|
page.code.should eq 200
|
|
|
|
end
|
|
|
|
|
2021-08-09 10:49:06 +02:00
|
|
|
it "can escape non-ascii character" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
page = agent.get("http://example.com/あああ")
|
|
|
|
page.uri.to_s.should eq "http://example.com/%E3%81%82%E3%81%82%E3%81%82"
|
|
|
|
end
|
|
|
|
|
2021-06-10 10:09:36 +02:00
|
|
|
it "set custom request headers" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
uri = "http://example.com/"
|
|
|
|
headers = HTTP::Headers{"Foo" => "Bar"}
|
|
|
|
agent.request_headers.empty?.should eq true
|
|
|
|
page = agent.get(uri, headers: headers)
|
|
|
|
agent.request_headers.empty?.should eq false
|
|
|
|
agent.request_headers["Foo"].should eq "Bar"
|
|
|
|
end
|
2021-06-11 06:47:36 +02:00
|
|
|
|
2021-08-04 07:37:56 +02:00
|
|
|
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"
|
|
|
|
agent.user_agent = mac_chrome_agent
|
|
|
|
page = agent.get("http://example.com/")
|
|
|
|
agent.request_headers["User-Agent"].should eq mac_chrome_agent
|
|
|
|
end
|
2021-10-09 13:26:33 +02:00
|
|
|
|
|
|
|
it "can complete uri when uri is relative" do
|
|
|
|
agent = Mechanize.new
|
|
|
|
agent.get("https://example.com/")
|
|
|
|
page = agent.get("/post")
|
|
|
|
page.uri.to_s.should eq "https://example.com/post"
|
|
|
|
end
|
2021-04-19 06:50:12 +02:00
|
|
|
end
|