annotate Lib/test/test_cgi.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents f0bc6d8aa21e
children 7df3e13d3e50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1 from test.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
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
5 import tempfile
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
6 import unittest
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
7 from StringIO 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 try:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 from cStringIO import StringIO
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 except ImportError:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 from StringIO import StringIO
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 class ComparableException:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23 def __init__(self, err):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 self.err = err
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 def __str__(self):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 return str(self.err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
28
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
29 def __cmp__(self, anExc):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30 if not isinstance(anExc, Exception):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31 return -1
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
32 x = cmp(self.err.__class__, anExc.__class__)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33 if x != 0:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34 return x
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 return cmp(self.err.args, anExc.args)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37 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
38 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
39
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
40 def do_test(buf, method):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41 env = {}
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42 if method == "GET":
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
43 fp = None
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
44 env['REQUEST_METHOD'] = 'GET'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
45 env['QUERY_STRING'] = buf
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
46 elif method == "POST":
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47 fp = StringIO(buf)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48 env['REQUEST_METHOD'] = 'POST'
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
49 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
50 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
51 else:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
52 raise ValueError, "unknown method: %s" % method
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53 try:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
54 return cgi.parse(fp, env, strict_parsing=1)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
55 except StandardError, err:
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
56 return ComparableException(err)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
57
32758
859ea8612eb0 Don't return spurious empty fields if 'keep_empty_values' is True.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 24442
diff changeset
58 parse_strict_test_cases = [
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59 ("", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60 ("&", ValueError("bad query field: ''")),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61 ("&&", 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
62 (";", 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
63 (";&;", ValueError("bad query field: ''")),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64 # 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
65 ("=", {}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 ("=&=", {}),
14606
f2fc66a6153b fix bug #110661 (PR#356) -- accept either & or ; as separator for CGI
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14196
diff changeset
67 ("=;=", {}),
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68 # 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
69 ("=a", {'': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
70 ("&=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 ("=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 ("=&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
73 ("b=a", {'b': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74 ("b+=a", {'b ': ['a']}),
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
75 ("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
76 ("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
77 ("&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
78 ("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
79 ("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
80 ("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
81 ("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
82 ("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
83 ("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
84 ("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
85 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
86 'cuyer': ['r'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
87 'expire': ['964546263'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
88 'kid': ['130003.300038'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
89 'lobale': ['en-US'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
90 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91 'ss': ['env'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92 'view': ['bustomer'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
93 }),
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14606
diff changeset
94
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
95 ("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
96 {'SUBMIT': ['Browse'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97 '_assigned_to': ['31392'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
98 '_category': ['100'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
99 '_status': ['1'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
100 'group_id': ['5470'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
101 'set': ['custom'],
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
102 })
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
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
105 def first_elts(list):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
106 return map(lambda x:x[0], list)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
107
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
108 def first_second_elts(list):
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
109 return map(lambda p:(p[0], p[1][0]), list)
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
110
47798
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
111 def gen_result(data, environ):
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
112 fake_stdin = StringIO(data)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
113 fake_stdin.seek(0)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
114 form = cgi.FieldStorage(fp=fake_stdin, environ=environ)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
115
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
116 result = {}
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
117 for k, v in dict(form).items():
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
118 result[k] = type(v) is list and form.getlist(k) or v.value
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
119
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
120 return result
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
121
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
122 class CgiTests(unittest.TestCase):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
123
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
124 def test_strict(self):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
125 for orig, expect in parse_strict_test_cases:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
126 # Test basic parsing
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
127 d = do_test(orig, "GET")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
128 self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
129 d = do_test(orig, "POST")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
130 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
131
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
132 env = {'QUERY_STRING': orig}
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
133 fcd = cgi.FormContentDict(env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
134 sd = cgi.SvFormContentDict(env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
135 fs = cgi.FieldStorage(environ=env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
136 if type(expect) == type({}):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
137 # test dict interface
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
138 self.assertEqual(len(expect), len(fcd))
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
139 self.assertSameElements(expect.keys(), fcd.keys())
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
140 self.assertSameElements(expect.values(), fcd.values())
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
141 self.assertSameElements(expect.items(), fcd.items())
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
142 self.assertEqual(fcd.get("nonexistent field", "default"), "default")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
143 self.assertEqual(len(sd), len(fs))
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
144 self.assertSameElements(sd.keys(), fs.keys())
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
145 self.assertEqual(fs.getvalue("nonexistent field", "default"), "default")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
146 # test individual fields
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
147 for key in expect.keys():
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
148 expect_val = expect[key]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49974
diff changeset
149 self.assertTrue(fcd.has_key(key))
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
150 self.assertSameElements(fcd[key], expect[key])
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
151 self.assertEqual(fcd.get(key, "default"), fcd[key])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49974
diff changeset
152 self.assertTrue(fs.has_key(key))
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
153 if len(expect_val) > 1:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
154 single_value = 0
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
155 else:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
156 single_value = 1
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
157 try:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
158 val = sd[key]
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
159 except IndexError:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49974
diff changeset
160 self.assertFalse(single_value)
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
161 self.assertEqual(fs.getvalue(key), expect_val)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
162 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49974
diff changeset
163 self.assertTrue(single_value)
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
164 self.assertEqual(val, expect_val[0])
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
165 self.assertEqual(fs.getvalue(key), expect_val[0])
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
166 self.assertSameElements(sd.getlist(key), expect_val)
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
167 if single_value:
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
168 self.assertSameElements(sd.values(),
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
169 first_elts(expect.values()))
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
170 self.assertSameElements(sd.items(),
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
171 first_second_elts(expect.items()))
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
172
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
173 def test_weird_formcontentdict(self):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
174 # Test the weird FormContentDict classes
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
175 env = {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"}
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
176 expect = {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'}
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
177 d = cgi.InterpFormContentDict(env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
178 for k, v in expect.items():
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
179 self.assertEqual(d[k], v)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
180 for k, v in d.items():
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
181 self.assertEqual(expect[k], v)
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
182 self.assertSameElements(expect.values(), d.values())
13842
87488c3113f2 test the non-multipart sections of the cgi module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
183
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
184 def test_log(self):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
185 cgi.log("Testing")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
186
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
187 cgi.logfp = StringIO()
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
188 cgi.initlog("%s", "Testing initlog 1")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
189 cgi.log("%s", "Testing log 2")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
190 self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
191 if os.path.exists("/dev/null"):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
192 cgi.logfp = None
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
193 cgi.logfile = "/dev/null"
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
194 cgi.initlog("%s", "Testing log 3")
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
195 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
196
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
197 def test_fieldstorage_readline(self):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
198 # FieldStorage uses readline, which has the capacity to read all
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
199 # contents of the input file into memory; we use readline's size argument
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
200 # to prevent that for files that do not contain any newlines in
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
201 # non-GET/HEAD requests
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
202 class TestReadlineFile:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
203 def __init__(self, file):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
204 self.file = file
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
205 self.numcalls = 0
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
206
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
207 def readline(self, size=None):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
208 self.numcalls += 1
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
209 if size:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
210 return self.file.readline(size)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
211 else:
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
212 return self.file.readline()
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
213
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
214 def __getattr__(self, name):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
215 file = self.__dict__['file']
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
216 a = getattr(file, name)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
217 if not isinstance(a, int):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
218 setattr(self, name, a)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
219 return a
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
220
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
221 f = TestReadlineFile(tempfile.TemporaryFile())
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
222 f.write('x' * 256 * 1024)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
223 f.seek(0)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
224 env = {'REQUEST_METHOD':'PUT'}
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
225 fs = cgi.FieldStorage(fp=f, environ=env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
226 # if we're not chunking properly, readline is only called twice
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
227 # (by read_binary); if we are chunking properly, it will be called 5 times
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
228 # as long as the chunksize is 1 << 16.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 49974
diff changeset
229 self.assertTrue(f.numcalls > 2)
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
230
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
231 def test_fieldstorage_multipart(self):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
232 #Test basic FieldStorage multipart parsing
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
233 env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'}
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
234 postdata = """-----------------------------721837373350705526688164684
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
235 Content-Disposition: form-data; name="id"
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
236
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
237 1234
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
238 -----------------------------721837373350705526688164684
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
239 Content-Disposition: form-data; name="title"
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
240
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
241
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
242 -----------------------------721837373350705526688164684
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
243 Content-Disposition: form-data; name="file"; filename="test.txt"
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
244 Content-Type: text/plain
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
245
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
246 Testing 123.
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
247
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
248 -----------------------------721837373350705526688164684
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
249 Content-Disposition: form-data; name="submit"
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
250
39433
2aca4f040012 Whitespace normalization broke test_cgi, because a line
Tim Peters <tim.peters@gmail.com>
parents: 39432
diff changeset
251 Add\x20
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
252 -----------------------------721837373350705526688164684--
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
253 """
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
254 fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
255 self.assertEquals(len(fs.list), 4)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
256 expect = [{'name':'id', 'filename':None, 'value':'1234'},
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
257 {'name':'title', 'filename':None, 'value':''},
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
258 {'name':'file', 'filename':'test.txt','value':'Testing 123.\n'},
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
259 {'name':'submit', 'filename':None, 'value':' Add '}]
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
260 for x in range(len(fs.list)):
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
261 for k, exp in expect[x].items():
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
262 got = getattr(fs.list[x], k)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
263 self.assertEquals(got, exp)
39427
9f08516a4d0c Chris McDonough's patch to defend against certain DoS attacks on FieldStorage.
Guido van Rossum <guido@python.org>
parents: 32919
diff changeset
264
47798
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
265 _qs_result = {
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
266 'key1': 'value1',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
267 'key2': ['value2x', 'value2y'],
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
268 'key3': 'value3',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
269 'key4': 'value4'
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
270 }
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
271 def testQSAndUrlEncode(self):
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
272 data = "key2=value2x&key3=value3&key4=value4"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
273 environ = {
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
274 'CONTENT_LENGTH': str(len(data)),
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
275 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
276 'QUERY_STRING': 'key1=value1&key2=value2y',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
277 'REQUEST_METHOD': 'POST',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
278 }
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
279 v = gen_result(data, environ)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
280 self.assertEqual(self._qs_result, v)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
281
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
282 def testQSAndFormData(self):
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
283 data = """
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
284 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
285 Content-Disposition: form-data; name="key2"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
286
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
287 value2y
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
288 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
289 Content-Disposition: form-data; name="key3"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
290
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
291 value3
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
292 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
293 Content-Disposition: form-data; name="key4"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
294
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
295 value4
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
296 ---123--
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
297 """
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
298 environ = {
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
299 'CONTENT_LENGTH': str(len(data)),
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
300 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
301 'QUERY_STRING': 'key1=value1&key2=value2x',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
302 'REQUEST_METHOD': 'POST',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
303 }
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
304 v = gen_result(data, environ)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
305 self.assertEqual(self._qs_result, v)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
306
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
307 def testQSAndFormDataFile(self):
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
308 data = """
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
309 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
310 Content-Disposition: form-data; name="key2"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
311
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
312 value2y
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
313 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
314 Content-Disposition: form-data; name="key3"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
315
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
316 value3
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
317 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
318 Content-Disposition: form-data; name="key4"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
319
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
320 value4
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
321 ---123
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
322 Content-Disposition: form-data; name="upload"; filename="fake.txt"
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
323 Content-Type: text/plain
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
324
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
325 this is the content of the fake file
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
326
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
327 ---123--
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
328 """
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
329 environ = {
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
330 'CONTENT_LENGTH': str(len(data)),
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
331 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
332 'QUERY_STRING': 'key1=value1&key2=value2x',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
333 'REQUEST_METHOD': 'POST',
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
334 }
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
335 result = self._qs_result.copy()
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
336 result.update({
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
337 'upload': 'this is the content of the fake file\n'
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
338 })
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
339 v = gen_result(data, environ)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
340 self.assertEqual(result, v)
5e8a7f9d9523 Now a from submitted via POST that also has a query string
Facundo Batista <facundobatista@gmail.com>
parents: 40264
diff changeset
341
49080
7b1f8ede9b53 Issue 3801. Fixing a dumb error in the deprecated parse_qsl()
Facundo Batista <facundobatista@gmail.com>
parents: 48995
diff changeset
342 def test_deprecated_parse_qs(self):
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
343 with check_warnings():
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
344 # this func is moved to urlparse, this is just a sanity check
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
345 self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']},
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
346 cgi.parse_qs('a=A1&b=B2&B=B3'))
49080
7b1f8ede9b53 Issue 3801. Fixing a dumb error in the deprecated parse_qsl()
Facundo Batista <facundobatista@gmail.com>
parents: 48995
diff changeset
347
7b1f8ede9b53 Issue 3801. Fixing a dumb error in the deprecated parse_qsl()
Facundo Batista <facundobatista@gmail.com>
parents: 48995
diff changeset
348 def test_deprecated_parse_qsl(self):
58605
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
349 with check_warnings():
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
350 # this func is moved to urlparse, this is just a sanity check
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
351 self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
f0bc6d8aa21e #7092: silence more -3 and -Wd warnings
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
352 cgi.parse_qsl('a=A1&b=B2&B=B3'))
49080
7b1f8ede9b53 Issue 3801. Fixing a dumb error in the deprecated parse_qsl()
Facundo Batista <facundobatista@gmail.com>
parents: 48995
diff changeset
353
49974
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
354 def test_parse_header(self):
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
355 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
356 cgi.parse_header("text/plain"),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
357 ("text/plain", {}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
358 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
359 cgi.parse_header("text/vnd.just.made.this.up ; "),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
360 ("text/vnd.just.made.this.up", {}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
361 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
362 cgi.parse_header("text/plain;charset=us-ascii"),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
363 ("text/plain", {"charset": "us-ascii"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
364 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
365 cgi.parse_header('text/plain ; charset="us-ascii"'),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
366 ("text/plain", {"charset": "us-ascii"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
367 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
368 cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
369 ("text/plain", {"charset": "us-ascii", "another": "opt"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
370 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
371 cgi.parse_header('attachment; filename="silly.txt"'),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
372 ("attachment", {"filename": "silly.txt"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
373 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
374 cgi.parse_header('attachment; filename="strange;name"'),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
375 ("attachment", {"filename": "strange;name"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
376 self.assertEqual(
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
377 cgi.parse_header('attachment; filename="strange;name";size=123;'),
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
378 ("attachment", {"filename": "strange;name", "size": "123"}))
74bfeb364a23 Merged revisions 67528 via svnmerge from
Fred Drake <fdrake@acm.org>
parents: 49080
diff changeset
379
49080
7b1f8ede9b53 Issue 3801. Fixing a dumb error in the deprecated parse_qsl()
Facundo Batista <facundobatista@gmail.com>
parents: 48995
diff changeset
380
40264
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
381 def test_main():
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
382 run_unittest(CgiTests)
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
383
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
384 if __name__ == '__main__':
ca3d1d3c4d8c Convert test_cgi to unittest.
Georg Brandl <georg@python.org>
parents: 39433
diff changeset
385 test_main()