@@ -1892,6 +1892,75 @@ def save_and_restore(cj, ignore_discard):
18921892 self .assertEqual (len (new_c ), 4 ) # 2 of them discarded on save
18931893 self .assertIn ("name='foo1', value='bar'" , repr (new_c ))
18941894
1895+ def test_mozilla_httponly_prefix (self ):
1896+ # Save / load Mozilla/Netscape cookie file with HttpOnly prefix.
1897+ filename = os_helper .TESTFN
1898+
1899+ # Load the input file test
1900+ c1 = MozillaCookieJar (filename )
1901+ one_year_later = int (time .time ()) + 365 * 24 * 60 * 60
1902+ try :
1903+ with open (filename , "w" ) as f :
1904+ f .write ("# Netscape HTTP Cookie File\n " )
1905+ f .write ("#HttpOnly_.example.com\t TRUE\t /\t FALSE\t %d\t foo\t bar\n "
1906+ % (one_year_later ,))
1907+ c1 .load ()
1908+ finally :
1909+ os_helper .unlink (filename )
1910+
1911+ cookie = list (c1 )[0 ]
1912+ self .assertIn ("HttpOnly" , repr (cookie ))
1913+ self .assertTrue (cookie .has_nonstandard_attr ("HttpOnly" , case_insensitive = True ))
1914+ self .assertTrue (cookie .has_nonstandard_attr ("HTTPOnly" , case_insensitive = True ))
1915+ self .assertFalse (cookie .has_nonstandard_attr ("HTTPOnly" ))
1916+
1917+ # Save and read the output file test
1918+ c2 = MozillaCookieJar (filename )
1919+ year_plus_one = time .localtime ()[0 ] + 1
1920+ expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one ,)
1921+ # foo1 has the HttpOnly flag set
1922+ interact_netscape (c2 , "http://example.com/" ,
1923+ "foo1=bar1; %s; HttpOnly;" % expires )
1924+ # foo2 will have the HttpOnly flag set later
1925+ interact_netscape (c2 , "http://example.com/" ,
1926+ "foo2=bar2; %s;" % expires )
1927+ # foo3 will have the HTTPOnly flag set later
1928+ interact_netscape (c2 , "http://example.com/" ,
1929+ "foo3=bar3; %s;" % expires )
1930+ # foo4 does not have the HttpOnly flag set
1931+ interact_netscape (c2 , "http://example.com/" ,
1932+ "foo4=bar4; %s;" % expires )
1933+ # Set flags manually
1934+ for cookie in c2 :
1935+ if cookie .name == "foo2" :
1936+ cookie .set_nonstandard_attr ("HttpOnly" , "" )
1937+ if cookie .name == "foo3" :
1938+ cookie .set_nonstandard_attr ("HTTPOnly" , "" )
1939+
1940+ # Save and read the output file
1941+ try :
1942+ c2 .save ()
1943+ with open (filename , "r" ) as f :
1944+ lines = f .readlines ()
1945+ finally :
1946+ os_helper .unlink (filename )
1947+
1948+ # Check that the HttpOnly prefix is added to the correct cookies
1949+ for value in ["foo1" , "foo2" , "foo3" ]:
1950+ matches = [x for x in lines if value in x ]
1951+ self .assertEqual (len (matches ), 1 ,
1952+ "Incorrect number of matches for cookie with value %r" % value )
1953+ self .assertTrue (matches [0 ].startswith ("#HttpOnly_" ),
1954+ "Cookie with value %r is missing the HttpOnly prefix" % value )
1955+
1956+ # Check that the HttpOnly prefix is not added to the correct cookies
1957+ for value in ["foo4" ]:
1958+ matches = [x for x in lines if value in x ]
1959+ self .assertEqual (len (matches ), 1 ,
1960+ "Incorrect number of matches for cookie with value %r" % value )
1961+ self .assertFalse (matches [0 ].startswith ("#HttpOnly_" ),
1962+ "Cookie with value %r has the HttpOnly prefix" % value )
1963+
18951964 def test_netscape_misc (self ):
18961965 # Some additional Netscape cookies tests.
18971966 c = CookieJar ()
0 commit comments