Mercurial > cpython-lru_cache-weakref
comparison Lib/test/test_httpservers.py @ 76247:c67efb8ffca4 2.7
Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Thu, 12 Apr 2012 02:23:23 +0800 |
| parents | 89eeaa18700f |
| children | a58b620e4dc9 |
comparison
equal
deleted
inserted
replaced
| 76225:4d49a2415ced | 76247:c67efb8ffca4 |
|---|---|
| 1 """Unittests for the various HTTPServer modules. | 1 """Unittests for the various HTTPServer modules. |
| 2 | 2 |
| 3 Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>, | 3 Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>, |
| 4 Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. | 4 Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. |
| 5 """ | 5 """ |
| 6 | |
| 7 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
| 8 from SimpleHTTPServer import SimpleHTTPRequestHandler | |
| 9 from CGIHTTPServer import CGIHTTPRequestHandler | |
| 10 import CGIHTTPServer | |
| 11 | 6 |
| 12 import os | 7 import os |
| 13 import sys | 8 import sys |
| 14 import re | 9 import re |
| 15 import base64 | 10 import base64 |
| 16 import shutil | 11 import shutil |
| 17 import urllib | 12 import urllib |
| 18 import httplib | 13 import httplib |
| 19 import tempfile | 14 import tempfile |
| 20 | |
| 21 import unittest | 15 import unittest |
| 22 | 16 import CGIHTTPServer |
| 17 | |
| 18 | |
| 19 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
| 20 from SimpleHTTPServer import SimpleHTTPRequestHandler | |
| 21 from CGIHTTPServer import CGIHTTPRequestHandler | |
| 23 from StringIO import StringIO | 22 from StringIO import StringIO |
| 24 | |
| 25 from test import test_support | 23 from test import test_support |
| 24 | |
| 25 | |
| 26 threading = test_support.import_module('threading') | 26 threading = test_support.import_module('threading') |
| 27 | 27 |
| 28 | 28 |
| 29 class NoLogRequestHandler: | 29 class NoLogRequestHandler: |
| 30 def log_message(self, *args): | 30 def log_message(self, *args): |
| 41 self.send_response(200) | 41 self.send_response(200) |
| 42 self.send_header('Content-Type', 'text/html') | 42 self.send_header('Content-Type', 'text/html') |
| 43 self.end_headers() | 43 self.end_headers() |
| 44 self.wfile.write(b'<html><body>Data</body></html>\r\n') | 44 self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 45 | 45 |
| 46 def log_message(self, format, *args): | 46 def log_message(self, fmt, *args): |
| 47 pass | 47 pass |
| 48 | 48 |
| 49 | 49 |
| 50 class TestServerThread(threading.Thread): | 50 class TestServerThread(threading.Thread): |
| 51 def __init__(self, test_object, request_handler): | 51 def __init__(self, test_object, request_handler): |
| 95 | 95 |
| 96 def setUp (self): | 96 def setUp (self): |
| 97 self.handler = SocketlessRequestHandler() | 97 self.handler = SocketlessRequestHandler() |
| 98 | 98 |
| 99 def send_typical_request(self, message): | 99 def send_typical_request(self, message): |
| 100 input = StringIO(message) | 100 input_msg = StringIO(message) |
| 101 output = StringIO() | 101 output = StringIO() |
| 102 self.handler.rfile = input | 102 self.handler.rfile = input_msg |
| 103 self.handler.wfile = output | 103 self.handler.wfile = output |
| 104 self.handler.handle_one_request() | 104 self.handler.handle_one_request() |
| 105 output.seek(0) | 105 output.seek(0) |
| 106 return output.readlines() | 106 return output.readlines() |
| 107 | 107 |
| 294 def tearDown(self): | 294 def tearDown(self): |
| 295 try: | 295 try: |
| 296 os.chdir(self.cwd) | 296 os.chdir(self.cwd) |
| 297 try: | 297 try: |
| 298 shutil.rmtree(self.tempdir) | 298 shutil.rmtree(self.tempdir) |
| 299 except: | 299 except OSError: |
| 300 pass | 300 pass |
| 301 finally: | 301 finally: |
| 302 BaseTestCase.tearDown(self) | 302 BaseTestCase.tearDown(self) |
| 303 | 303 |
| 304 def check_status_and_reason(self, response, status, data=None): | 304 def check_status_and_reason(self, response, status, data=None): |
| 416 os.rmdir(self.cgi_dir) | 416 os.rmdir(self.cgi_dir) |
| 417 os.rmdir(self.parent_dir) | 417 os.rmdir(self.parent_dir) |
| 418 finally: | 418 finally: |
| 419 BaseTestCase.tearDown(self) | 419 BaseTestCase.tearDown(self) |
| 420 | 420 |
| 421 def test_url_collapse_path_split(self): | 421 def test_url_collapse_path(self): |
| 422 # verify tail is the last portion and head is the rest on proper urls | 422 # verify tail is the last portion and head is the rest on proper urls |
| 423 test_vectors = { | 423 test_vectors = { |
| 424 '': ('/', ''), | 424 '': '//', |
| 425 '..': IndexError, | 425 '..': IndexError, |
| 426 '/.//..': IndexError, | 426 '/.//..': IndexError, |
| 427 '/': ('/', ''), | 427 '/': '//', |
| 428 '//': ('/', ''), | 428 '//': '//', |
| 429 '/\\': ('/', '\\'), | 429 '/\\': '//\\', |
| 430 '/.//': ('/', ''), | 430 '/.//': '//', |
| 431 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), | 431 'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 432 '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), | 432 '/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 433 'a': ('/', 'a'), | 433 'a': '//a', |
| 434 '/a': ('/', 'a'), | 434 '/a': '//a', |
| 435 '//a': ('/', 'a'), | 435 '//a': '//a', |
| 436 './a': ('/', 'a'), | 436 './a': '//a', |
| 437 './C:/': ('/C:', ''), | 437 './C:/': '/C:/', |
| 438 '/a/b': ('/a', 'b'), | 438 '/a/b': '/a/b', |
| 439 '/a/b/': ('/a/b', ''), | 439 '/a/b/': '/a/b/', |
| 440 '/a/b/c/..': ('/a/b', ''), | 440 '/a/b/.': '/a/b/', |
| 441 '/a/b/c/../d': ('/a/b', 'd'), | 441 '/a/b/c/..': '/a/b/', |
| 442 '/a/b/c/../d/e/../f': ('/a/b/d', 'f'), | 442 '/a/b/c/../d': '/a/b/d', |
| 443 '/a/b/c/../d/e/../../f': ('/a/b', 'f'), | 443 '/a/b/c/../d/e/../f': '/a/b/d/f', |
| 444 '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'), | 444 '/a/b/c/../d/e/../../f': '/a/b/f', |
| 445 '/a/b/c/../d/e/.././././..//f': '/a/b/f', | |
| 445 '../a/b/c/../d/e/.././././..//f': IndexError, | 446 '../a/b/c/../d/e/.././././..//f': IndexError, |
| 446 '/a/b/c/../d/e/../../../f': ('/a', 'f'), | 447 '/a/b/c/../d/e/../../../f': '/a/f', |
| 447 '/a/b/c/../d/e/../../../../f': ('/', 'f'), | 448 '/a/b/c/../d/e/../../../../f': '//f', |
| 448 '/a/b/c/../d/e/../../../../../f': IndexError, | 449 '/a/b/c/../d/e/../../../../../f': IndexError, |
| 449 '/a/b/c/../d/e/../../../../f/..': ('/', ''), | 450 '/a/b/c/../d/e/../../../../f/..': '//', |
| 451 '/a/b/c/../d/e/../../../../f/../.': '//', | |
| 450 } | 452 } |
| 451 for path, expected in test_vectors.iteritems(): | 453 for path, expected in test_vectors.iteritems(): |
| 452 if isinstance(expected, type) and issubclass(expected, Exception): | 454 if isinstance(expected, type) and issubclass(expected, Exception): |
| 453 self.assertRaises(expected, | 455 self.assertRaises(expected, |
| 454 CGIHTTPServer._url_collapse_path_split, path) | 456 CGIHTTPServer._url_collapse_path, path) |
| 455 else: | 457 else: |
| 456 actual = CGIHTTPServer._url_collapse_path_split(path) | 458 actual = CGIHTTPServer._url_collapse_path(path) |
| 457 self.assertEqual(expected, actual, | 459 self.assertEqual(expected, actual, |
| 458 msg='path = %r\nGot: %r\nWanted: %r' % | 460 msg='path = %r\nGot: %r\nWanted: %r' % |
| 459 (path, actual, expected)) | 461 (path, actual, expected)) |
| 460 | 462 |
| 461 def test_headers_and_content(self): | 463 def test_headers_and_content(self): |
