Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cookie.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 8d6d315b2af3 |
| children | f340cb045bf9 61ad81a774c5 c33959392953 |
| rev | line source |
|---|---|
| 14072 | 1 # Simple test suite for Cookie.py |
| 2 | |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
3 from test.test_support import run_unittest, run_doctest |
|
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 import warnings |
|
6bbbe67d1eaf
Suppress unsafe *Cookie class warnings
Andrew M. Kuchling <amk@amk.ca>
parents:
24364
diff
changeset
|
8 warnings.filterwarnings("ignore", |
|
6bbbe67d1eaf
Suppress unsafe *Cookie class warnings
Andrew M. Kuchling <amk@amk.ca>
parents:
24364
diff
changeset
|
9 ".* class is insecure.*", |
|
6bbbe67d1eaf
Suppress unsafe *Cookie class warnings
Andrew M. Kuchling <amk@amk.ca>
parents:
24364
diff
changeset
|
10 DeprecationWarning) |
|
6bbbe67d1eaf
Suppress unsafe *Cookie class warnings
Andrew M. Kuchling <amk@amk.ca>
parents:
24364
diff
changeset
|
11 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
12 class CookieTests(unittest.TestCase): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
13 # Currently this only tests SimpleCookie |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
14 def test_basic(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
15 cases = [ |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
16 { 'data': 'chips=ahoy; vienna=finger', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
17 'dict': {'chips':'ahoy', 'vienna':'finger'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
18 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>", |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
19 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
20 }, |
| 14072 | 21 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
22 { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
23 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
24 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
25 '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
|
26 }, |
|
16716
ef64dc68520f
Added test for patch #103473: test an unquoted cookie value containing '='
Andrew M. Kuchling <amk@amk.ca>
parents:
15966
diff
changeset
|
27 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
28 # 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
|
29 { 'data': 'keebler=E=mc2', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
30 'dict': {'keebler' : 'E=mc2'}, |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
31 'repr': "<SimpleCookie: keebler='E=mc2'>", |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
32 'output': 'Set-Cookie: keebler=E=mc2', |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
33 } |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
34 ] |
| 14072 | 35 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
36 for case in cases: |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
37 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
38 C.load(case['data']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
39 self.assertEqual(repr(C), case['repr']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
40 self.assertEqual(C.output(sep='\n'), case['output']) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
41 for k, v in sorted(case['dict'].iteritems()): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
42 self.assertEqual(C[k].value, v) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
43 |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
44 def test_load(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
45 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
46 C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
| 14072 | 47 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
48 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
49 self.assertEqual(C['Customer']['version'], '1') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
50 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
|
51 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
52 self.assertEqual(C.output(['path']), |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
53 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
54 self.assertEqual(C.js_output(), """ |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
55 <script type="text/javascript"> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
56 <!-- begin hiding |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
57 document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme; Version=1"; |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
58 // end hiding --> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
59 </script> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
60 """) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
61 self.assertEqual(C.js_output(['path']), """ |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
62 <script type="text/javascript"> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
63 <!-- begin hiding |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
64 document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme"; |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
65 // end hiding --> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
66 </script> |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
67 """) |
|
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
|
68 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
69 def test_quoted_meta(self): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
70 # Try cookie with quoted meta-data |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
71 C = Cookie.SimpleCookie() |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
72 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
|
73 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
74 self.assertEqual(C['Customer']['version'], '1') |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
75 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
|
76 |
|
40263
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
77 def test_main(): |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
78 run_unittest(CookieTests) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
79 run_doctest(Cookie) |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
80 |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
81 if __name__ == '__main__': |
|
8d6d315b2af3
Convert test_cookie to unittest.
Georg Brandl <georg@python.org>
parents:
35434
diff
changeset
|
82 test_main() |
