0001 from test.test_support import verify, verbose 0002 import cgi 0003 import os 0004 import sys 0005 0006 class HackedSysModule: 0007 # The regression test will have real values in sys.argv, which 0008 # will completely confuse the test of the cgi module 0009 argv = [] 0010 stdin = sys.stdin 0011 0012 cgi.sys = HackedSysModule() 0013 0014 try: 0015 from cStringIO import StringIO 0016 except ImportError: 0017 from StringIO import StringIO 0018 0019 class ComparableException: 0020 def __init__(self, err): 0021 self.err = err 0022 0023 def __str__(self): 0024 return str(self.err) 0025 0026 def __cmp__(self, anExc): 0027 if not isinstance(anExc, Exception): 0028 return -1 0029 x = cmp(self.err.__class__, anExc.__class__) 0030 if x != 0: 0031 return x 0032 return cmp(self.err.args, anExc.args) 0033 0034 def __getattr__(self, attr): 0035 return getattr(self.err, attr) 0036 0037 def do_test(buf, method): 0038 env = {} 0039 if method == "GET": 0040 fp = None 0041 env['REQUEST_METHOD'] = 'GET' 0042 env['QUERY_STRING'] = buf 0043 elif method == "POST": 0044 fp = StringIO(buf) 0045 env['REQUEST_METHOD'] = 'POST' 0046 env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' 0047 env['CONTENT_LENGTH'] = str(len(buf)) 0048 else: 0049 raise ValueError, "unknown method: %s" % method 0050 try: 0051 return cgi.parse(fp, env, strict_parsing=1) 0052 except StandardError, err: 0053 return ComparableException(err) 0054 0055 # A list of test cases. Each test case is a a two-tuple that contains 0056 # a string with the query and a dictionary with the expected result. 0057 0058 parse_qsl_test_cases = [ 0059 ("", []), 0060 ("&", []), 0061 ("&&", []), 0062 ("=", [('', '')]), 0063 ("=a", [('', 'a')]), 0064 ("a", [('a', '')]), 0065 ("a=", [('a', '')]), 0066 ("a=", [('a', '')]), 0067 ("&a=b", [('a', 'b')]), 0068 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]), 0069 ("a=1&a=2", [('a', '1'), ('a', '2')]), 0070 ] 0071 0072 parse_strict_test_cases = [ 0073 ("", ValueError("bad query field: ''")), 0074 ("&", ValueError("bad query field: ''")), 0075 ("&&", ValueError("bad query field: ''")), 0076 (";", ValueError("bad query field: ''")), 0077 (";&;", ValueError("bad query field: ''")), 0078 # Should the next few really be valid? 0079 ("=", {}), 0080 ("=&=", {}), 0081 ("=;=", {}), 0082 # This rest seem to make sense 0083 ("=a", {'': ['a']}), 0084 ("&=a", ValueError("bad query field: ''")), 0085 ("=a&", ValueError("bad query field: ''")), 0086 ("=&a", ValueError("bad query field: 'a'")), 0087 ("b=a", {'b': ['a']}), 0088 ("b+=a", {'b ': ['a']}), 0089 ("a=b=a", {'a': ['b=a']}), 0090 ("a=+b=a", {'a': [' b=a']}), 0091 ("&b=a", ValueError("bad query field: ''")), 0092 ("b&=a", ValueError("bad query field: 'b'")), 0093 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}), 0094 ("a=a+b&a=b+a", {'a': ['a b', 'b a']}), 0095 ("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), 0096 ("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), 0097 ("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}), 0098 ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env", 0099 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'], 0100 'cuyer': ['r'], 0101 'expire': ['964546263'], 0102 'kid': ['130003.300038'], 0103 'lobale': ['en-US'], 0104 'order_id': ['0bb2e248638833d48cb7fed300000f1b'], 0105 'ss': ['env'], 0106 'view': ['bustomer'], 0107 }), 0108 0109 ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse", 0110 {'SUBMIT': ['Browse'], 0111 '_assigned_to': ['31392'], 0112 '_category': ['100'], 0113 '_status': ['1'], 0114 'group_id': ['5470'], 0115 'set': ['custom'], 0116 }) 0117 ] 0118 0119 def norm(list): 0120 if type(list) == type([]): 0121 list.sort() 0122 return list 0123 0124 def first_elts(list): 0125 return map(lambda x:x[0], list) 0126 0127 def first_second_elts(list): 0128 return map(lambda p:(p[0], p[1][0]), list) 0129 0130 def main(): 0131 for orig, expect in parse_qsl_test_cases: 0132 result = cgi.parse_qsl(orig, keep_blank_values=True) 0133 print repr(orig), '=>', result 0134 verify(result == expect, "Error parsing %s" % repr(orig)) 0135 0136 for orig, expect in parse_strict_test_cases: 0137 # Test basic parsing 0138 print repr(orig) 0139 d = do_test(orig, "GET") 0140 verify(d == expect, "Error parsing %s" % repr(orig)) 0141 d = do_test(orig, "POST") 0142 verify(d == expect, "Error parsing %s" % repr(orig)) 0143 0144 env = {'QUERY_STRING': orig} 0145 fcd = cgi.FormContentDict(env) 0146 sd = cgi.SvFormContentDict(env) 0147 fs = cgi.FieldStorage(environ=env) 0148 if type(expect) == type({}): 0149 # test dict interface 0150 verify(len(expect) == len(fcd)) 0151 verify(norm(expect.keys()) == norm(fcd.keys())) 0152 verify(norm(expect.values()) == norm(fcd.values())) 0153 verify(norm(expect.items()) == norm(fcd.items())) 0154 verify(fcd.get("nonexistent field", "default") == "default") 0155 verify(len(sd) == len(fs)) 0156 verify(norm(sd.keys()) == norm(fs.keys())) 0157 verify(fs.getvalue("nonexistent field", "default") == "default") 0158 # test individual fields 0159 for key in expect.keys(): 0160 expect_val = expect[key] 0161 verify(fcd.has_key(key)) 0162 verify(norm(fcd[key]) == norm(expect[key])) 0163 verify(fcd.get(key, "default") == fcd[key]) 0164 verify(fs.has_key(key)) 0165 if len(expect_val) > 1: 0166 single_value = 0 0167 else: 0168 single_value = 1 0169 try: 0170 val = sd[key] 0171 except IndexError: 0172 verify(not single_value) 0173 verify(fs.getvalue(key) == expect_val) 0174 else: 0175 verify(single_value) 0176 verify(val == expect_val[0]) 0177 verify(fs.getvalue(key) == expect_val[0]) 0178 verify(norm(sd.getlist(key)) == norm(expect_val)) 0179 if single_value: 0180 verify(norm(sd.values()) == \ 0181 first_elts(norm(expect.values()))) 0182 verify(norm(sd.items()) == \ 0183 first_second_elts(norm(expect.items()))) 0184 0185 # Test the weird FormContentDict classes 0186 env = {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"} 0187 expect = {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'} 0188 d = cgi.InterpFormContentDict(env) 0189 for k, v in expect.items(): 0190 verify(d[k] == v) 0191 for k, v in d.items(): 0192 verify(expect[k] == v) 0193 verify(norm(expect.values()) == norm(d.values())) 0194 0195 print "Testing log" 0196 cgi.log("Testing") 0197 cgi.logfp = sys.stdout 0198 cgi.initlog("%s", "Testing initlog 1") 0199 cgi.log("%s", "Testing log 2") 0200 if os.path.exists("/dev/null"): 0201 cgi.logfp = None 0202 cgi.logfile = "/dev/null" 0203 cgi.initlog("%s", "Testing log 3") 0204 cgi.log("Testing log 4") 0205 0206 main() 0207
Generated by PyXR 0.9.4