Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Private Attributes
test.test_cgi.CgiTests Class Reference

List of all members.

Public Member Functions

def test_strict
def test_log
def test_fieldstorage_readline
def test_fieldstorage_multipart
def test_fieldstorage_multipart_non_ascii
def testQSAndUrlEncode
def testQSAndFormData
def testQSAndFormDataFile
def test_deprecated_parse_qs
def test_deprecated_parse_qsl
def test_parse_header

Public Attributes

 file
 numcalls

Static Private Attributes

dictionary _qs_result

Detailed Description

Definition at line 119 of file test_cgi.py.


Member Function Documentation

Definition at line 312 of file test_cgi.py.

00312 
00313     def test_deprecated_parse_qs(self):
00314         # this func is moved to urllib.parse, this is just a sanity check
00315         with check_warnings(('cgi.parse_qs is deprecated, use urllib.parse.'
00316                              'parse_qs instead', DeprecationWarning)):
00317             self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']},
00318                              cgi.parse_qs('a=A1&b=B2&B=B3'))

Here is the call graph for this function:

Definition at line 319 of file test_cgi.py.

00319 
00320     def test_deprecated_parse_qsl(self):
00321         # this func is moved to urllib.parse, this is just a sanity check
00322         with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.'
00323                              'parse_qsl instead', DeprecationWarning)):
00324             self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
00325                              cgi.parse_qsl('a=A1&b=B2&B=B3'))

Here is the call graph for this function:

Definition at line 204 of file test_cgi.py.

00204 
00205     def test_fieldstorage_multipart(self):
00206         #Test basic FieldStorage multipart parsing
00207         env = {
00208             'REQUEST_METHOD': 'POST',
00209             'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY),
00210             'CONTENT_LENGTH': '558'}
00211         fp = BytesIO(POSTDATA.encode('latin-1'))
00212         fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1")
00213         self.assertEqual(len(fs.list), 4)
00214         expect = [{'name':'id', 'filename':None, 'value':'1234'},
00215                   {'name':'title', 'filename':None, 'value':''},
00216                   {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'},
00217                   {'name':'submit', 'filename':None, 'value':' Add '}]
00218         for x in range(len(fs.list)):
00219             for k, exp in expect[x].items():
00220                 got = getattr(fs.list[x], k)
00221                 self.assertEqual(got, exp)

Here is the call graph for this function:

Definition at line 222 of file test_cgi.py.

00222 
00223     def test_fieldstorage_multipart_non_ascii(self):
00224         #Test basic FieldStorage multipart parsing
00225         env = {'REQUEST_METHOD':'POST',
00226             'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY),
00227             'CONTENT_LENGTH':'558'}
00228         for encoding in ['iso-8859-1','utf-8']:
00229             fp = BytesIO(POSTDATA_NON_ASCII.encode(encoding))
00230             fs = cgi.FieldStorage(fp, environ=env,encoding=encoding)
00231             self.assertEqual(len(fs.list), 1)
00232             expect = [{'name':'id', 'filename':None, 'value':'\xe7\xf1\x80'}]
00233             for x in range(len(fs.list)):
00234                 for k, exp in expect[x].items():
00235                     got = getattr(fs.list[x], k)
00236                     self.assertEqual(got, exp)

Here is the call graph for this function:

Definition at line 167 of file test_cgi.py.

00167 
00168     def test_fieldstorage_readline(self):
00169         # FieldStorage uses readline, which has the capacity to read all
00170         # contents of the input file into memory; we use readline's size argument
00171         # to prevent that for files that do not contain any newlines in
00172         # non-GET/HEAD requests
00173         class TestReadlineFile:
00174             def __init__(self, file):
00175                 self.file = file
00176                 self.numcalls = 0
00177 
00178             def readline(self, size=None):
00179                 self.numcalls += 1
00180                 if size:
00181                     return self.file.readline(size)
00182                 else:
00183                     return self.file.readline()
00184 
00185             def __getattr__(self, name):
00186                 file = self.__dict__['file']
00187                 a = getattr(file, name)
00188                 if not isinstance(a, int):
00189                     setattr(self, name, a)
00190                 return a
00191 
00192         f = TestReadlineFile(tempfile.TemporaryFile("wb+"))
00193         self.addCleanup(f.close)
00194         f.write(b'x' * 256 * 1024)
00195         f.seek(0)
00196         env = {'REQUEST_METHOD':'PUT'}
00197         fs = cgi.FieldStorage(fp=f, environ=env)
00198         self.addCleanup(fs.file.close)
00199         # if we're not chunking properly, readline is only called twice
00200         # (by read_binary); if we are chunking properly, it will be called 5 times
00201         # as long as the chunksize is 1 << 16.
00202         self.assertTrue(f.numcalls > 2)
00203         f.close()

Here is the call graph for this function:

Definition at line 147 of file test_cgi.py.

00147 
00148     def test_log(self):
00149         cgi.log("Testing")
00150 
00151         cgi.logfp = StringIO()
00152         cgi.initlog("%s", "Testing initlog 1")
00153         cgi.log("%s", "Testing log 2")
00154         self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n")
00155         if os.path.exists("/dev/null"):
00156             cgi.logfp = None
00157             cgi.logfile = "/dev/null"
00158             cgi.initlog("%s", "Testing log 3")
00159             def log_cleanup():
00160                 """Restore the global state of the log vars."""
00161                 cgi.logfile = ''
00162                 cgi.logfp.close()
00163                 cgi.logfp = None
00164                 cgi.log = cgi.initlog
00165             self.addCleanup(log_cleanup)
00166             cgi.log("Testing log 4")

Here is the call graph for this function:

Definition at line 326 of file test_cgi.py.

00326 
00327     def test_parse_header(self):
00328         self.assertEqual(
00329             cgi.parse_header("text/plain"),
00330             ("text/plain", {}))
00331         self.assertEqual(
00332             cgi.parse_header("text/vnd.just.made.this.up ; "),
00333             ("text/vnd.just.made.this.up", {}))
00334         self.assertEqual(
00335             cgi.parse_header("text/plain;charset=us-ascii"),
00336             ("text/plain", {"charset": "us-ascii"}))
00337         self.assertEqual(
00338             cgi.parse_header('text/plain ; charset="us-ascii"'),
00339             ("text/plain", {"charset": "us-ascii"}))
00340         self.assertEqual(
00341             cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
00342             ("text/plain", {"charset": "us-ascii", "another": "opt"}))
00343         self.assertEqual(
00344             cgi.parse_header('attachment; filename="silly.txt"'),
00345             ("attachment", {"filename": "silly.txt"}))
00346         self.assertEqual(
00347             cgi.parse_header('attachment; filename="strange;name"'),
00348             ("attachment", {"filename": "strange;name"}))
00349         self.assertEqual(
00350             cgi.parse_header('attachment; filename="strange;name";size=123;'),
00351             ("attachment", {"filename": "strange;name", "size": "123"}))

Here is the call graph for this function:

Definition at line 121 of file test_cgi.py.

00121 
00122     def test_strict(self):
00123         for orig, expect in parse_strict_test_cases:
00124             # Test basic parsing
00125             d = do_test(orig, "GET")
00126             self.assertEqual(d, expect, "Error parsing %s method GET" % repr(orig))
00127             d = do_test(orig, "POST")
00128             self.assertEqual(d, expect, "Error parsing %s method POST" % repr(orig))
00129 
00130             env = {'QUERY_STRING': orig}
00131             fs = cgi.FieldStorage(environ=env)
00132             if isinstance(expect, dict):
00133                 # test dict interface
00134                 self.assertEqual(len(expect), len(fs))
00135                 self.assertCountEqual(expect.keys(), fs.keys())
00136                 ##self.assertEqual(norm(expect.values()), norm(fs.values()))
00137                 ##self.assertEqual(norm(expect.items()), norm(fs.items()))
00138                 self.assertEqual(fs.getvalue("nonexistent field", "default"), "default")
00139                 # test individual fields
00140                 for key in expect.keys():
00141                     expect_val = expect[key]
00142                     self.assertIn(key, fs)
00143                     if len(expect_val) > 1:
00144                         self.assertEqual(fs.getvalue(key), expect_val)
00145                     else:
00146                         self.assertEqual(fs.getvalue(key), expect_val[0])

Here is the call graph for this function:

Definition at line 254 of file test_cgi.py.

00254 
00255     def testQSAndFormData(self):
00256         data = """---123
00257 Content-Disposition: form-data; name="key2"
00258 
00259 value2y
00260 ---123
00261 Content-Disposition: form-data; name="key3"
00262 
00263 value3
00264 ---123
00265 Content-Disposition: form-data; name="key4"
00266 
00267 value4
00268 ---123--
00269 """
00270         environ = {
00271             'CONTENT_LENGTH':   str(len(data)),
00272             'CONTENT_TYPE':     'multipart/form-data; boundary=-123',
00273             'QUERY_STRING':     'key1=value1&key2=value2x',
00274             'REQUEST_METHOD':   'POST',
00275         }
00276         v = gen_result(data, environ)
00277         self.assertEqual(self._qs_result, v)

Here is the call graph for this function:

Definition at line 278 of file test_cgi.py.

00278 
00279     def testQSAndFormDataFile(self):
00280         data = """---123
00281 Content-Disposition: form-data; name="key2"
00282 
00283 value2y
00284 ---123
00285 Content-Disposition: form-data; name="key3"
00286 
00287 value3
00288 ---123
00289 Content-Disposition: form-data; name="key4"
00290 
00291 value4
00292 ---123
00293 Content-Disposition: form-data; name="upload"; filename="fake.txt"
00294 Content-Type: text/plain
00295 
00296 this is the content of the fake file
00297 
00298 ---123--
00299 """
00300         environ = {
00301             'CONTENT_LENGTH':   str(len(data)),
00302             'CONTENT_TYPE':     'multipart/form-data; boundary=-123',
00303             'QUERY_STRING':     'key1=value1&key2=value2x',
00304             'REQUEST_METHOD':   'POST',
00305         }
00306         result = self._qs_result.copy()
00307         result.update({
00308             'upload': b'this is the content of the fake file\n'
00309         })
00310         v = gen_result(data, environ)
00311         self.assertEqual(result, v)

Here is the call graph for this function:

Definition at line 243 of file test_cgi.py.

00243 
00244     def testQSAndUrlEncode(self):
00245         data = "key2=value2x&key3=value3&key4=value4"
00246         environ = {
00247             'CONTENT_LENGTH':   str(len(data)),
00248             'CONTENT_TYPE':     'application/x-www-form-urlencoded',
00249             'QUERY_STRING':     'key1=value1&key2=value2y',
00250             'REQUEST_METHOD':   'POST',
00251         }
00252         v = gen_result(data, environ)
00253         self.assertEqual(self._qs_result, v)

Here is the call graph for this function:


Member Data Documentation

Initial value:
{
        'key1': 'value1',
        'key2': ['value2x', 'value2y'],
        'key3': 'value3',
        'key4': 'value4'
    }

Definition at line 237 of file test_cgi.py.

Definition at line 174 of file test_cgi.py.

Definition at line 175 of file test_cgi.py.


The documentation for this class was generated from the following file: