add cookie test

master
Kanezoh 2021-06-13 07:58:50 +09:00
parent d96d597547
commit f3bd9845e3
2 changed files with 33 additions and 9 deletions

View File

@ -1,4 +1,7 @@
require "./spec_helper" require "./spec_helper"
WebMock.stub(:get, "example.com/")
WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"})
WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "name=kanezoh"})
WebMock.stub(:get, "html_example.com").to_return(body: WebMock.stub(:get, "html_example.com").to_return(body:
<<-BODY <<-BODY
<html> <html>
@ -21,12 +24,33 @@ WebMock.stub(:post, "http://html_example.com/post_path").
to_return(body: "success") to_return(body: "success")
describe "Mechanize Agent test" do describe "Mechanize Agent test" do
agent = Mechanize.new it "fill and submit form" do
page = agent.get("http://html_example.com/") agent = Mechanize.new
form = page.forms[0] page = agent.get("http://html_example.com/")
form.field_with("name").value = "foo" form = page.forms[0]
form.field_with("email").value = "bar" form.field_with("name").value = "foo"
page = agent.submit(form) form.field_with("email").value = "bar"
page.not_nil!.code.should eq 200 page = agent.submit(form)
page.not_nil!.body.should eq "success" page.not_nil!.code.should eq 200
page.not_nil!.body.should eq "success"
end
it "receive and send cookie" do
agent = Mechanize.new
# receive cookies
agent.get("http://example.com/cookies1")
# send cookies
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
end
it "receive and send multiple cookies" do
agent = Mechanize.new
# receive cookies1
agent.get("http://example.com/cookies1")
# receive cookies2
agent.get("http://example.com/cookies2")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123; name=kanezoh"
end
end end

View File

@ -74,7 +74,7 @@ module MechanizeCr
# end # end
#end #end
header_cookies = response.try &.cookies header_cookies = response.try &.cookies
if (header_cookies.nil?) if (header_cookies.nil? || header_cookies.try &.empty?)
request_headers request_headers
else else
if cookies.fetch(uri.host.to_s, ::HTTP::Cookies.new).empty? if cookies.fetch(uri.host.to_s, ::HTTP::Cookies.new).empty?