@@ -907,10 +907,37 @@ def test_wrong_domain(self):
907907
908908 # XXX far from complete
909909 c = CookieJar ()
910- interact_2965 (c , "http://www.nasty .com/" ,
911- 'foo=bar; domain=friendly .org; Version="1"' )
910+ interact_2965 (c , "http://www.example .com/" ,
911+ 'foo=bar; domain=python .org; Version="1"' )
912912 self .assertEqual (len (c ), 0 )
913913
914+ def test_negative_version (self ):
915+ # A negative version is neither Netscape (0) nor RFC 2965 (> 0), so
916+ # such cookies must be neither set nor returned.
917+ for rfc2965 in False , True :
918+ for interact in interact_netscape , interact_2965 :
919+ with self .subTest (rfc2965 = rfc2965 , interact = interact .__name__ ):
920+ c = CookieJar (DefaultCookiePolicy (rfc2965 = rfc2965 ))
921+ interact (c , "http://www.example.com/" ,
922+ 'foo=bar; domain=python.org; Version=-1' )
923+ self .assertEqual (len (c ), 0 )
924+ interact (c , "http://www.example.com/" , 'foo=bar; Version=-1' )
925+ self .assertEqual (len (c ), 0 )
926+
927+ # Such a header is discarded before the policy is consulted.
928+ c = CookieJar ()
929+ req = urllib .request .Request ("http://www.example.com/" )
930+ res = FakeResponse (["Set-Cookie: foo=bar; Version=-1" ],
931+ "http://www.example.com/" )
932+ self .assertEqual (c .make_cookies (res , req ), [])
933+
934+ # Not set from a response, for example loaded from an LWP cookie file.
935+ c = CookieJar ()
936+ c .set_cookie (Cookie (- 1 , "foo" , "bar" , None , False ,
937+ ".python.org" , True , True , "/" , True ,
938+ False , None , True , None , None , {}))
939+ self .assertEqual (interact_netscape (c , "http://www.python.org/" ), "" )
940+
914941 def test_strict_domain (self ):
915942 # Cookies whose domain is a country-code tld like .co.uk should
916943 # not be set if CookiePolicy.strict_domain is true.
@@ -1410,6 +1437,7 @@ def cookiejar_from_cookie_headers(headers):
14101437 ["Set-Cookie: b=foo; max-age=oops" ],
14111438 # bad version
14121439 ["Set-Cookie: b=foo; version=spam" ],
1440+ ["Set-Cookie: b=foo; version=-1" ],
14131441 ["Set-Cookie:; Expires=%s" % future ],
14141442 ]:
14151443 c = cookiejar_from_cookie_headers (headers )
0 commit comments