@@ -676,6 +676,8 @@ def test_decode(self):
676
676
c2 = SimpleCookie ()
677
677
c2 .load (c .output ()[12 :])
678
678
self .assertEqual (c ['test' ].value , c2 ['test' ].value )
679
+ c3 = parse_cookie (c .output ()[12 :])
680
+ self .assertEqual (c ['test' ].value , c3 ['test' ])
679
681
680
682
def test_decode_2 (self ):
681
683
"""
@@ -686,6 +688,8 @@ def test_decode_2(self):
686
688
c2 = SimpleCookie ()
687
689
c2 .load (c .output ()[12 :])
688
690
self .assertEqual (c ['test' ].value , c2 ['test' ].value )
691
+ c3 = parse_cookie (c .output ()[12 :])
692
+ self .assertEqual (c ['test' ].value , c3 ['test' ])
689
693
690
694
def test_nonstandard_keys (self ):
691
695
"""
@@ -699,6 +703,52 @@ def test_repeated_nonstandard_keys(self):
699
703
"""
700
704
self .assertIn ('good_cookie' , parse_cookie ('a:=b; a:=c; good_cookie=yes' ).keys ())
701
705
706
+ def test_python_cookies (self ):
707
+ """
708
+ Test cases copied from Python's Lib/test/test_http_cookies.py
709
+ """
710
+ self .assertEqual (parse_cookie ('chips=ahoy; vienna=finger' ), {'chips' : 'ahoy' , 'vienna' : 'finger' })
711
+ # Here parse_cookie() differs from Python's cookie parsing in that it
712
+ # treats all semicolons as delimiters, even within quotes.
713
+ self .assertEqual (
714
+ parse_cookie ('keebler="E=mc2; L=\\ "Loves\\ "; fudge=\\ 012;"' ),
715
+ {'keebler' : '"E=mc2' , 'L' : '\\ "Loves\\ "' , 'fudge' : '\\ 012' , '' : '"' }
716
+ )
717
+ # Illegal cookies that have an '=' char in an unquoted value.
718
+ self .assertEqual (parse_cookie ('keebler=E=mc2' ), {'keebler' : 'E=mc2' })
719
+ # Cookies with ':' character in their name.
720
+ self .assertEqual (parse_cookie ('key:term=value:term' ), {'key:term' : 'value:term' })
721
+ # Cookies with '[' and ']'.
722
+ self .assertEqual (parse_cookie ('a=b; c=[; d=r; f=h' ), {'a' : 'b' , 'c' : '[' , 'd' : 'r' , 'f' : 'h' })
723
+
724
+ def test_cookie_edgecases (self ):
725
+ # Cookies that RFC6265 allows.
726
+ self .assertEqual (parse_cookie ('a=b; Domain=example.com' ), {'a' : 'b' , 'Domain' : 'example.com' })
727
+ # parse_cookie() has historically kept only the last cookie with the
728
+ # same name.
729
+ self .assertEqual (parse_cookie ('a=b; h=i; a=c' ), {'a' : 'c' , 'h' : 'i' })
730
+
731
+ def test_invalid_cookies (self ):
732
+ """
733
+ Cookie strings that go against RFC6265 but browsers will send if set
734
+ via document.cookie.
735
+ """
736
+ # Chunks without an equals sign appear as unnamed values per
737
+ # https://bugzilla.mozilla.org/show_bug.cgi?id=169091
738
+ self .assertIn ('django_language' , parse_cookie ('abc=def; unnamed; django_language=en' ).keys ())
739
+ # Even a double quote may be an unamed value.
740
+ self .assertEqual (parse_cookie ('a=b; "; c=d' ), {'a' : 'b' , '' : '"' , 'c' : 'd' })
741
+ # Spaces in names and values, and an equals sign in values.
742
+ self .assertEqual (parse_cookie ('a b c=d e = f; gh=i' ), {'a b c' : 'd e = f' , 'gh' : 'i' })
743
+ # More characters the spec forbids.
744
+ self .assertEqual (parse_cookie ('a b,c<>@:/[]?{}=d " =e,f g' ), {'a b,c<>@:/[]?{}' : 'd " =e,f g' })
745
+ # Unicode characters. The spec only allows ASCII.
746
+ self .assertEqual (parse_cookie ('saint=André Bessette' ), {'saint' : force_str ('André Bessette' )})
747
+ # Browsers don't send extra whitespace or semicolons in Cookie headers,
748
+ # but parse_cookie() should parse whitespace the same way
749
+ # document.cookie parses whitespace.
750
+ self .assertEqual (parse_cookie (' = b ; ; = ; c = ; ' ), {'' : 'b' , 'c' : '' })
751
+
702
752
def test_httponly_after_load (self ):
703
753
"""
704
754
Test that we can use httponly attribute on cookies that we load
0 commit comments