Back to index

python3.2  3.2.2
Classes | Public Member Functions | Public Attributes
test.test_httpservers.CGIHTTPServerTestCase Class Reference
Inheritance diagram for test.test_httpservers.CGIHTTPServerTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_httpservers.CGIHTTPServerTestCase:
Collaboration graph
[legend]

List of all members.

Classes

class  request_handler

Public Member Functions

def setUp
def tearDown
def test_url_collapse_path_split
def test_headers_and_content
def test_post
def test_invaliduri
def test_authorization
def test_no_leading_slash
def test_os_environ_is_not_altered
def request

Public Attributes

 cwd
 parent_dir
 cgi_dir
 file1_path
 file2_path
 pythonexe
 server_started
 thread
 connection

Detailed Description

Definition at line 308 of file test_httpservers.py.


Member Function Documentation

def test.test_httpservers.BaseTestCase.request (   self,
  uri,
  method = 'GET',
  body = None,
  headers = {} 
) [inherited]

Definition at line 68 of file test_httpservers.py.

00068 
00069     def request(self, uri, method='GET', body=None, headers={}):
00070         self.connection = http.client.HTTPConnection(self.HOST, self.PORT)
00071         self.connection.request(method, uri, body, headers)
00072         return self.connection.getresponse()
00073 

Here is the caller graph for this function:

Reimplemented from test.test_httpservers.BaseTestCase.

Definition at line 312 of file test_httpservers.py.

00312 
00313     def setUp(self):
00314         BaseTestCase.setUp(self)
00315         self.cwd = os.getcwd()
00316         self.parent_dir = tempfile.mkdtemp()
00317         self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
00318         os.mkdir(self.cgi_dir)
00319         self.file1_path = None
00320         self.file2_path = None
00321 
00322         # The shebang line should be pure ASCII: use symlink if possible.
00323         # See issue #7668.
00324         if support.can_symlink():
00325             self.pythonexe = os.path.join(self.parent_dir, 'python')
00326             os.symlink(sys.executable, self.pythonexe)
00327         else:
00328             self.pythonexe = sys.executable
00329 
00330         try:
00331             # The python executable path is written as the first line of the
00332             # CGI Python script. The encoding cookie cannot be used, and so the
00333             # path should be encodable to the default script encoding (utf-8)
00334             self.pythonexe.encode('utf-8')
00335         except UnicodeEncodeError:
00336             self.tearDown()
00337             raise self.skipTest(
00338                 "Python executable path is not encodable to utf-8")
00339 
00340         self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
00341         with open(self.file1_path, 'w', encoding='utf-8') as file1:
00342             file1.write(cgi_file1 % self.pythonexe)
00343         os.chmod(self.file1_path, 0o777)
00344 
00345         self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
00346         with open(self.file2_path, 'w', encoding='utf-8') as file2:
00347             file2.write(cgi_file2 % self.pythonexe)
00348         os.chmod(self.file2_path, 0o777)
00349 
00350         os.chdir(self.parent_dir)

Here is the caller graph for this function:

Reimplemented from test.test_httpservers.BaseTestCase.

Definition at line 351 of file test_httpservers.py.

00351 
00352     def tearDown(self):
00353         try:
00354             os.chdir(self.cwd)
00355             if self.pythonexe != sys.executable:
00356                 os.remove(self.pythonexe)
00357             if self.file1_path:
00358                 os.remove(self.file1_path)
00359             if self.file2_path:
00360                 os.remove(self.file2_path)
00361             os.rmdir(self.cgi_dir)
00362             os.rmdir(self.parent_dir)
00363         finally:
00364             BaseTestCase.tearDown(self)

Here is the caller graph for this function:

Definition at line 422 of file test_httpservers.py.

00422 
00423     def test_authorization(self):
00424         headers = {b'Authorization' : b'Basic ' +
00425                    base64.b64encode(b'username:pass')}
00426         res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
00427         self.assertEqual((b'Hello World\n', 'text/html', 200),
00428                 (res.read(), res.getheader('Content-type'), res.status))

Here is the call graph for this function:

Definition at line 404 of file test_httpservers.py.

00404 
00405     def test_headers_and_content(self):
00406         res = self.request('/cgi-bin/file1.py')
00407         self.assertEqual((b'Hello World\n', 'text/html', 200),
00408             (res.read(), res.getheader('Content-type'), res.status))

Here is the call graph for this function:

Definition at line 417 of file test_httpservers.py.

00417 
00418     def test_invaliduri(self):
00419         res = self.request('/cgi-bin/invalid')
00420         res.read()
00421         self.assertEqual(res.status, 404)

Here is the call graph for this function:

Definition at line 429 of file test_httpservers.py.

00429 
00430     def test_no_leading_slash(self):
00431         # http://bugs.python.org/issue2254
00432         res = self.request('cgi-bin/file1.py')
00433         self.assertEqual((b'Hello World\n', 'text/html', 200),
00434              (res.read(), res.getheader('Content-type'), res.status))

Here is the call graph for this function:

Definition at line 435 of file test_httpservers.py.

00435 
00436     def test_os_environ_is_not_altered(self):
00437         signature = "Test CGI Server"
00438         os.environ['SERVER_SOFTWARE'] = signature
00439         res = self.request('/cgi-bin/file1.py')
00440         self.assertEqual((b'Hello World\n', 'text/html', 200),
00441                 (res.read(), res.getheader('Content-type'), res.status))
00442         self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
00443 

Here is the call graph for this function:

Definition at line 409 of file test_httpservers.py.

00409 
00410     def test_post(self):
00411         params = urllib.parse.urlencode(
00412             {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
00413         headers = {'Content-type' : 'application/x-www-form-urlencoded'}
00414         res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
00415 
00416         self.assertEqual(res.read(), b'1, python, 123456\n')

Here is the call graph for this function:

Definition at line 365 of file test_httpservers.py.

00365 
00366     def test_url_collapse_path_split(self):
00367         test_vectors = {
00368             '': ('/', ''),
00369             '..': IndexError,
00370             '/.//..': IndexError,
00371             '/': ('/', ''),
00372             '//': ('/', ''),
00373             '/\\': ('/', '\\'),
00374             '/.//': ('/', ''),
00375             'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
00376             '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
00377             'a': ('/', 'a'),
00378             '/a': ('/', 'a'),
00379             '//a': ('/', 'a'),
00380             './a': ('/', 'a'),
00381             './C:/': ('/C:', ''),
00382             '/a/b': ('/a', 'b'),
00383             '/a/b/': ('/a/b', ''),
00384             '/a/b/c/..': ('/a/b', ''),
00385             '/a/b/c/../d': ('/a/b', 'd'),
00386             '/a/b/c/../d/e/../f': ('/a/b/d', 'f'),
00387             '/a/b/c/../d/e/../../f': ('/a/b', 'f'),
00388             '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'),
00389             '../a/b/c/../d/e/.././././..//f': IndexError,
00390             '/a/b/c/../d/e/../../../f': ('/a', 'f'),
00391             '/a/b/c/../d/e/../../../../f': ('/', 'f'),
00392             '/a/b/c/../d/e/../../../../../f': IndexError,
00393             '/a/b/c/../d/e/../../../../f/..': ('/', ''),
00394         }
00395         for path, expected in test_vectors.items():
00396             if isinstance(expected, type) and issubclass(expected, Exception):
00397                 self.assertRaises(expected,
00398                                   server._url_collapse_path_split, path)
00399             else:
00400                 actual = server._url_collapse_path_split(path)
00401                 self.assertEqual(expected, actual,
00402                                  msg='path = %r\nGot:    %r\nWanted: %r' %
00403                                  (path, actual, expected))

Here is the call graph for this function:


Member Data Documentation

Definition at line 316 of file test_httpservers.py.

Definition at line 69 of file test_httpservers.py.

Definition at line 314 of file test_httpservers.py.

Definition at line 318 of file test_httpservers.py.

Definition at line 319 of file test_httpservers.py.

Definition at line 315 of file test_httpservers.py.

Definition at line 324 of file test_httpservers.py.

Definition at line 58 of file test_httpservers.py.

Definition at line 59 of file test_httpservers.py.


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