@@ -936,10 +936,37 @@ def test_wrong_domain(self):
936936
937937 # XXX far from complete
938938 c = CookieJar ()
939- interact_2965 (c , "http://www.nasty .com/" ,
940- 'foo=bar; domain=friendly .org; Version="1"' )
939+ interact_2965 (c , "http://www.example .com/" ,
940+ 'foo=bar; domain=python .org; Version="1"' )
941941 self .assertEqual (len (c ), 0 )
942942
943+ def test_negative_version (self ):
944+ # A negative version is neither Netscape (0) nor RFC 2965 (> 0), so
945+ # such cookies must be neither set nor returned.
946+ for rfc2965 in False , True :
947+ for interact in interact_netscape , interact_2965 :
948+ with self .subTest (rfc2965 = rfc2965 , interact = interact .__name__ ):
949+ c = CookieJar (DefaultCookiePolicy (rfc2965 = rfc2965 ))
950+ interact (c , "http://www.example.com/" ,
951+ 'foo=bar; domain=python.org; Version=-1' )
952+ self .assertEqual (len (c ), 0 )
953+ interact (c , "http://www.example.com/" , 'foo=bar; Version=-1' )
954+ self .assertEqual (len (c ), 0 )
955+
956+ # Such a header is discarded before the policy is consulted.
957+ c = CookieJar ()
958+ req = urllib .request .Request ("http://www.example.com/" )
959+ res = FakeResponse (["Set-Cookie: foo=bar; Version=-1" ],
960+ "http://www.example.com/" )
961+ self .assertEqual (c .make_cookies (res , req ), [])
962+
963+ # Not set from a response, for example loaded from an LWP cookie file.
964+ c = CookieJar ()
965+ c .set_cookie (Cookie (- 1 , "foo" , "bar" , None , False ,
966+ ".python.org" , True , True , "/" , True ,
967+ False , None , True , None , None , {}))
968+ self .assertEqual (interact_netscape (c , "http://www.python.org/" ), "" )
969+
943970 def test_strict_domain (self ):
944971 # Cookies whose domain is a country-code tld like .co.uk should
945972 # not be set if CookiePolicy.strict_domain is true.
@@ -1439,6 +1466,7 @@ def cookiejar_from_cookie_headers(headers):
14391466 ["Set-Cookie: b=foo; max-age=oops" ],
14401467 # bad version
14411468 ["Set-Cookie: b=foo; version=spam" ],
1469+ ["Set-Cookie: b=foo; version=-1" ],
14421470 ["Set-Cookie:; Expires=%s" % future ],
14431471 ]:
14441472 c = cookiejar_from_cookie_headers (headers )
0 commit comments