Jeremy Hylton | d9827c4 | 2000-08-03 22:11:43 +0000 | [diff] [blame^] | 1 | import cgi |
| 2 | import os |
| 3 | import sys |
| 4 | |
| 5 | class HackedSysModule: |
| 6 | # The regression test will have real values in sys.argv, which |
| 7 | # will completely confuse the test of the cgi module |
| 8 | argv = [] |
| 9 | stdin = sys.stdin |
| 10 | |
| 11 | cgi.sys = HackedSysModule() |
| 12 | |
| 13 | try: |
| 14 | from cStringIO import StringIO |
| 15 | except ImportError: |
| 16 | from StringIO import StringIO |
| 17 | |
| 18 | class ComparableException: |
| 19 | def __init__(self, err): |
| 20 | self.err = err |
| 21 | |
| 22 | def __str__(self): |
| 23 | return str(self.err) |
| 24 | |
| 25 | def __cmp__(self, anExc): |
| 26 | if not isinstance(anExc, Exception): |
| 27 | return -1 |
| 28 | x = cmp(self.err.__class__, anExc.__class__) |
| 29 | if x != 0: |
| 30 | return x |
| 31 | return cmp(self.err.args, anExc.args) |
| 32 | |
| 33 | def __getattr__(self, attr): |
| 34 | return getattr(self, self.err) |
| 35 | |
| 36 | def do_test(buf, method): |
| 37 | env = {} |
| 38 | if method == "GET": |
| 39 | fp = None |
| 40 | env['REQUEST_METHOD'] = 'GET' |
| 41 | env['QUERY_STRING'] = buf |
| 42 | elif method == "POST": |
| 43 | fp = StringIO(buf) |
| 44 | env['REQUEST_METHOD'] = 'POST' |
| 45 | env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' |
| 46 | env['CONTENT_LENGTH'] = str(len(buf)) |
| 47 | else: |
| 48 | raise ValueError, "unknown method: %s" % method |
| 49 | try: |
| 50 | return cgi.parse(fp, env, strict_parsing=1) |
| 51 | except StandardError, err: |
| 52 | return ComparableException(err) |
| 53 | |
| 54 | # A list of test cases. Each test case is a a two-tuple that contains |
| 55 | # a string with the query and a dictionary with the expected result. |
| 56 | |
| 57 | parse_test_cases = [ |
| 58 | ("", ValueError("bad query field: ''")), |
| 59 | ("&", ValueError("bad query field: ''")), |
| 60 | ("&&", ValueError("bad query field: ''")), |
| 61 | # Should the next few really be valid? |
| 62 | ("=", {}), |
| 63 | ("=&=", {}), |
| 64 | # This rest seem to make sense |
| 65 | ("=a", {'': ['a']}), |
| 66 | ("&=a", ValueError("bad query field: ''")), |
| 67 | ("=a&", ValueError("bad query field: ''")), |
| 68 | ("=&a", ValueError("bad query field: 'a'")), |
| 69 | ("b=a", {'b': ['a']}), |
| 70 | ("b+=a", {'b ': ['a']}), |
| 71 | ("a=b=a", {'a': ['b=a']}), |
| 72 | ("a=+b=a", {'a': [' b=a']}), |
| 73 | ("&b=a", ValueError("bad query field: ''")), |
| 74 | ("b&=a", ValueError("bad query field: 'b'")), |
| 75 | ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}), |
| 76 | ("a=a+b&a=b+a", {'a': ['a b', 'b a']}), |
| 77 | ("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), |
| 78 | ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env", |
| 79 | {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'], |
| 80 | 'cuyer': ['r'], |
| 81 | 'expire': ['964546263'], |
| 82 | 'kid': ['130003.300038'], |
| 83 | 'lobale': ['en-US'], |
| 84 | 'order_id': ['0bb2e248638833d48cb7fed300000f1b'], |
| 85 | 'ss': ['env'], |
| 86 | 'view': ['bustomer'], |
| 87 | }), |
| 88 | |
| 89 | ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse", |
| 90 | {'SUBMIT': ['Browse'], |
| 91 | '_assigned_to': ['31392'], |
| 92 | '_category': ['100'], |
| 93 | '_status': ['1'], |
| 94 | 'group_id': ['5470'], |
| 95 | 'set': ['custom'], |
| 96 | }) |
| 97 | ] |
| 98 | |
| 99 | def norm(list): |
| 100 | if type(list) == type([]): |
| 101 | list.sort() |
| 102 | return list |
| 103 | |
| 104 | def first_elts(list): |
| 105 | return map(lambda x:x[0], list) |
| 106 | |
| 107 | def first_second_elts(list): |
| 108 | return map(lambda p:(p[0], p[1][0]), list) |
| 109 | |
| 110 | def main(): |
| 111 | for orig, expect in parse_test_cases: |
| 112 | # Test basic parsing |
| 113 | print repr(orig) |
| 114 | d = do_test(orig, "GET") |
| 115 | assert d == expect, "Error parsing %s" % repr(orig) |
| 116 | d = do_test(orig, "POST") |
| 117 | assert d == expect, "Error parsing %s" % repr(orig) |
| 118 | |
| 119 | d = {'QUERY_STRING': orig} |
| 120 | fcd = cgi.FormContentDict(d) |
| 121 | sd = cgi.SvFormContentDict(d) |
| 122 | if type(expect) == type({}): |
| 123 | # test dict interface |
| 124 | assert len(expect) == len(fcd) |
| 125 | assert norm(expect.keys()) == norm(fcd.keys()) |
| 126 | assert norm(expect.values()) == norm(fcd.values()) |
| 127 | assert norm(expect.items()) == norm(fcd.items()) |
| 128 | for key in expect.keys(): |
| 129 | expect_val = expect[key] |
| 130 | assert fcd.has_key(key) |
| 131 | assert norm(fcd[key]) == norm(expect[key]) |
| 132 | if len(expect_val) > 1: |
| 133 | single_value = 0 |
| 134 | else: |
| 135 | single_value = 1 |
| 136 | try: |
| 137 | val = sd[key] |
| 138 | except IndexError: |
| 139 | assert not single_value |
| 140 | else: |
| 141 | assert single_value |
| 142 | assert val == expect_val[0] |
| 143 | assert norm(sd.getlist(key)) == norm(expect_val) |
| 144 | if single_value: |
| 145 | assert norm(sd.values()) == \ |
| 146 | first_elts(norm(expect.values())) |
| 147 | assert norm(sd.items()) == \ |
| 148 | first_second_elts(norm(expect.items())) |
| 149 | |
| 150 | # Test the weird FormContentDict classes |
| 151 | env = {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"} |
| 152 | expect = {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'} |
| 153 | d = cgi.InterpFormContentDict(env) |
| 154 | for k, v in expect.items(): |
| 155 | assert d[k] == v |
| 156 | for k, v in d.items(): |
| 157 | assert expect[k] == v |
| 158 | assert norm(expect.values()) == norm(d.values()) |
| 159 | |
| 160 | print "Testing log" |
| 161 | cgi.initlog() |
| 162 | cgi.log("Testing") |
| 163 | cgi.logfp = sys.stdout |
| 164 | cgi.initlog("%s", "Testing initlog 1") |
| 165 | cgi.log("%s", "Testing log 2") |
| 166 | if os.path.exists("/dev/null"): |
| 167 | cgi.logfp = None |
| 168 | cgi.logfile = "/dev/null" |
| 169 | cgi.initlog("%s", "Testing log 3") |
| 170 | cgi.log("Testing log 4") |
| 171 | |
| 172 | main() |