Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test.support import run_unittest |
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 |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 7 | from io import StringIO |
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 | |
| 17 | try: |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 18 | from io import StringIO |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 19 | except ImportError: |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 20 | from io import StringIO |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 21 | |
| 22 | class ComparableException: |
| 23 | def __init__(self, err): |
| 24 | self.err = err |
| 25 | |
| 26 | def __str__(self): |
| 27 | return str(self.err) |
| 28 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 29 | def __eq__(self, anExc): |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 30 | if not isinstance(anExc, Exception): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 31 | return NotImplemented |
| 32 | return (self.err.__class__ == anExc.__class__ and |
| 33 | self.err.args == anExc.args) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 34 | |
| 35 | def __getattr__(self, attr): |
Guido van Rossum | 846d6db | 2001-01-17 15:08:37 +0000 | [diff] [blame] | 36 | return getattr(self.err, attr) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 37 | |
| 38 | def do_test(buf, method): |
| 39 | env = {} |
| 40 | if method == "GET": |
| 41 | fp = None |
| 42 | env['REQUEST_METHOD'] = 'GET' |
| 43 | env['QUERY_STRING'] = buf |
| 44 | elif method == "POST": |
| 45 | fp = StringIO(buf) |
| 46 | env['REQUEST_METHOD'] = 'POST' |
| 47 | env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' |
| 48 | env['CONTENT_LENGTH'] = str(len(buf)) |
| 49 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 50 | raise ValueError("unknown method: %s" % method) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 51 | try: |
| 52 | return cgi.parse(fp, env, strict_parsing=1) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 53 | except Exception as err: |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 54 | return ComparableException(err) |
| 55 | |
| 56 | # A list of test cases. Each test case is a a two-tuple that contains |
| 57 | # a string with the query and a dictionary with the expected result. |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 58 | |
Neil Schemenauer | 66edb62 | 2004-07-19 15:38:11 +0000 | [diff] [blame] | 59 | parse_qsl_test_cases = [ |
| 60 | ("", []), |
| 61 | ("&", []), |
| 62 | ("&&", []), |
| 63 | ("=", [('', '')]), |
| 64 | ("=a", [('', 'a')]), |
| 65 | ("a", [('a', '')]), |
| 66 | ("a=", [('a', '')]), |
| 67 | ("a=", [('a', '')]), |
| 68 | ("&a=b", [('a', 'b')]), |
| 69 | ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]), |
| 70 | ("a=1&a=2", [('a', '1'), ('a', '2')]), |
| 71 | ] |
| 72 | |
| 73 | parse_strict_test_cases = [ |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 74 | ("", ValueError("bad query field: ''")), |
| 75 | ("&", ValueError("bad query field: ''")), |
| 76 | ("&&", ValueError("bad query field: ''")), |
Jeremy Hylton | afde7e2 | 2000-09-15 20:06:57 +0000 | [diff] [blame] | 77 | (";", ValueError("bad query field: ''")), |
| 78 | (";&;", ValueError("bad query field: ''")), |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 79 | # Should the next few really be valid? |
| 80 | ("=", {}), |
| 81 | ("=&=", {}), |
Jeremy Hylton | afde7e2 | 2000-09-15 20:06:57 +0000 | [diff] [blame] | 82 | ("=;=", {}), |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 83 | # This rest seem to make sense |
| 84 | ("=a", {'': ['a']}), |
| 85 | ("&=a", ValueError("bad query field: ''")), |
| 86 | ("=a&", ValueError("bad query field: ''")), |
| 87 | ("=&a", ValueError("bad query field: 'a'")), |
| 88 | ("b=a", {'b': ['a']}), |
| 89 | ("b+=a", {'b ': ['a']}), |
| 90 | ("a=b=a", {'a': ['b=a']}), |
| 91 | ("a=+b=a", {'a': [' b=a']}), |
| 92 | ("&b=a", ValueError("bad query field: ''")), |
| 93 | ("b&=a", ValueError("bad query field: 'b'")), |
| 94 | ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}), |
| 95 | ("a=a+b&a=b+a", {'a': ['a b', 'b a']}), |
| 96 | ("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] | 97 | ("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), |
| 98 | ("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] | 99 | ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env", |
| 100 | {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'], |
| 101 | 'cuyer': ['r'], |
| 102 | 'expire': ['964546263'], |
| 103 | 'kid': ['130003.300038'], |
| 104 | 'lobale': ['en-US'], |
| 105 | 'order_id': ['0bb2e248638833d48cb7fed300000f1b'], |
| 106 | 'ss': ['env'], |
| 107 | 'view': ['bustomer'], |
| 108 | }), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 109 | |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 110 | ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse", |
| 111 | {'SUBMIT': ['Browse'], |
| 112 | '_assigned_to': ['31392'], |
| 113 | '_category': ['100'], |
| 114 | '_status': ['1'], |
| 115 | 'group_id': ['5470'], |
| 116 | 'set': ['custom'], |
| 117 | }) |
| 118 | ] |
| 119 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 120 | def norm(seq): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 121 | return sorted(seq, key=repr) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 122 | |
| 123 | def first_elts(list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 124 | return [p[0] for p in list] |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 125 | |
| 126 | def first_second_elts(list): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 127 | return [(p[0], p[1][0]) for p in list] |
| 128 | |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 129 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | class CgiTests(unittest.TestCase): |
Neil Schemenauer | 66edb62 | 2004-07-19 15:38:11 +0000 | [diff] [blame] | 131 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 132 | def test_qsl(self): |
| 133 | for orig, expect in parse_qsl_test_cases: |
| 134 | result = cgi.parse_qsl(orig, keep_blank_values=True) |
| 135 | self.assertEqual(result, expect, "Error parsing %s" % repr(orig)) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 136 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | def test_strict(self): |
| 138 | for orig, expect in parse_strict_test_cases: |
| 139 | # Test basic parsing |
| 140 | d = do_test(orig, "GET") |
| 141 | self.assertEqual(d, expect, "Error parsing %s" % repr(orig)) |
| 142 | d = do_test(orig, "POST") |
| 143 | self.assertEqual(d, expect, "Error parsing %s" % repr(orig)) |
| 144 | |
| 145 | env = {'QUERY_STRING': orig} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 146 | fs = cgi.FieldStorage(environ=env) |
| 147 | if type(expect) == type({}): |
| 148 | # test dict interface |
Georg Brandl | 49d1b4f | 2008-05-11 21:42:51 +0000 | [diff] [blame] | 149 | self.assertEqual(len(expect), len(fs)) |
| 150 | self.assertEqual(norm(expect.keys()), norm(fs.keys())) |
| 151 | ##self.assertEqual(norm(expect.values()), norm(fs.values())) |
| 152 | ##self.assertEqual(norm(expect.items()), norm(fs.items())) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 153 | self.assertEqual(fs.getvalue("nonexistent field", "default"), "default") |
| 154 | # test individual fields |
| 155 | for key in expect.keys(): |
| 156 | expect_val = expect[key] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | self.assert_(key in fs) |
| 158 | if len(expect_val) > 1: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 159 | self.assertEqual(fs.getvalue(key), expect_val) |
| 160 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 161 | self.assertEqual(fs.getvalue(key), expect_val[0]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 162 | |
| 163 | def test_log(self): |
| 164 | cgi.log("Testing") |
| 165 | |
| 166 | cgi.logfp = StringIO() |
| 167 | cgi.initlog("%s", "Testing initlog 1") |
| 168 | cgi.log("%s", "Testing log 2") |
| 169 | self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n") |
| 170 | if os.path.exists("/dev/null"): |
| 171 | cgi.logfp = None |
| 172 | cgi.logfile = "/dev/null" |
| 173 | cgi.initlog("%s", "Testing log 3") |
| 174 | cgi.log("Testing log 4") |
| 175 | |
| 176 | def test_fieldstorage_readline(self): |
| 177 | # FieldStorage uses readline, which has the capacity to read all |
| 178 | # contents of the input file into memory; we use readline's size argument |
| 179 | # to prevent that for files that do not contain any newlines in |
| 180 | # non-GET/HEAD requests |
| 181 | class TestReadlineFile: |
| 182 | def __init__(self, file): |
| 183 | self.file = file |
| 184 | self.numcalls = 0 |
| 185 | |
| 186 | def readline(self, size=None): |
| 187 | self.numcalls += 1 |
| 188 | if size: |
| 189 | return self.file.readline(size) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 190 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 191 | return self.file.readline() |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 192 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 193 | def __getattr__(self, name): |
| 194 | file = self.__dict__['file'] |
| 195 | a = getattr(file, name) |
| 196 | if not isinstance(a, int): |
| 197 | setattr(self, name, a) |
| 198 | return a |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | a1a6852 | 2007-08-28 03:11:34 +0000 | [diff] [blame] | 200 | f = TestReadlineFile(tempfile.TemporaryFile("w+")) |
| 201 | f.write('x' * 256 * 1024) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 202 | f.seek(0) |
| 203 | env = {'REQUEST_METHOD':'PUT'} |
| 204 | fs = cgi.FieldStorage(fp=f, environ=env) |
| 205 | # if we're not chunking properly, readline is only called twice |
| 206 | # (by read_binary); if we are chunking properly, it will be called 5 times |
| 207 | # as long as the chunksize is 1 << 16. |
| 208 | self.assert_(f.numcalls > 2) |
Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame] | 209 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 210 | def test_fieldstorage_multipart(self): |
| 211 | #Test basic FieldStorage multipart parsing |
| 212 | env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'} |
| 213 | postdata = """-----------------------------721837373350705526688164684 |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 214 | Content-Disposition: form-data; name="id" |
| 215 | |
| 216 | 1234 |
| 217 | -----------------------------721837373350705526688164684 |
| 218 | Content-Disposition: form-data; name="title" |
| 219 | |
| 220 | |
| 221 | -----------------------------721837373350705526688164684 |
| 222 | Content-Disposition: form-data; name="file"; filename="test.txt" |
| 223 | Content-Type: text/plain |
| 224 | |
| 225 | Testing 123. |
| 226 | |
| 227 | -----------------------------721837373350705526688164684 |
| 228 | Content-Disposition: form-data; name="submit" |
| 229 | |
| 230 | Add\x20 |
| 231 | -----------------------------721837373350705526688164684-- |
| 232 | """ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 233 | fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env) |
| 234 | self.assertEquals(len(fs.list), 4) |
| 235 | expect = [{'name':'id', 'filename':None, 'value':'1234'}, |
| 236 | {'name':'title', 'filename':None, 'value':''}, |
| 237 | {'name':'file', 'filename':'test.txt','value':'Testing 123.\n'}, |
| 238 | {'name':'submit', 'filename':None, 'value':' Add '}] |
| 239 | for x in range(len(fs.list)): |
| 240 | for k, exp in expect[x].items(): |
| 241 | got = getattr(fs.list[x], k) |
| 242 | self.assertEquals(got, exp) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 243 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 244 | def test_main(): |
| 245 | run_unittest(CgiTests) |
| 246 | |
| 247 | if __name__ == '__main__': |
| 248 | test_main() |