Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cookie.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 1db41c410f63 |
| children | a7d8c28280c0 |
| rev | line source |
|---|---|
| 14072 | 1 # Simple test suite for Cookie.py |
| 2 | |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63511
diff
changeset
|
3 from test.test_support import run_unittest, run_doctest, check_warnings |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
4 import unittest |
| 14072 | 5 import Cookie |
| 6 | |
|
26820
6bbbe67d1eaf
Suppress unsafe *Cookie class warnings
Andrew M. Kuchling <amk@amk.ca>
parents:
24364
diff
changeset
|
7 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
8 class CookieTests(unittest.TestCase): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
9 # Currently this only tests SimpleCookie |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
10 def test_basic(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
11 cases = [ |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
12 { 'data': 'chips=ahoy; vienna=finger', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
13 'dict': {'chips':'ahoy', 'vienna':'finger'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
14 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>", |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
15 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
16 }, |
| 14072 | 17 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
18 { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
19 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
20 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
21 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
22 }, |
|
16716
ef64dc68520f
Added test for patch #103473: test an unquoted cookie value containing '='
Andrew M. Kuchling <amk@amk.ca>
parents:
15966
diff
changeset
|
23 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
24 # Check illegal cookies that have an '=' char in an unquoted value |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
25 { 'data': 'keebler=E=mc2', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
26 'dict': {'keebler' : 'E=mc2'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
27 'repr': "<SimpleCookie: keebler='E=mc2'>", |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
28 'output': 'Set-Cookie: keebler=E=mc2', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
29 } |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
30 ] |
| 14072 | 31 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
32 for case in cases: |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
33 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
34 C.load(case['data']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
35 self.assertEqual(repr(C), case['repr']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
36 self.assertEqual(C.output(sep='\n'), case['output']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
37 for k, v in sorted(case['dict'].iteritems()): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
38 self.assertEqual(C[k].value, v) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
39 |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
40 def test_load(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
41 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
42 C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
| 14072 | 43 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
44 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
45 self.assertEqual(C['Customer']['version'], '1') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
46 self.assertEqual(C['Customer']['path'], '/acme') |
|
14074
713dd76be37b
Adding tests of the "attrs" optional argument, and of the js_output
Moshe Zadka <moshez@math.huji.ac.il>
parents:
14072
diff
changeset
|
47 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
48 self.assertEqual(C.output(['path']), |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
49 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
|
52716
c33959392953
Fixing the issue4860. Escaping embedded '"' character in js_output() method of Morsel.
Senthil Kumaran <orsenthil@gmail.com>
parents:
40263
diff
changeset
|
50 self.assertEqual(C.js_output(), r""" |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
51 <script type="text/javascript"> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
52 <!-- begin hiding |
|
52716
c33959392953
Fixing the issue4860. Escaping embedded '"' character in js_output() method of Morsel.
Senthil Kumaran <orsenthil@gmail.com>
parents:
40263
diff
changeset
|
53 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
54 // end hiding --> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
55 </script> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
56 """) |
|
52716
c33959392953
Fixing the issue4860. Escaping embedded '"' character in js_output() method of Morsel.
Senthil Kumaran <orsenthil@gmail.com>
parents:
40263
diff
changeset
|
57 self.assertEqual(C.js_output(['path']), r""" |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
58 <script type="text/javascript"> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
59 <!-- begin hiding |
|
52716
c33959392953
Fixing the issue4860. Escaping embedded '"' character in js_output() method of Morsel.
Senthil Kumaran <orsenthil@gmail.com>
parents:
40263
diff
changeset
|
60 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
61 // end hiding --> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
62 </script> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
63 """) |
|
14074
713dd76be37b
Adding tests of the "attrs" optional argument, and of the js_output
Moshe Zadka <moshez@math.huji.ac.il>
parents:
14072
diff
changeset
|
64 |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
65 # loading 'expires' |
|
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
66 C = Cookie.SimpleCookie() |
|
77063
1db41c410f63
Issue #14426: Correct the Date format in Expires attribute of Set-Cookie. Patch by Federico Reghenzani and Müte Invert
Senthil Kumaran <senthil@uthcode.com>
parents:
68256
diff
changeset
|
67 C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT') |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
68 self.assertEqual(C['Customer']['expires'], |
|
77063
1db41c410f63
Issue #14426: Correct the Date format in Expires attribute of Set-Cookie. Patch by Federico Reghenzani and Müte Invert
Senthil Kumaran <senthil@uthcode.com>
parents:
68256
diff
changeset
|
69 'Wed, 01 Jan 2010 00:00:00 GMT') |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
70 C = Cookie.SimpleCookie() |
|
77063
1db41c410f63
Issue #14426: Correct the Date format in Expires attribute of Set-Cookie. Patch by Federico Reghenzani and Müte Invert
Senthil Kumaran <senthil@uthcode.com>
parents:
68256
diff
changeset
|
71 C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT') |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
72 self.assertEqual(C['Customer']['expires'], |
|
77063
1db41c410f63
Issue #14426: Correct the Date format in Expires attribute of Set-Cookie. Patch by Federico Reghenzani and Müte Invert
Senthil Kumaran <senthil@uthcode.com>
parents:
68256
diff
changeset
|
73 'Wed, 01 Jan 98 00:00:00 GMT') |
|
63511
61ad81a774c5
Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents:
40263
diff
changeset
|
74 |
|
67196
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
75 def test_extended_encode(self): |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
76 # Issue 9824: some browsers don't follow the standard; we now |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
77 # encode , and ; to keep them from tripping up. |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
78 C = Cookie.SimpleCookie() |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
79 C['val'] = "some,funky;stuff" |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
80 self.assertEqual(C.output(['val']), |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
81 'Set-Cookie: val="some\\054funky\\073stuff"') |
|
9559e1152595
Merged revisions 87550 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
63437
diff
changeset
|
82 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
83 def test_quoted_meta(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
84 # Try cookie with quoted meta-data |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
85 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
86 C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
87 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
88 self.assertEqual(C['Customer']['version'], '1') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
89 self.assertEqual(C['Customer']['path'], '/acme') |
|
17331
d328cfeb0c75
Since Guido fiddled Cookie.py to work with doctest, it's a Good Thing to
Tim Peters <tim.peters@gmail.com>
parents:
16716
diff
changeset
|
90 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
91 def test_main(): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
92 run_unittest(CookieTests) |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63511
diff
changeset
|
93 with check_warnings(('.+Cookie class is insecure; do not use it', |
|
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63511
diff
changeset
|
94 DeprecationWarning)): |
|
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63511
diff
changeset
|
95 run_doctest(Cookie) |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
96 |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
97 if __name__ == '__main__': |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
98 test_main() |
