add subdomain cookie test

master
Kanezoh 2021-08-06 15:11:20 +09:00
parent ab093f299d
commit b67d20b157
2 changed files with 15 additions and 2 deletions

View File

@ -1,8 +1,10 @@
require "./spec_helper"
WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"})
WebMock.stub(:get, "example.com/cookies1_domain").to_return(headers: {"Set-Cookie" => "id=123; Domain=example.com"})
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, "www.example.com").to_return()
WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
<<-BODY
<html>
@ -61,4 +63,15 @@ describe "Mechanize Cookie test" do
agent.get("http://another_domain.com/")
agent.request_headers.has_key?("Cookie").should eq false
end
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
end

View File

@ -130,8 +130,8 @@ module MechanizeCr
private def valid_cookies(uri)
host = uri.host
valid_cookies = ::HTTP::Cookies.new
request_cookies.select do |cookie|
valid_cookies << cookie if cookie.origin == host || cookie.domain.try &.=~(/.*#{host}/)
request_cookies.each do |cookie|
valid_cookies << cookie if cookie.origin == host || (cookie.domain && host.try &.=~(/.*#{cookie.domain.try &.gsub(".", "\.")}$/))
end
valid_cookies
end