divide cookie test

master
Kanezoh 2021-08-06 13:44:10 +09:00
parent 5956081ad1
commit ab093f299d
2 changed files with 64 additions and 48 deletions

View File

@ -1,18 +1,4 @@
require "./spec_helper" require "./spec_helper"
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, "example.com/meta_cookie").to_return(body:
<<-BODY
<html>
<head>
<title>page_title</title>
<meta http-equiv='Set-Cookie' content='id=123;SameSite=None;Secure'>
</head>
<body>
</body>
</html>
BODY
)
describe "Mechanize Agent test" do describe "Mechanize Agent test" do
it "can fill and submit form" do it "can fill and submit form" do
@ -38,40 +24,6 @@ describe "Mechanize Agent test" do
page.not_nil!.body.should eq "success with button" page.not_nil!.body.should eq "success with button"
end end
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 "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
it "can save history" do it "can save history" do
agent = Mechanize.new agent = Mechanize.new
agent.get("http://example.com/") agent.get("http://example.com/")

64
spec/cookie_spec.cr Normal file
View File

@ -0,0 +1,64 @@
require "./spec_helper"
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, "example.com/cookies3").to_return(headers: {"Set-Cookie" => "id=456"})
WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
<<-BODY
<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
end