mechanize.cr/spec/cookie_spec.cr

105 lines
3.8 KiB
Crystal
Raw Normal View History

2021-08-06 06:44:10 +02:00
require "./spec_helper"
WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"})
2021-08-06 08:11:20 +02:00
WebMock.stub(:get, "example.com/cookies1_domain").to_return(headers: {"Set-Cookie" => "id=123; Domain=example.com"})
2021-08-06 06:44:10 +02:00
WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "name=kanezoh"})
WebMock.stub(:get, "example.com/cookies3").to_return(headers: {"Set-Cookie" => "id=456"})
2021-08-07 07:05:10 +02:00
WebMock.stub(:get, "example.com/secure_cookies").to_return(headers: {"Set-Cookie" => "id=123; Secure"})
2021-08-06 09:58:31 +02:00
WebMock.stub(:get, "example.com/paths").to_return(headers: {"Set-Cookie" => "id=123; Path=/paths"})
2021-08-17 18:30:01 +02:00
WebMock.stub(:get, "example.com/paths/hoge").to_return
WebMock.stub(:get, "https://example.com/").to_return
WebMock.stub(:get, "example.com/hoge/paths").to_return
WebMock.stub(:get, "www.example.com").to_return
WebMock.stub(:get, "example.com/meta_cookie").to_return(body: <<-BODY
2021-08-06 06:44:10 +02:00
<html>
<head>
<title>page_title</title>
<meta http-equiv='Set-Cookie' content='id=123;SameSite=None;Secure'>
</head>
<body>
</body>
</html>
BODY
)
describe "Mechanize Cookie test" do
it "can 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 "updates cookie value if key is same" do
agent = Mechanize.new
agent.get("http://example.com/cookies1")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
agent.get("http://example.com/cookies3")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=456"
end
it "can 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
it "can get cookie from meta head" do
agent = Mechanize.new
agent.get("http://example.com/meta_cookie")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
end
it "doesn't send cookies to another domain" do
agent = Mechanize.new
agent.get("http://example.com/cookies1")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
agent.get("http://another_domain.com/")
agent.request_headers.has_key?("Cookie").should eq false
end
2021-08-06 08:11:20 +02:00
it "sends cookie to subdomain if domain attribute is set" do
agent = Mechanize.new
agent.get("http://example.com/cookies1")
agent.get("http://www.example.com/")
agent.request_headers.has_key?("Cookie").should eq false
agent.get("http://example.com/cookies1_domain")
agent.get("http://www.example.com/")
agent.request_headers.has_key?("Cookie").should eq true
end
2021-08-06 09:58:31 +02:00
it "doesn't send not-matched path if path attribute is set" do
agent = Mechanize.new
agent.get("http://example.com/paths")
agent.get("http://example.com/")
agent.request_headers.has_key?("Cookie").should eq false
agent.get("http://example.com/paths")
agent.request_headers.has_key?("Cookie").should eq true
agent.get("http://example.com/paths/hoge")
agent.request_headers.has_key?("Cookie").should eq true
agent.get("http://example.com/hoge/paths")
agent.request_headers.has_key?("Cookie").should eq false
end
2021-08-07 07:05:10 +02:00
it "doesn't send cookie to http protocol if secure attribute is set" do
agent = Mechanize.new
agent.get("http://example.com/secure_cookies")
agent.get("http://example.com/")
agent.request_headers.has_key?("Cookie").should eq false
agent.get("https://example.com/")
agent.request_headers.has_key?("Cookie").should eq true
agent.request_headers["Cookie"].should eq "id=123"
end
2021-08-06 06:44:10 +02:00
end