diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index 42084b8..634e80e 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -1,18 +1,4 @@ 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 - - - page_title - - - - - -BODY -) describe "Mechanize Agent test" 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" 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 agent = Mechanize.new agent.get("http://example.com/") diff --git a/spec/cookie_spec.cr b/spec/cookie_spec.cr new file mode 100644 index 0000000..194c7d8 --- /dev/null +++ b/spec/cookie_spec.cr @@ -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 + + + page_title + + + + + +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