@@ -943,10 +943,37 @@ def test_wrong_domain(self):
943943
944944 # XXX far from complete
945945 c = CookieJar ()
946- interact_2965 (c , "http://www.nasty .com/" ,
947- 'foo=bar; domain=friendly .org; Version="1"' )
946+ interact_2965 (c , "http://www.example .com/" ,
947+ 'foo=bar; domain=python .org; Version="1"' )
948948 self .assertEqual (len (c ), 0 )
949949
950+ def test_negative_version (self ):
951+ # A negative version is neither Netscape (0) nor RFC 2965 (> 0), so
952+ # such cookies must be neither set nor returned.
953+ for rfc2965 in False , True :
954+ for interact in interact_netscape , interact_2965 :
955+ with self .subTest (rfc2965 = rfc2965 , interact = interact .__name__ ):
956+ c = CookieJar (DefaultCookiePolicy (rfc2965 = rfc2965 ))
957+ interact (c , "http://www.example.com/" ,
958+ 'foo=bar; domain=python.org; Version=-1' )
959+ self .assertEqual (len (c ), 0 )
960+ interact (c , "http://www.example.com/" , 'foo=bar; Version=-1' )
961+ self .assertEqual (len (c ), 0 )
962+
963+ # Such a header is discarded before the policy is consulted.
964+ c = CookieJar ()
965+ req = urllib .request .Request ("http://www.example.com/" )
966+ res = FakeResponse (["Set-Cookie: foo=bar; Version=-1" ],
967+ "http://www.example.com/" )
968+ self .assertEqual (c .make_cookies (res , req ), [])
969+
970+ # Not set from a response, for example loaded from an LWP cookie file.
971+ c = CookieJar ()
972+ c .set_cookie (Cookie (- 1 , "foo" , "bar" , None , False ,
973+ ".python.org" , True , True , "/" , True ,
974+ False , None , True , None , None , {}))
975+ self .assertEqual (interact_netscape (c , "http://www.python.org/" ), "" )
976+
950977 def test_strict_domain (self ):
951978 # Cookies whose domain is a country-code tld like .co.uk should
952979 # not be set if CookiePolicy.strict_domain is true.
@@ -1446,6 +1473,7 @@ def cookiejar_from_cookie_headers(headers):
14461473 ["Set-Cookie: b=foo; max-age=oops" ],
14471474 # bad version
14481475 ["Set-Cookie: b=foo; version=spam" ],
1476+ ["Set-Cookie: b=foo; version=-1" ],
14491477 ["Set-Cookie:; Expires=%s" % future ],
14501478 ]:
14511479 c = cookiejar_from_cookie_headers (headers )
0 commit comments