Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 1 | from test.support import run_unittest, check_warnings |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 2 | import cgi |
| 3 | import os |
| 4 | import sys |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 5 | import tempfile |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6 | import unittest |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 7 | from io import StringIO, BytesIO |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 8 | |
| 9 | class HackedSysModule: |
| 10 | # The regression test will have real values in sys.argv, which |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 11 | # will completely confuse the test of the cgi module |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 12 | argv = [] |
| 13 | stdin = sys.stdin |
| 14 | |
| 15 | cgi.sys = HackedSysModule() |
| 16 | |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 17 | class ComparableException: |
| 18 | def __init__(self, err): |
| 19 | self.err = err |
| 20 | |
| 21 | def __str__(self): |
| 22 | return str(self.err) |
| 23 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 24 | def __eq__(self, anExc): |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 25 | if not isinstance(anExc, Exception): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 26 | return NotImplemented |
| 27 | return (self.err.__class__ == anExc.__class__ and |
| 28 | self.err.args == anExc.args) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 29 | |
| 30 | def __getattr__(self, attr): |
Guido van Rossum | 846d6db | 2001-01-17 15:08:37 +0000 | [diff] [blame] | 31 | return getattr(self.err, attr) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 32 | |
| 33 | def do_test(buf, method): |
| 34 | env = {} |
| 35 | if method == "GET": |
| 36 | fp = None |
| 37 | env['REQUEST_METHOD'] = 'GET' |
| 38 | env['QUERY_STRING'] = buf |
| 39 | elif method == "POST": |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 40 | fp = BytesIO(buf.encode('latin-1')) # FieldStorage expects bytes |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 41 | env['REQUEST_METHOD'] = 'POST' |
| 42 | env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' |
| 43 | env['CONTENT_LENGTH'] = str(len(buf)) |
| 44 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 45 | raise ValueError("unknown method: %s" % method) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 46 | try: |
| 47 | return cgi.parse(fp, env, strict_parsing=1) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 48 | except Exception as err: |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 49 | return ComparableException(err) |
| 50 | |
Neil Schemenauer | 66edb62 | 2004-07-19 15:38:11 +0000 | [diff] [blame] | 51 | parse_strict_test_cases = [ |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 52 | ("", ValueError("bad query field: ''")), |
| 53 | ("&", ValueError("bad query field: ''")), |
| 54 | ("&&", ValueError("bad query field: ''")), |
Jeremy Hylton | afde7e2 | 2000-09-15 20:06:57 +0000 | [diff] [blame] | 55 | (";", ValueError("bad query field: ''")), |
| 56 | (";&;", ValueError("bad query field: ''")), |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 57 | # Should the next few really be valid? |
| 58 | ("=", {}), |
| 59 | ("=&=", {}), |
Jeremy Hylton | afde7e2 | 2000-09-15 20:06:57 +0000 | [diff] [blame] | 60 | ("=;=", {}), |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 61 | # This rest seem to make sense |
| 62 | ("=a", {'': ['a']}), |
| 63 | ("&=a", ValueError("bad query field: ''")), |
| 64 | ("=a&", ValueError("bad query field: ''")), |
| 65 | ("=&a", ValueError("bad query field: 'a'")), |
| 66 | ("b=a", {'b': ['a']}), |
| 67 | ("b+=a", {'b ': ['a']}), |
| 68 | ("a=b=a", {'a': ['b=a']}), |
| 69 | ("a=+b=a", {'a': [' b=a']}), |
| 70 | ("&b=a", ValueError("bad query field: ''")), |
| 71 | ("b&=a", ValueError("bad query field: 'b'")), |
| 72 | ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}), |
| 73 | ("a=a+b&a=b+a", {'a': ['a b', 'b a']}), |
| 74 | ("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), |
Jeremy Hylton | afde7e2 | 2000-09-15 20:06:57 +0000 | [diff] [blame] | 75 | ("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), |
| 76 | ("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 77 | ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env", |
| 78 | {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'], |
| 79 | 'cuyer': ['r'], |
| 80 | 'expire': ['964546263'], |
| 81 | 'kid': ['130003.300038'], |
| 82 | 'lobale': ['en-US'], |
| 83 | 'order_id': ['0bb2e248638833d48cb7fed300000f1b'], |
| 84 | 'ss': ['env'], |
| 85 | 'view': ['bustomer'], |
| 86 | }), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 87 | |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 88 | ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse", |
| 89 | {'SUBMIT': ['Browse'], |
| 90 | '_assigned_to': ['31392'], |
| 91 | '_category': ['100'], |
| 92 | '_status': ['1'], |
| 93 | 'group_id': ['5470'], |
| 94 | 'set': ['custom'], |
| 95 | }) |
| 96 | ] |
| 97 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 98 | def norm(seq): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 99 | return sorted(seq, key=repr) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 100 | |
| 101 | def first_elts(list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 102 | return [p[0] for p in list] |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 103 | |
| 104 | def first_second_elts(list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 105 | return [(p[0], p[1][0]) for p in list] |
| 106 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 107 | def gen_result(data, environ): |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 108 | encoding = 'latin-1' |
| 109 | fake_stdin = BytesIO(data.encode(encoding)) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 110 | fake_stdin.seek(0) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 111 | form = cgi.FieldStorage(fp=fake_stdin, environ=environ, encoding=encoding) |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 112 | |
| 113 | result = {} |
| 114 | for k, v in dict(form).items(): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 115 | result[k] = isinstance(v, list) and form.getlist(k) or v.value |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 116 | |
| 117 | return result |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 118 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | class CgiTests(unittest.TestCase): |
Neil Schemenauer | 66edb62 | 2004-07-19 15:38:11 +0000 | [diff] [blame] | 120 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | def test_strict(self): |
| 122 | for orig, expect in parse_strict_test_cases: |
| 123 | # Test basic parsing |
| 124 | d = do_test(orig, "GET") |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 125 | self.assertEqual(d, expect, "Error parsing %s method GET" % repr(orig)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | d = do_test(orig, "POST") |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 127 | self.assertEqual(d, expect, "Error parsing %s method POST" % repr(orig)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 128 | |
| 129 | env = {'QUERY_STRING': orig} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | fs = cgi.FieldStorage(environ=env) |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 131 | if isinstance(expect, dict): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 132 | # test dict interface |
Georg Brandl | 49d1b4f | 2008-05-11 21:42:51 +0000 | [diff] [blame] | 133 | self.assertEqual(len(expect), len(fs)) |
Ezio Melotti | 263cbdf | 2010-11-29 02:02:10 +0000 | [diff] [blame] | 134 | self.assertCountEqual(expect.keys(), fs.keys()) |
Georg Brandl | 49d1b4f | 2008-05-11 21:42:51 +0000 | [diff] [blame] | 135 | ##self.assertEqual(norm(expect.values()), norm(fs.values())) |
| 136 | ##self.assertEqual(norm(expect.items()), norm(fs.items())) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | self.assertEqual(fs.getvalue("nonexistent field", "default"), "default") |
| 138 | # test individual fields |
| 139 | for key in expect.keys(): |
| 140 | expect_val = expect[key] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 141 | self.assertIn(key, fs) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | if len(expect_val) > 1: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | self.assertEqual(fs.getvalue(key), expect_val) |
| 144 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 145 | self.assertEqual(fs.getvalue(key), expect_val[0]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 146 | |
| 147 | def test_log(self): |
| 148 | cgi.log("Testing") |
| 149 | |
| 150 | cgi.logfp = StringIO() |
| 151 | cgi.initlog("%s", "Testing initlog 1") |
| 152 | cgi.log("%s", "Testing log 2") |
| 153 | self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n") |
| 154 | if os.path.exists("/dev/null"): |
| 155 | cgi.logfp = None |
| 156 | cgi.logfile = "/dev/null" |
| 157 | cgi.initlog("%s", "Testing log 3") |
Victor Stinner | d33344a | 2011-07-14 22:28:36 +0200 | [diff] [blame] | 158 | self.addCleanup(cgi.closelog) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 159 | cgi.log("Testing log 4") |
| 160 | |
| 161 | def test_fieldstorage_readline(self): |
| 162 | # FieldStorage uses readline, which has the capacity to read all |
| 163 | # contents of the input file into memory; we use readline's size argument |
| 164 | # to prevent that for files that do not contain any newlines in |
| 165 | # non-GET/HEAD requests |
| 166 | class TestReadlineFile: |
| 167 | def __init__(self, file): |
| 168 | self.file = file |
| 169 | self.numcalls = 0 |
| 170 | |
| 171 | def readline(self, size=None): |
| 172 | self.numcalls += 1 |
| 173 | if size: |
| 174 | return self.file.readline(size) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 175 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 176 | return self.file.readline() |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 177 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 178 | def __getattr__(self, name): |
| 179 | file = self.__dict__['file'] |
| 180 | a = getattr(file, name) |
| 181 | if not isinstance(a, int): |
| 182 | setattr(self, name, a) |
| 183 | return a |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 184 | |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 185 | f = TestReadlineFile(tempfile.TemporaryFile("wb+")) |
Benjamin Peterson | 807a5a1 | 2010-10-30 22:59:37 +0000 | [diff] [blame] | 186 | self.addCleanup(f.close) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 187 | f.write(b'x' * 256 * 1024) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | f.seek(0) |
| 189 | env = {'REQUEST_METHOD':'PUT'} |
| 190 | fs = cgi.FieldStorage(fp=f, environ=env) |
Benjamin Peterson | 807a5a1 | 2010-10-30 22:59:37 +0000 | [diff] [blame] | 191 | self.addCleanup(fs.file.close) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | # if we're not chunking properly, readline is only called twice |
| 193 | # (by read_binary); if we are chunking properly, it will be called 5 times |
| 194 | # as long as the chunksize is 1 << 16. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 195 | self.assertTrue(f.numcalls > 2) |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 196 | f.close() |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 197 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 198 | def test_fieldstorage_multipart(self): |
| 199 | #Test basic FieldStorage multipart parsing |
Victor Stinner | f0e293c | 2011-03-01 23:09:57 +0000 | [diff] [blame] | 200 | env = { |
| 201 | 'REQUEST_METHOD': 'POST', |
| 202 | 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), |
| 203 | 'CONTENT_LENGTH': '558'} |
| 204 | fp = BytesIO(POSTDATA.encode('latin-1')) |
| 205 | fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 206 | self.assertEqual(len(fs.list), 4) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 207 | expect = [{'name':'id', 'filename':None, 'value':'1234'}, |
| 208 | {'name':'title', 'filename':None, 'value':''}, |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 209 | {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 210 | {'name':'submit', 'filename':None, 'value':' Add '}] |
| 211 | for x in range(len(fs.list)): |
| 212 | for k, exp in expect[x].items(): |
| 213 | got = getattr(fs.list[x], k) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 214 | self.assertEqual(got, exp) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 215 | |
Victor Stinner | f0e293c | 2011-03-01 23:09:57 +0000 | [diff] [blame] | 216 | def test_fieldstorage_multipart_non_ascii(self): |
| 217 | #Test basic FieldStorage multipart parsing |
| 218 | env = {'REQUEST_METHOD':'POST', |
| 219 | 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), |
| 220 | 'CONTENT_LENGTH':'558'} |
| 221 | for encoding in ['iso-8859-1','utf-8']: |
| 222 | fp = BytesIO(POSTDATA_NON_ASCII.encode(encoding)) |
| 223 | fs = cgi.FieldStorage(fp, environ=env,encoding=encoding) |
| 224 | self.assertEqual(len(fs.list), 1) |
| 225 | expect = [{'name':'id', 'filename':None, 'value':'\xe7\xf1\x80'}] |
| 226 | for x in range(len(fs.list)): |
| 227 | for k, exp in expect[x].items(): |
| 228 | got = getattr(fs.list[x], k) |
| 229 | self.assertEqual(got, exp) |
| 230 | |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 231 | _qs_result = { |
| 232 | 'key1': 'value1', |
| 233 | 'key2': ['value2x', 'value2y'], |
| 234 | 'key3': 'value3', |
| 235 | 'key4': 'value4' |
| 236 | } |
| 237 | def testQSAndUrlEncode(self): |
| 238 | data = "key2=value2x&key3=value3&key4=value4" |
| 239 | environ = { |
| 240 | 'CONTENT_LENGTH': str(len(data)), |
| 241 | 'CONTENT_TYPE': 'application/x-www-form-urlencoded', |
| 242 | 'QUERY_STRING': 'key1=value1&key2=value2y', |
| 243 | 'REQUEST_METHOD': 'POST', |
| 244 | } |
| 245 | v = gen_result(data, environ) |
| 246 | self.assertEqual(self._qs_result, v) |
| 247 | |
| 248 | def testQSAndFormData(self): |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 249 | data = """---123 |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 250 | Content-Disposition: form-data; name="key2" |
| 251 | |
| 252 | value2y |
| 253 | ---123 |
| 254 | Content-Disposition: form-data; name="key3" |
| 255 | |
| 256 | value3 |
| 257 | ---123 |
| 258 | Content-Disposition: form-data; name="key4" |
| 259 | |
| 260 | value4 |
| 261 | ---123-- |
| 262 | """ |
| 263 | environ = { |
| 264 | 'CONTENT_LENGTH': str(len(data)), |
| 265 | 'CONTENT_TYPE': 'multipart/form-data; boundary=-123', |
| 266 | 'QUERY_STRING': 'key1=value1&key2=value2x', |
| 267 | 'REQUEST_METHOD': 'POST', |
| 268 | } |
| 269 | v = gen_result(data, environ) |
| 270 | self.assertEqual(self._qs_result, v) |
| 271 | |
| 272 | def testQSAndFormDataFile(self): |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 273 | data = """---123 |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 274 | Content-Disposition: form-data; name="key2" |
| 275 | |
| 276 | value2y |
| 277 | ---123 |
| 278 | Content-Disposition: form-data; name="key3" |
| 279 | |
| 280 | value3 |
| 281 | ---123 |
| 282 | Content-Disposition: form-data; name="key4" |
| 283 | |
| 284 | value4 |
| 285 | ---123 |
| 286 | Content-Disposition: form-data; name="upload"; filename="fake.txt" |
| 287 | Content-Type: text/plain |
| 288 | |
| 289 | this is the content of the fake file |
| 290 | |
| 291 | ---123-- |
| 292 | """ |
| 293 | environ = { |
| 294 | 'CONTENT_LENGTH': str(len(data)), |
| 295 | 'CONTENT_TYPE': 'multipart/form-data; boundary=-123', |
| 296 | 'QUERY_STRING': 'key1=value1&key2=value2x', |
| 297 | 'REQUEST_METHOD': 'POST', |
| 298 | } |
| 299 | result = self._qs_result.copy() |
| 300 | result.update({ |
Victor Stinner | 5c23b8e | 2011-01-14 13:05:21 +0000 | [diff] [blame] | 301 | 'upload': b'this is the content of the fake file\n' |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 302 | }) |
| 303 | v = gen_result(data, environ) |
| 304 | self.assertEqual(result, v) |
| 305 | |
Facundo Batista | bd18fd6 | 2008-09-08 00:23:29 +0000 | [diff] [blame] | 306 | def test_deprecated_parse_qs(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 307 | # this func is moved to urllib.parse, this is just a sanity check |
| 308 | with check_warnings(('cgi.parse_qs is deprecated, use urllib.parse.' |
| 309 | 'parse_qs instead', DeprecationWarning)): |
Facundo Batista | a27244b | 2008-09-09 02:43:19 +0000 | [diff] [blame] | 310 | self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']}, |
| 311 | cgi.parse_qs('a=A1&b=B2&B=B3')) |
Facundo Batista | bd18fd6 | 2008-09-08 00:23:29 +0000 | [diff] [blame] | 312 | |
| 313 | def test_deprecated_parse_qsl(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 314 | # this func is moved to urllib.parse, this is just a sanity check |
| 315 | with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.' |
| 316 | 'parse_qsl instead', DeprecationWarning)): |
Facundo Batista | a27244b | 2008-09-09 02:43:19 +0000 | [diff] [blame] | 317 | self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')], |
| 318 | cgi.parse_qsl('a=A1&b=B2&B=B3')) |
Facundo Batista | bd18fd6 | 2008-09-08 00:23:29 +0000 | [diff] [blame] | 319 | |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 320 | def test_parse_header(self): |
| 321 | self.assertEqual( |
| 322 | cgi.parse_header("text/plain"), |
| 323 | ("text/plain", {})) |
| 324 | self.assertEqual( |
| 325 | cgi.parse_header("text/vnd.just.made.this.up ; "), |
| 326 | ("text/vnd.just.made.this.up", {})) |
| 327 | self.assertEqual( |
| 328 | cgi.parse_header("text/plain;charset=us-ascii"), |
| 329 | ("text/plain", {"charset": "us-ascii"})) |
| 330 | self.assertEqual( |
| 331 | cgi.parse_header('text/plain ; charset="us-ascii"'), |
| 332 | ("text/plain", {"charset": "us-ascii"})) |
| 333 | self.assertEqual( |
| 334 | cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'), |
| 335 | ("text/plain", {"charset": "us-ascii", "another": "opt"})) |
| 336 | self.assertEqual( |
| 337 | cgi.parse_header('attachment; filename="silly.txt"'), |
| 338 | ("attachment", {"filename": "silly.txt"})) |
| 339 | self.assertEqual( |
| 340 | cgi.parse_header('attachment; filename="strange;name"'), |
| 341 | ("attachment", {"filename": "strange;name"})) |
| 342 | self.assertEqual( |
| 343 | cgi.parse_header('attachment; filename="strange;name";size=123;'), |
| 344 | ("attachment", {"filename": "strange;name", "size": "123"})) |
| 345 | |
Victor Stinner | f0e293c | 2011-03-01 23:09:57 +0000 | [diff] [blame] | 346 | BOUNDARY = "---------------------------721837373350705526688164684" |
| 347 | |
| 348 | POSTDATA = """-----------------------------721837373350705526688164684 |
| 349 | Content-Disposition: form-data; name="id" |
| 350 | |
| 351 | 1234 |
| 352 | -----------------------------721837373350705526688164684 |
| 353 | Content-Disposition: form-data; name="title" |
| 354 | |
| 355 | |
| 356 | -----------------------------721837373350705526688164684 |
| 357 | Content-Disposition: form-data; name="file"; filename="test.txt" |
| 358 | Content-Type: text/plain |
| 359 | |
| 360 | Testing 123. |
| 361 | |
| 362 | -----------------------------721837373350705526688164684 |
| 363 | Content-Disposition: form-data; name="submit" |
| 364 | |
| 365 | Add\x20 |
| 366 | -----------------------------721837373350705526688164684-- |
| 367 | """ |
| 368 | |
| 369 | POSTDATA_NON_ASCII = """-----------------------------721837373350705526688164684 |
| 370 | Content-Disposition: form-data; name="id" |
| 371 | |
| 372 | \xe7\xf1\x80 |
| 373 | -----------------------------721837373350705526688164684 |
| 374 | """ |
| 375 | |
Fred Drake | 9a0a65b | 2008-12-04 19:24:50 +0000 | [diff] [blame] | 376 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 377 | def test_main(): |
| 378 | run_unittest(CgiTests) |
| 379 | |
| 380 | if __name__ == '__main__': |
| 381 | test_main() |