annotate Lib/test/test_cgi.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 13922f6d87f2
children a46a0dafcb7a 70712a806bdb
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
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
7 from io import StringIO, BytesIO
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 class ComparableException:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 def __init__(self, err):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 self.err = err
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21 def __str__(self):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 return str(self.err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
24 def __eq__(self, anExc):
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25 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
26 return NotImplemented
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
27 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
28 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
29
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30 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
31 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
32
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33 def do_test(buf, method):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34 env = {}
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 if method == "GET":
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36 fp = None
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37 env['REQUEST_METHOD'] = 'GET'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
38 env['QUERY_STRING'] = buf
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39 elif method == "POST":
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
40 fp = BytesIO(buf.encode('latin-1')) # FieldStorage expects bytes
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41 env['REQUEST_METHOD'] = 'POST'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42 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
43 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
44 else:
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42930
diff changeset
45 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
46 try:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47 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
48 except Exception as err:
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
49 return ComparableException(err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
50
32758
859ea8612eb0 Don't return spurious empty fields if 'keep_empty_values' is True.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24442
diff changeset
51 parse_strict_test_cases = [
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
52 ("", ValueError("bad query field: ''")),
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: ''")),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
55 (";", 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
56 (";&;", ValueError("bad query field: ''")),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
57 # 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
58 ("=", {}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59 ("=&=", {}),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
60 ("=;=", {}),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61 # 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
62 ("=a", {'': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63 ("&=a", ValueError("bad query field: ''")),
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: 'a'")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 ("b=a", {'b': ['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 ("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
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 ("&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
71 ("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
72 ("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
73 ("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
74 ("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
75 ("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
76 ("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
77 ("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
78 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
79 'cuyer': ['r'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80 'expire': ['964546263'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81 'kid': ['130003.300038'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82 'lobale': ['en-US'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
83 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
84 'ss': ['env'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85 'view': ['bustomer'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
86 }),
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14606
diff changeset
87
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
88 ("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
89 {'SUBMIT': ['Browse'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
90 '_assigned_to': ['31392'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91 '_category': ['100'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92 '_status': ['1'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
93 'group_id': ['5470'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
94 'set': ['custom'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
95 })
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
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
98 def norm(seq):
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
99 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
100
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
101 def first_elts(list):
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
102 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
103
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
104 def first_second_elts(list):
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
105 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
106
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
107 def gen_result(data, environ):
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
108 encoding = 'latin-1'
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
109 fake_stdin = BytesIO(data.encode(encoding))
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
110 fake_stdin.seek(0)
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
111 form = cgi.FieldStorage(fp=fake_stdin, environ=environ, encoding=encoding)
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
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
75582
13922f6d87f2 3.2 - Fix closes Issue14281 - Test for cgi.escape by Brian Landers
Senthil Kumaran <senthil@uthcode.com>
parents: 72989
diff changeset
121 def test_escape(self):
13922f6d87f2 3.2 - Fix closes Issue14281 - Test for cgi.escape by Brian Landers
Senthil Kumaran <senthil@uthcode.com>
parents: 72989
diff changeset
122 self.assertEqual("test &amp; string", cgi.escape("test & string"))
13922f6d87f2 3.2 - Fix closes Issue14281 - Test for cgi.escape by Brian Landers
Senthil Kumaran <senthil@uthcode.com>
parents: 72989
diff changeset
123 self.assertEqual("&lt;test string&gt;", cgi.escape("<test string>"))
13922f6d87f2 3.2 - Fix closes Issue14281 - Test for cgi.escape by Brian Landers
Senthil Kumaran <senthil@uthcode.com>
parents: 72989
diff changeset
124 self.assertEqual("&quot;test string&quot;", cgi.escape('"test string"', True))
13922f6d87f2 3.2 - Fix closes Issue14281 - Test for cgi.escape by Brian Landers
Senthil Kumaran <senthil@uthcode.com>
parents: 72989
diff changeset
125
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
126 def test_strict(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
127 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
128 # Test basic parsing
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
129 d = do_test(orig, "GET")
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
130 self.assertEqual(d, expect, "Error parsing %s method GET" % repr(orig))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
131 d = do_test(orig, "POST")
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
132 self.assertEqual(d, expect, "Error parsing %s method POST" % repr(orig))
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
134 env = {'QUERY_STRING': orig}
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
135 fs = cgi.FieldStorage(environ=env)
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58415
diff changeset
136 if isinstance(expect, dict):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
137 # test dict interface
46915
e94574455d9d Remove old backwards-compatibility classes from the cgi module.
Georg Brandl <georg@python.org>
parents: 43014
diff changeset
138 self.assertEqual(len(expect), len(fs))
66530
93cd4de8ee0b Use assertCountEqual instead of assertItemsEqual
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66310
diff changeset
139 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
140 ##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
141 ##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
142 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
143 # test individual fields
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
144 for key in expect.keys():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
145 expect_val = expect[key]
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
146 self.assertIn(key, fs)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
147 if len(expect_val) > 1:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
148 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
149 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
150 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
151
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
152 def test_log(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
153 cgi.log("Testing")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
154
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
155 cgi.logfp = StringIO()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
156 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
157 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
158 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
159 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
160 cgi.logfp = None
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
161 cgi.logfile = "/dev/null"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
162 cgi.initlog("%s", "Testing log 3")
71324
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
163 def log_cleanup():
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
164 """Restore the global state of the log vars."""
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
165 cgi.logfile = ''
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
166 cgi.logfp.close()
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
167 cgi.logfp = None
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
168 cgi.log = cgi.initlog
530ba6c7e578 Restore the global state of the log vars, so that test_cgi can be run twice without failures.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
169 self.addCleanup(log_cleanup)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
170 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
171
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
172 def test_fieldstorage_readline(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
173 # 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
174 # 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
175 # 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
176 # non-GET/HEAD requests
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
177 class TestReadlineFile:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
178 def __init__(self, file):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
179 self.file = file
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
180 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
181
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
182 def readline(self, size=None):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
183 self.numcalls += 1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
184 if size:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
185 return self.file.readline(size)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
186 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
187 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
188
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
189 def __getattr__(self, name):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
190 file = self.__dict__['file']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
191 a = getattr(file, name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
192 if not isinstance(a, int):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
193 setattr(self, name, a)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
194 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
195
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
196 f = TestReadlineFile(tempfile.TemporaryFile("wb+"))
65774
24b18cd4d5f3 close files properly
Benjamin Peterson <benjamin@python.org>
parents: 59761
diff changeset
197 self.addCleanup(f.close)
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
198 f.write(b'x' * 256 * 1024)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
199 f.seek(0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
200 env = {'REQUEST_METHOD':'PUT'}
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
201 fs = cgi.FieldStorage(fp=f, environ=env)
65774
24b18cd4d5f3 close files properly
Benjamin Peterson <benjamin@python.org>
parents: 59761
diff changeset
202 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
203 # 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
204 # (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
205 # 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
206 self.assertTrue(f.numcalls > 2)
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
207 f.close()
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
208
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
209 def test_fieldstorage_multipart(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
210 #Test basic FieldStorage multipart parsing
68200
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
211 env = {
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
212 'REQUEST_METHOD': 'POST',
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
213 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY),
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
214 'CONTENT_LENGTH': '558'}
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
215 fp = BytesIO(POSTDATA.encode('latin-1'))
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
216 fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1")
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
217 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
218 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
219 {'name':'title', 'filename':None, 'value':''},
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
220 {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'},
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
221 {'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
222 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
223 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
224 got = getattr(fs.list[x], k)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
225 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
226
68200
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
227 def test_fieldstorage_multipart_non_ascii(self):
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
228 #Test basic FieldStorage multipart parsing
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
229 env = {'REQUEST_METHOD':'POST',
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
230 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY),
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
231 'CONTENT_LENGTH':'558'}
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
232 for encoding in ['iso-8859-1','utf-8']:
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
233 fp = BytesIO(POSTDATA_NON_ASCII.encode(encoding))
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
234 fs = cgi.FieldStorage(fp, environ=env,encoding=encoding)
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
235 self.assertEqual(len(fs.list), 1)
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
236 expect = [{'name':'id', 'filename':None, 'value':'\xe7\xf1\x80'}]
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
237 for x in range(len(fs.list)):
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
238 for k, exp in expect[x].items():
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
239 got = getattr(fs.list[x], k)
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
240 self.assertEqual(got, exp)
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
241
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
242 _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
243 '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
244 '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
245 '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
246 '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
247 }
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 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
249 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
250 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
251 '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
252 '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
253 '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
254 '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
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 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
257 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
258
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 def testQSAndFormData(self):
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
260 data = """---123
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
261 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
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 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
264 ---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
265 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
266
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 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
268 ---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
269 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
270
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 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
272 ---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
273 """
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 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
275 '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
276 '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
277 '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
278 '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
279 }
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 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
281 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
282
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 def testQSAndFormDataFile(self):
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
284 data = """---123
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
285 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
286
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 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
288 ---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
289 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
290
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 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
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 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
294
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 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
296 ---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 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
298 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
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 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
301
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 ---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
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 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 '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
306 '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
307 '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
308 '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
309 }
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
310 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
311 result.update({
67590
d864328a74e4 Issue #4953: cgi.FieldStorage and cgi.parse() parse the request as bytes, not
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66530
diff changeset
312 'upload': b'this is the content of the fake file\n'
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
313 })
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
314 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
315 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
316
49081
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
317 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
318 # 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
319 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
320 '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
321 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
322 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
323
f085f39a6f4d Added sanity checks for the deprecated parse_qs() and
Facundo Batista <facundobatista@gmail.com>
parents: 48998
diff changeset
324 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
325 # 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
326 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
327 '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
328 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
329 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
330
49976
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
331 def test_parse_header(self):
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
332 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
333 cgi.parse_header("text/plain"),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
334 ("text/plain", {}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
335 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
336 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
337 ("text/vnd.just.made.this.up", {}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
338 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
339 cgi.parse_header("text/plain;charset=us-ascii"),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
340 ("text/plain", {"charset": "us-ascii"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
341 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
342 cgi.parse_header('text/plain ; charset="us-ascii"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
343 ("text/plain", {"charset": "us-ascii"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
344 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
345 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
346 ("text/plain", {"charset": "us-ascii", "another": "opt"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
347 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
348 cgi.parse_header('attachment; filename="silly.txt"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
349 ("attachment", {"filename": "silly.txt"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
350 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
351 cgi.parse_header('attachment; filename="strange;name"'),
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
352 ("attachment", {"filename": "strange;name"}))
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
353 self.assertEqual(
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
354 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
355 ("attachment", {"filename": "strange;name", "size": "123"}))
72989
f5bd78b11275 3.2 - Fix closes Issue12529 - cgi.parse_header failure on double quotes and
Senthil Kumaran <senthil@uthcode.com>
parents: 71324
diff changeset
356 self.assertEqual(
f5bd78b11275 3.2 - Fix closes Issue12529 - cgi.parse_header failure on double quotes and
Senthil Kumaran <senthil@uthcode.com>
parents: 71324
diff changeset
357 cgi.parse_header('form-data; name="files"; filename="fo\\"o;bar"'),
f5bd78b11275 3.2 - Fix closes Issue12529 - cgi.parse_header failure on double quotes and
Senthil Kumaran <senthil@uthcode.com>
parents: 71324
diff changeset
358 ("form-data", {"name": "files", "filename": 'fo"o;bar'}))
f5bd78b11275 3.2 - Fix closes Issue12529 - cgi.parse_header failure on double quotes and
Senthil Kumaran <senthil@uthcode.com>
parents: 71324
diff changeset
359
49976
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
360
68200
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
361 BOUNDARY = "---------------------------721837373350705526688164684"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
362
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
363 POSTDATA = """-----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
364 Content-Disposition: form-data; name="id"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
365
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
366 1234
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
367 -----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
368 Content-Disposition: form-data; name="title"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
369
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
370
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
371 -----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
372 Content-Disposition: form-data; name="file"; filename="test.txt"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
373 Content-Type: text/plain
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
374
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
375 Testing 123.
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
376
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
377 -----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
378 Content-Disposition: form-data; name="submit"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
379
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
380 Add\x20
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
381 -----------------------------721837373350705526688164684--
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
382 """
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
383
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
384 POSTDATA_NON_ASCII = """-----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
385 Content-Disposition: form-data; name="id"
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
386
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
387 \xe7\xf1\x80
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
388 -----------------------------721837373350705526688164684
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
389 """
1d198518cb87 Issue #10911: Add tests on CGI with non-ASCII characters
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68095
diff changeset
390
49976
89e738c792c0 Merged revisions 67534 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49101
diff changeset
391
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
392 def test_main():
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
393 run_unittest(CgiTests)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
394
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
395 if __name__ == '__main__':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39644
diff changeset
396 test_main()