annotate Lib/test/test_cgi.py @ 66807:d28126ebd60d

Entries for datetime, callable, and collections.Counter.
author Raymond Hettinger <python@rcn.com>
date Thu, 09 Dec 2010 16:41:54 +0000
parents 93cd4de8ee0b
children d864328a74e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
1 from test.support import run_unittest, check_warnings
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2 import cgi
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3 import os
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4 import sys
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
5 import tempfile
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
6 import unittest
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
7 from io import StringIO
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 class HackedSysModule:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10 # The regression test will have real values in sys.argv, which
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14606
diff changeset
11 # will completely confuse the test of the cgi module
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 argv = []
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13 stdin = sys.stdin
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15 cgi.sys = HackedSysModule()
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 class ComparableException:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 def __init__(self, err):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 self.err = err
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 def __str__(self):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23 return str(self.err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
25 def __eq__(self, anExc):
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 if not isinstance(anExc, Exception):
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
27 return NotImplemented
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
28 return (self.err.__class__ == anExc.__class__ and
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
29 self.err.args == anExc.args)
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31 def __getattr__(self, attr):
15936
bec0f61cfcd5 Fix a bizarre typo in the helper class ComparableException: the
Guido van Rossum <guido@python.org>
parents: 15305
diff changeset
32 return getattr(self.err, attr)
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34 def do_test(buf, method):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 env = {}
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36 if method == "GET":
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37 fp = None
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
38 env['REQUEST_METHOD'] = 'GET'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39 env['QUERY_STRING'] = buf
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
40 elif method == "POST":
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41 fp = StringIO(buf)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42 env['REQUEST_METHOD'] = 'POST'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
43 env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
44 env['CONTENT_LENGTH'] = str(len(buf))
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
45 else:
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42930
diff changeset
46 raise ValueError("unknown method: %s" % method)
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47 try:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48 return cgi.parse(fp, env, strict_parsing=1)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
49 except Exception as err:
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
50 return ComparableException(err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
51
32758
859ea8612eb0 Don't return spurious empty fields if 'keep_empty_values' is True.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24442
diff changeset
52 parse_strict_test_cases = [
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53 ("", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
54 ("&", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
55 ("&&", ValueError("bad query field: ''")),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
56 (";", ValueError("bad query field: ''")),
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
57 (";&;", ValueError("bad query field: ''")),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
58 # Should the next few really be valid?
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59 ("=", {}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60 ("=&=", {}),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
61 ("=;=", {}),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
62 # This rest seem to make sense
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63 ("=a", {'': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64 ("&=a", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
65 ("=a&", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 ("=&a", ValueError("bad query field: 'a'")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67 ("b=a", {'b': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68 ("b+=a", {'b ': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69 ("a=b=a", {'a': ['b=a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
70 ("a=+b=a", {'a': [' b=a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71 ("&b=a", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
72 ("b&=a", ValueError("bad query field: 'b'")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
73 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74 ("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
75 ("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
76 ("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
77 ("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
78 ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
79 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80 'cuyer': ['r'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81 'expire': ['964546263'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82 'kid': ['130003.300038'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
83 'lobale': ['en-US'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
84 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85 'ss': ['env'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
86 'view': ['bustomer'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
87 }),
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14606
diff changeset
88
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
89 ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse",
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
90 {'SUBMIT': ['Browse'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91 '_assigned_to': ['31392'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92 '_category': ['100'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
93 '_status': ['1'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
94 'group_id': ['5470'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
95 'set': ['custom'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
96 })
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97 ]
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
98
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
99 def norm(seq):
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
100 return sorted(seq, key=repr)
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
101
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
102 def first_elts(list):
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
103 return [p[0] for p in list]
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
104
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
105 def first_second_elts(list):
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
106 return [(p[0], p[1][0]) for p in list]
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
107
48410
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
108 def gen_result(data, environ):
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
109 fake_stdin = StringIO(data)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
110 fake_stdin.seek(0)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
111 form = cgi.FieldStorage(fp=fake_stdin, environ=environ)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
112
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
113 result = {}
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
114 for k, v in dict(form).items():
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
115 result[k] = isinstance(v, list) and form.getlist(k) or v.value
48410
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
116
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
117 return result
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
118
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
119 class CgiTests(unittest.TestCase):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
120
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
121 def test_strict(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
122 for orig, expect in parse_strict_test_cases:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
123 # Test basic parsing
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
124 d = do_test(orig, "GET")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
125 self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
126 d = do_test(orig, "POST")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
127 self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
128
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
129 env = {'QUERY_STRING': orig}
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
130 fs = cgi.FieldStorage(environ=env)
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
131 if isinstance(expect, dict):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
132 # test dict interface
46915
e94574455d9d Remove old backwards-compatibility classes from the cgi module.
Georg Brandl <georg@python.org>
parents: 43014
diff changeset
133 self.assertEqual(len(expect), len(fs))
66530
93cd4de8ee0b Use assertCountEqual instead of assertItemsEqual
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66310
diff changeset
134 self.assertCountEqual(expect.keys(), fs.keys())
46915
e94574455d9d Remove old backwards-compatibility classes from the cgi module.
Georg Brandl <georg@python.org>
parents: 43014
diff changeset
135 ##self.assertEqual(norm(expect.values()), norm(fs.values()))
e94574455d9d Remove old backwards-compatibility classes from the cgi module.
Georg Brandl <georg@python.org>
parents: 43014
diff changeset
136 ##self.assertEqual(norm(expect.items()), norm(fs.items()))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
137 self.assertEqual(fs.getvalue("nonexistent field", "default"), "default")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
138 # test individual fields
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
139 for key in expect.keys():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
140 expect_val = expect[key]
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
141 self.assertIn(key, fs)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
142 if len(expect_val) > 1:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
143 self.assertEqual(fs.getvalue(key), expect_val)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
144 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
145 self.assertEqual(fs.getvalue(key), expect_val[0])
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
146
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
147 def test_log(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
148 cgi.log("Testing")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
149
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
150 cgi.logfp = StringIO()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
151 cgi.initlog("%s", "Testing initlog 1")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
152 cgi.log("%s", "Testing log 2")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
153 self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
154 if os.path.exists("/dev/null"):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
155 cgi.logfp = None
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
156 cgi.logfile = "/dev/null"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
157 cgi.initlog("%s", "Testing log 3")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
158 cgi.log("Testing log 4")
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
159
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
160 def test_fieldstorage_readline(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
161 # FieldStorage uses readline, which has the capacity to read all
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
162 # contents of the input file into memory; we use readline's size argument
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
163 # to prevent that for files that do not contain any newlines in
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
164 # non-GET/HEAD requests
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
165 class TestReadlineFile:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
166 def __init__(self, file):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
167 self.file = file
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
168 self.numcalls = 0
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
169
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
170 def readline(self, size=None):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
171 self.numcalls += 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
172 if size:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
173 return self.file.readline(size)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
174 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
175 return self.file.readline()
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
176
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
177 def __getattr__(self, name):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
178 file = self.__dict__['file']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
179 a = getattr(file, name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
180 if not isinstance(a, int):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
181 setattr(self, name, a)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
182 return a
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
183
42930
cbd50ece3b61 Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
184 f = TestReadlineFile(tempfile.TemporaryFile("w+"))
65774
24b18cd4d5f3 close files properly
Benjamin Peterson <benjamin@python.org>
parents: 59761
diff changeset
185 self.addCleanup(f.close)
42930
cbd50ece3b61 Make test_cgi pass. I changed the internal file created by make_file()
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
186 f.write('x' * 256 * 1024)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
187 f.seek(0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
188 env = {'REQUEST_METHOD':'PUT'}
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
189 fs = cgi.FieldStorage(fp=f, environ=env)
65774
24b18cd4d5f3 close files properly
Benjamin Peterson <benjamin@python.org>
parents: 59761
diff changeset
190 self.addCleanup(fs.file.close)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
191 # if we're not chunking properly, readline is only called twice
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
192 # (by read_binary); if we are chunking properly, it will be called 5 times
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
193 # as long as the chunksize is 1 << 16.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49976
diff changeset
194 self.assertTrue(f.numcalls > 2)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
195
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
196 def test_fieldstorage_multipart(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
197 #Test basic FieldStorage multipart parsing
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
198 env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'}
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
199 postdata = """-----------------------------721837373350705526688164684
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
200 Content-Disposition: form-data; name="id"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
201
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
202 1234
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
203 -----------------------------721837373350705526688164684
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
204 Content-Disposition: form-data; name="title"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
205
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
206
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
207 -----------------------------721837373350705526688164684
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
208 Content-Disposition: form-data; name="file"; filename="test.txt"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
209 Content-Type: text/plain
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
210
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
211 Testing 123.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
212
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
213 -----------------------------721837373350705526688164684
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
214 Content-Disposition: form-data; name="submit"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
215
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
216 Add\x20
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
217 -----------------------------721837373350705526688164684--
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
218 """
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
219 fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65774
diff changeset
220 self.assertEqual(len(fs.list), 4)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
221 expect = [{'name':'id', 'filename':None, 'value':'1234'},
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
222 {'name':'title', 'filename':None, 'value':''},
47632
18eaa1d5f394 Patch by Humberto Diogenes for issue 2849, removing rfc822 module from
Barry Warsaw <barry@python.org>
parents: 47201
diff changeset
223 {'name':'file', 'filename':'test.txt', 'value':'Testing 123.'},
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
224 {'name':'submit', 'filename':None, 'value':' Add '}]
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
225 for x in range(len(fs.list)):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
226 for k, exp in expect[x].items():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
227 got = getattr(fs.list[x], k)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65774
diff changeset
228 self.assertEqual(got, exp)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
229
48410
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
230 _qs_result = {
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
231 'key1': 'value1',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
232 'key2': ['value2x', 'value2y'],
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
233 'key3': 'value3',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
234 'key4': 'value4'
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
235 }
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
236 def testQSAndUrlEncode(self):
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
237 data = "key2=value2x&key3=value3&key4=value4"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
238 environ = {
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
239 'CONTENT_LENGTH': str(len(data)),
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
240 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
241 'QUERY_STRING': 'key1=value1&key2=value2y',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
242 'REQUEST_METHOD': 'POST',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
243 }
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
244 v = gen_result(data, environ)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
245 self.assertEqual(self._qs_result, v)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
246
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
247 def testQSAndFormData(self):
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
248 data = """
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
249 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
250 Content-Disposition: form-data; name="key2"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
251
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
252 value2y
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
253 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
254 Content-Disposition: form-data; name="key3"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
255
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
256 value3
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
257 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
258 Content-Disposition: form-data; name="key4"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
259
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
260 value4
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
261 ---123--
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
262 """
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
263 environ = {
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
264 'CONTENT_LENGTH': str(len(data)),
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
265 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
266 'QUERY_STRING': 'key1=value1&key2=value2x',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
267 'REQUEST_METHOD': 'POST',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
268 }
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
269 v = gen_result(data, environ)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
270 self.assertEqual(self._qs_result, v)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
271
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
272 def testQSAndFormDataFile(self):
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
273 data = """
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
274 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
275 Content-Disposition: form-data; name="key2"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
276
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
277 value2y
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
278 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
279 Content-Disposition: form-data; name="key3"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
280
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
281 value3
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
282 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
283 Content-Disposition: form-data; name="key4"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
284
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
285 value4
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
286 ---123
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
287 Content-Disposition: form-data; name="upload"; filename="fake.txt"
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
288 Content-Type: text/plain
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
289
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
290 this is the content of the fake file
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
291
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
292 ---123--
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
293 """
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
294 environ = {
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
295 'CONTENT_LENGTH': str(len(data)),
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
296 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
297 'QUERY_STRING': 'key1=value1&key2=value2x',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
298 'REQUEST_METHOD': 'POST',
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
299 }
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
300 result = self._qs_result.copy()
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
301 result.update({
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
302 'upload': 'this is the content of the fake file'
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
303 })
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
304 v = gen_result(data, environ)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
305 self.assertEqual(result, v)
81fd8a8baa9a Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47632
diff changeset
306
49081
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
307 def test_deprecated_parse_qs(self):
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
308 # this func is moved to urllib.parse, this is just a sanity check
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
309 with check_warnings(('cgi.parse_qs is deprecated, use urllib.parse.'
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
310 'parse_qs instead', DeprecationWarning)):
49101
c46bcccf2af2 Added a warning filter to don't show the warning during
Facundo Batista <facundobatista@gmail.com>
parents: 49081
diff changeset
311 self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']},
c46bcccf2af2 Added a warning filter to don't show the warning during
Facundo Batista <facundobatista@gmail.com>
parents: 49081
diff changeset
312 cgi.parse_qs('a=A1&b=B2&B=B3'))
49081
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
313
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
314 def test_deprecated_parse_qsl(self):
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
315 # this func is moved to urllib.parse, this is just a sanity check
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
316 with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.'
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
317 'parse_qsl instead', DeprecationWarning)):
49101
c46bcccf2af2 Added a warning filter to don't show the warning during
Facundo Batista <facundobatista@gmail.com>
parents: 49081
diff changeset
318 self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
c46bcccf2af2 Added a warning filter to don't show the warning during
Facundo Batista <facundobatista@gmail.com>
parents: 49081
diff changeset
319 cgi.parse_qsl('a=A1&b=B2&B=B3'))
49081
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
320
49976
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
321 def test_parse_header(self):
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
322 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
323 cgi.parse_header("text/plain"),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
324 ("text/plain", {}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
325 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
326 cgi.parse_header("text/vnd.just.made.this.up ; "),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
327 ("text/vnd.just.made.this.up", {}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
328 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
329 cgi.parse_header("text/plain;charset=us-ascii"),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
330 ("text/plain", {"charset": "us-ascii"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
331 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
332 cgi.parse_header('text/plain ; charset="us-ascii"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
333 ("text/plain", {"charset": "us-ascii"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
334 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
335 cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
336 ("text/plain", {"charset": "us-ascii", "another": "opt"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
337 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
338 cgi.parse_header('attachment; filename="silly.txt"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
339 ("attachment", {"filename": "silly.txt"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
340 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
341 cgi.parse_header('attachment; filename="strange;name"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
342 ("attachment", {"filename": "strange;name"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
343 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
344 cgi.parse_header('attachment; filename="strange;name";size=123;'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
345 ("attachment", {"filename": "strange;name", "size": "123"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
346
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
347
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
348 def test_main():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
349 run_unittest(CgiTests)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
350
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
351 if __name__ == '__main__':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
352 test_main()