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