Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | """Unittests for the various HTTPServer modules. |
| 2 | |
| 3 | Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>, |
| 4 | Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. |
| 5 | """ |
| 6 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 7 | from http.server import BaseHTTPRequestHandler, HTTPServer, \ |
| 8 | SimpleHTTPRequestHandler, CGIHTTPRequestHandler |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 9 | from http import server |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 10 | |
| 11 | import os |
| 12 | import sys |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 13 | import re |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 14 | import base64 |
| 15 | import shutil |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 16 | import urllib.parse |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 17 | import http.client |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 18 | import tempfile |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 19 | from io import BytesIO |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 20 | |
| 21 | import unittest |
| 22 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 23 | threading = support.import_module('threading') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 24 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 25 | class NoLogRequestHandler: |
| 26 | def log_message(self, *args): |
| 27 | # don't write log messages to stderr |
| 28 | pass |
| 29 | |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 30 | def read(self, n=None): |
| 31 | return '' |
| 32 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 33 | |
| 34 | class TestServerThread(threading.Thread): |
| 35 | def __init__(self, test_object, request_handler): |
| 36 | threading.Thread.__init__(self) |
| 37 | self.request_handler = request_handler |
| 38 | self.test_object = test_object |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 39 | |
| 40 | def run(self): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 41 | self.server = HTTPServer(('localhost', 0), self.request_handler) |
| 42 | self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 43 | self.test_object.server_started.set() |
| 44 | self.test_object = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 45 | try: |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 46 | self.server.serve_forever(0.05) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 47 | finally: |
| 48 | self.server.server_close() |
| 49 | |
| 50 | def stop(self): |
| 51 | self.server.shutdown() |
| 52 | |
| 53 | |
| 54 | class BaseTestCase(unittest.TestCase): |
| 55 | def setUp(self): |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 56 | self._threads = support.threading_setup() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 57 | os.environ = support.EnvironmentVarGuard() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 58 | self.server_started = threading.Event() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 59 | self.thread = TestServerThread(self, self.request_handler) |
| 60 | self.thread.start() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 61 | self.server_started.wait() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 62 | |
| 63 | def tearDown(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 64 | self.thread.stop() |
Antoine Pitrou | f727082 | 2012-09-30 01:05:30 +0200 | [diff] [blame] | 65 | self.thread = None |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 66 | os.environ.__exit__() |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 67 | support.threading_cleanup(*self._threads) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 68 | |
| 69 | def request(self, uri, method='GET', body=None, headers={}): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 70 | self.connection = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 71 | self.connection.request(method, uri, body, headers) |
| 72 | return self.connection.getresponse() |
| 73 | |
| 74 | |
| 75 | class BaseHTTPServerTestCase(BaseTestCase): |
| 76 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 77 | protocol_version = 'HTTP/1.1' |
| 78 | default_request_version = 'HTTP/1.1' |
| 79 | |
| 80 | def do_TEST(self): |
| 81 | self.send_response(204) |
| 82 | self.send_header('Content-Type', 'text/html') |
| 83 | self.send_header('Connection', 'close') |
| 84 | self.end_headers() |
| 85 | |
| 86 | def do_KEEP(self): |
| 87 | self.send_response(204) |
| 88 | self.send_header('Content-Type', 'text/html') |
| 89 | self.send_header('Connection', 'keep-alive') |
| 90 | self.end_headers() |
| 91 | |
| 92 | def do_KEYERROR(self): |
| 93 | self.send_error(999) |
| 94 | |
| 95 | def do_CUSTOM(self): |
| 96 | self.send_response(999) |
| 97 | self.send_header('Content-Type', 'text/html') |
| 98 | self.send_header('Connection', 'close') |
| 99 | self.end_headers() |
| 100 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 101 | def do_LATINONEHEADER(self): |
| 102 | self.send_response(999) |
| 103 | self.send_header('X-Special', 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 104 | self.send_header('Connection', 'close') |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 105 | self.end_headers() |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 106 | body = self.headers['x-special-incoming'].encode('utf-8') |
| 107 | self.wfile.write(body) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 108 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 109 | def setUp(self): |
| 110 | BaseTestCase.setUp(self) |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 111 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 112 | self.con.connect() |
| 113 | |
| 114 | def test_command(self): |
| 115 | self.con.request('GET', '/') |
| 116 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 117 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 118 | |
| 119 | def test_request_line_trimming(self): |
| 120 | self.con._http_vsn_str = 'HTTP/1.1\n' |
| 121 | self.con.putrequest('GET', '/') |
| 122 | self.con.endheaders() |
| 123 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 124 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 125 | |
| 126 | def test_version_bogus(self): |
| 127 | self.con._http_vsn_str = 'FUBAR' |
| 128 | self.con.putrequest('GET', '/') |
| 129 | self.con.endheaders() |
| 130 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 131 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 132 | |
| 133 | def test_version_digits(self): |
| 134 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 135 | self.con.putrequest('GET', '/') |
| 136 | self.con.endheaders() |
| 137 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 138 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 139 | |
| 140 | def test_version_none_get(self): |
| 141 | self.con._http_vsn_str = '' |
| 142 | self.con.putrequest('GET', '/') |
| 143 | self.con.endheaders() |
| 144 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 145 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 146 | |
| 147 | def test_version_none(self): |
| 148 | self.con._http_vsn_str = '' |
| 149 | self.con.putrequest('PUT', '/') |
| 150 | self.con.endheaders() |
| 151 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 152 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 153 | |
| 154 | def test_version_invalid(self): |
| 155 | self.con._http_vsn = 99 |
| 156 | self.con._http_vsn_str = 'HTTP/9.9' |
| 157 | self.con.putrequest('GET', '/') |
| 158 | self.con.endheaders() |
| 159 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 160 | self.assertEqual(res.status, 505) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 161 | |
| 162 | def test_send_blank(self): |
| 163 | self.con._http_vsn_str = '' |
| 164 | self.con.putrequest('', '') |
| 165 | self.con.endheaders() |
| 166 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 167 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 168 | |
| 169 | def test_header_close(self): |
| 170 | self.con.putrequest('GET', '/') |
| 171 | self.con.putheader('Connection', 'close') |
| 172 | self.con.endheaders() |
| 173 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 174 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 175 | |
| 176 | def test_head_keep_alive(self): |
| 177 | self.con._http_vsn_str = 'HTTP/1.1' |
| 178 | self.con.putrequest('GET', '/') |
| 179 | self.con.putheader('Connection', 'keep-alive') |
| 180 | self.con.endheaders() |
| 181 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 182 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 183 | |
| 184 | def test_handler(self): |
| 185 | self.con.request('TEST', '/') |
| 186 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 187 | self.assertEqual(res.status, 204) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 188 | |
| 189 | def test_return_header_keep_alive(self): |
| 190 | self.con.request('KEEP', '/') |
| 191 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 192 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 193 | self.con.request('TEST', '/') |
Brian Curtin | 61d0d60 | 2010-10-31 00:34:23 +0000 | [diff] [blame] | 194 | self.addCleanup(self.con.close) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 195 | |
| 196 | def test_internal_key_error(self): |
| 197 | self.con.request('KEYERROR', '/') |
| 198 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 199 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 200 | |
| 201 | def test_return_custom_status(self): |
| 202 | self.con.request('CUSTOM', '/') |
| 203 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 204 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 205 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 206 | def test_latin1_header(self): |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 207 | self.con.request('LATINONEHEADER', '/', headers={ |
| 208 | 'X-Special-Incoming': 'Ärger mit Unicode' |
| 209 | }) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 210 | res = self.con.getresponse() |
| 211 | self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 212 | self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8')) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 213 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 214 | |
| 215 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 216 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 217 | pass |
| 218 | |
| 219 | def setUp(self): |
| 220 | BaseTestCase.setUp(self) |
| 221 | self.cwd = os.getcwd() |
| 222 | basetempdir = tempfile.gettempdir() |
| 223 | os.chdir(basetempdir) |
| 224 | self.data = b'We are the knights who say Ni!' |
| 225 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 226 | self.tempdir_name = os.path.basename(self.tempdir) |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 227 | with open(os.path.join(self.tempdir, 'test'), 'wb') as temp: |
| 228 | temp.write(self.data) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 229 | |
| 230 | def tearDown(self): |
| 231 | try: |
| 232 | os.chdir(self.cwd) |
| 233 | try: |
| 234 | shutil.rmtree(self.tempdir) |
| 235 | except: |
| 236 | pass |
| 237 | finally: |
| 238 | BaseTestCase.tearDown(self) |
| 239 | |
| 240 | def check_status_and_reason(self, response, status, data=None): |
| 241 | body = response.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 242 | self.assertTrue(response) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 243 | self.assertEqual(response.status, status) |
| 244 | self.assertIsNotNone(response.reason) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 245 | if data: |
| 246 | self.assertEqual(data, body) |
| 247 | |
| 248 | def test_get(self): |
| 249 | #constructs the path relative to the root directory of the HTTPServer |
| 250 | response = self.request(self.tempdir_name + '/test') |
| 251 | self.check_status_and_reason(response, 200, data=self.data) |
Senthil Kumaran | 72c238e | 2013-09-13 00:21:18 -0700 | [diff] [blame] | 252 | # check for trailing "/" which should return 404. See Issue17324 |
| 253 | response = self.request(self.tempdir_name + '/test/') |
| 254 | self.check_status_and_reason(response, 404) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 255 | response = self.request(self.tempdir_name + '/') |
| 256 | self.check_status_and_reason(response, 200) |
| 257 | response = self.request(self.tempdir_name) |
| 258 | self.check_status_and_reason(response, 301) |
| 259 | response = self.request('/ThisDoesNotExist') |
| 260 | self.check_status_and_reason(response, 404) |
| 261 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
| 262 | self.check_status_and_reason(response, 404) |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 263 | with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as f: |
| 264 | response = self.request('/' + self.tempdir_name + '/') |
| 265 | self.check_status_and_reason(response, 200) |
Charles-François Natali | f7ed9fc | 2011-11-02 19:35:14 +0100 | [diff] [blame] | 266 | # chmod() doesn't work as expected on Windows, and filesystem |
| 267 | # permissions are ignored by root on Unix. |
| 268 | if os.name == 'posix' and os.geteuid() != 0: |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 269 | os.chmod(self.tempdir, 0) |
| 270 | response = self.request(self.tempdir_name + '/') |
| 271 | self.check_status_and_reason(response, 404) |
| 272 | os.chmod(self.tempdir, 0o755) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 273 | |
| 274 | def test_head(self): |
| 275 | response = self.request( |
| 276 | self.tempdir_name + '/test', method='HEAD') |
| 277 | self.check_status_and_reason(response, 200) |
| 278 | self.assertEqual(response.getheader('content-length'), |
| 279 | str(len(self.data))) |
| 280 | self.assertEqual(response.getheader('content-type'), |
| 281 | 'application/octet-stream') |
| 282 | |
| 283 | def test_invalid_requests(self): |
| 284 | response = self.request('/', method='FOO') |
| 285 | self.check_status_and_reason(response, 501) |
| 286 | # requests must be case sensitive,so this should fail too |
| 287 | response = self.request('/', method='get') |
| 288 | self.check_status_and_reason(response, 501) |
| 289 | response = self.request('/', method='GETs') |
| 290 | self.check_status_and_reason(response, 501) |
| 291 | |
| 292 | |
| 293 | cgi_file1 = """\ |
| 294 | #!%s |
| 295 | |
| 296 | print("Content-type: text/html") |
| 297 | print() |
| 298 | print("Hello World") |
| 299 | """ |
| 300 | |
| 301 | cgi_file2 = """\ |
| 302 | #!%s |
| 303 | import cgi |
| 304 | |
| 305 | print("Content-type: text/html") |
| 306 | print() |
| 307 | |
| 308 | form = cgi.FieldStorage() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 309 | print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 310 | form.getfirst("bacon"))) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 311 | """ |
| 312 | |
Charles-François Natali | f7ed9fc | 2011-11-02 19:35:14 +0100 | [diff] [blame] | 313 | |
| 314 | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
| 315 | "This test can't be run reliably as root (issue #13308).") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 316 | class CGIHTTPServerTestCase(BaseTestCase): |
| 317 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 318 | pass |
| 319 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 320 | linesep = os.linesep.encode('ascii') |
| 321 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 322 | def setUp(self): |
| 323 | BaseTestCase.setUp(self) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 324 | self.cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 325 | self.parent_dir = tempfile.mkdtemp() |
| 326 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
| 327 | os.mkdir(self.cgi_dir) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 328 | self.file1_path = None |
| 329 | self.file2_path = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 330 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 331 | # The shebang line should be pure ASCII: use symlink if possible. |
| 332 | # See issue #7668. |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 333 | if support.can_symlink(): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 334 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 335 | os.symlink(sys.executable, self.pythonexe) |
| 336 | else: |
| 337 | self.pythonexe = sys.executable |
| 338 | |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 339 | try: |
| 340 | # The python executable path is written as the first line of the |
| 341 | # CGI Python script. The encoding cookie cannot be used, and so the |
| 342 | # path should be encodable to the default script encoding (utf-8) |
| 343 | self.pythonexe.encode('utf-8') |
| 344 | except UnicodeEncodeError: |
| 345 | self.tearDown() |
Serhiy Storchaka | 0b4591e | 2013-02-04 15:45:00 +0200 | [diff] [blame] | 346 | self.skipTest("Python executable path is not encodable to utf-8") |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 347 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 348 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 349 | with open(self.file1_path, 'w', encoding='utf-8') as file1: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 350 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 351 | os.chmod(self.file1_path, 0o777) |
| 352 | |
| 353 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 354 | with open(self.file2_path, 'w', encoding='utf-8') as file2: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 355 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 356 | os.chmod(self.file2_path, 0o777) |
| 357 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 358 | os.chdir(self.parent_dir) |
| 359 | |
| 360 | def tearDown(self): |
| 361 | try: |
| 362 | os.chdir(self.cwd) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 363 | if self.pythonexe != sys.executable: |
| 364 | os.remove(self.pythonexe) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 365 | if self.file1_path: |
| 366 | os.remove(self.file1_path) |
| 367 | if self.file2_path: |
| 368 | os.remove(self.file2_path) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 369 | os.rmdir(self.cgi_dir) |
| 370 | os.rmdir(self.parent_dir) |
| 371 | finally: |
| 372 | BaseTestCase.tearDown(self) |
| 373 | |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 374 | def test_url_collapse_path(self): |
| 375 | # verify tail is the last portion and head is the rest on proper urls |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 376 | test_vectors = { |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 377 | '': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 378 | '..': IndexError, |
| 379 | '/.//..': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 380 | '/': '//', |
| 381 | '//': '//', |
| 382 | '/\\': '//\\', |
| 383 | '/.//': '//', |
| 384 | 'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 385 | '/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 386 | 'a': '//a', |
| 387 | '/a': '//a', |
| 388 | '//a': '//a', |
| 389 | './a': '//a', |
| 390 | './C:/': '/C:/', |
| 391 | '/a/b': '/a/b', |
| 392 | '/a/b/': '/a/b/', |
| 393 | '/a/b/.': '/a/b/', |
| 394 | '/a/b/c/..': '/a/b/', |
| 395 | '/a/b/c/../d': '/a/b/d', |
| 396 | '/a/b/c/../d/e/../f': '/a/b/d/f', |
| 397 | '/a/b/c/../d/e/../../f': '/a/b/f', |
| 398 | '/a/b/c/../d/e/.././././..//f': '/a/b/f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 399 | '../a/b/c/../d/e/.././././..//f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 400 | '/a/b/c/../d/e/../../../f': '/a/f', |
| 401 | '/a/b/c/../d/e/../../../../f': '//f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 402 | '/a/b/c/../d/e/../../../../../f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 403 | '/a/b/c/../d/e/../../../../f/..': '//', |
| 404 | '/a/b/c/../d/e/../../../../f/../.': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 405 | } |
| 406 | for path, expected in test_vectors.items(): |
| 407 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 408 | self.assertRaises(expected, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 409 | server._url_collapse_path, path) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 410 | else: |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 411 | actual = server._url_collapse_path(path) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 412 | self.assertEqual(expected, actual, |
| 413 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 414 | (path, actual, expected)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 415 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 416 | def test_headers_and_content(self): |
| 417 | res = self.request('/cgi-bin/file1.py') |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 418 | self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 419 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 420 | |
| 421 | def test_post(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 422 | params = urllib.parse.urlencode( |
| 423 | {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 424 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 425 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 426 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 427 | self.assertEqual(res.read(), b'1, python, 123456' + self.linesep) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 428 | |
| 429 | def test_invaliduri(self): |
| 430 | res = self.request('/cgi-bin/invalid') |
| 431 | res.read() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 432 | self.assertEqual(res.status, 404) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 433 | |
| 434 | def test_authorization(self): |
| 435 | headers = {b'Authorization' : b'Basic ' + |
| 436 | base64.b64encode(b'username:pass')} |
| 437 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 438 | self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 439 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 440 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 441 | def test_no_leading_slash(self): |
| 442 | # http://bugs.python.org/issue2254 |
| 443 | res = self.request('cgi-bin/file1.py') |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 444 | self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 445 | (res.read(), res.getheader('Content-type'), res.status)) |
| 446 | |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 447 | def test_os_environ_is_not_altered(self): |
| 448 | signature = "Test CGI Server" |
| 449 | os.environ['SERVER_SOFTWARE'] = signature |
| 450 | res = self.request('/cgi-bin/file1.py') |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 451 | self.assertEqual((b'Hello World' + self.linesep, 'text/html', 200), |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 452 | (res.read(), res.getheader('Content-type'), res.status)) |
| 453 | self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
| 454 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 455 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 456 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
| 457 | def __init__(self): |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 458 | self.get_called = False |
| 459 | self.protocol_version = "HTTP/1.1" |
| 460 | |
| 461 | def do_GET(self): |
| 462 | self.get_called = True |
| 463 | self.send_response(200) |
| 464 | self.send_header('Content-Type', 'text/html') |
| 465 | self.end_headers() |
| 466 | self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 467 | |
| 468 | def log_message(self, format, *args): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 469 | pass |
| 470 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 471 | class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
| 472 | def handle_expect_100(self): |
| 473 | self.send_error(417) |
| 474 | return False |
| 475 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 476 | |
| 477 | class AuditableBytesIO: |
| 478 | |
| 479 | def __init__(self): |
| 480 | self.datas = [] |
| 481 | |
| 482 | def write(self, data): |
| 483 | self.datas.append(data) |
| 484 | |
| 485 | def getData(self): |
| 486 | return b''.join(self.datas) |
| 487 | |
| 488 | @property |
| 489 | def numWrites(self): |
| 490 | return len(self.datas) |
| 491 | |
| 492 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 493 | class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
Ezio Melotti | 3b3499b | 2011-03-16 11:35:38 +0200 | [diff] [blame] | 494 | """Test the functionality of the BaseHTTPServer. |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 495 | |
| 496 | Test the support for the Expect 100-continue header. |
| 497 | """ |
| 498 | |
| 499 | HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
| 500 | |
| 501 | def setUp (self): |
| 502 | self.handler = SocketlessRequestHandler() |
| 503 | |
| 504 | def send_typical_request(self, message): |
| 505 | input = BytesIO(message) |
| 506 | output = BytesIO() |
| 507 | self.handler.rfile = input |
| 508 | self.handler.wfile = output |
| 509 | self.handler.handle_one_request() |
| 510 | output.seek(0) |
| 511 | return output.readlines() |
| 512 | |
| 513 | def verify_get_called(self): |
| 514 | self.assertTrue(self.handler.get_called) |
| 515 | |
| 516 | def verify_expected_headers(self, headers): |
| 517 | for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
| 518 | self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 519 | |
| 520 | def verify_http_server_response(self, response): |
| 521 | match = self.HTTPResponseMatch.search(response) |
| 522 | self.assertTrue(match is not None) |
| 523 | |
| 524 | def test_http_1_1(self): |
| 525 | result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
| 526 | self.verify_http_server_response(result[0]) |
| 527 | self.verify_expected_headers(result[1:-1]) |
| 528 | self.verify_get_called() |
| 529 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 530 | |
| 531 | def test_http_1_0(self): |
| 532 | result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
| 533 | self.verify_http_server_response(result[0]) |
| 534 | self.verify_expected_headers(result[1:-1]) |
| 535 | self.verify_get_called() |
| 536 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 537 | |
| 538 | def test_http_0_9(self): |
| 539 | result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
| 540 | self.assertEqual(len(result), 1) |
| 541 | self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
| 542 | self.verify_get_called() |
| 543 | |
| 544 | def test_with_continue_1_0(self): |
| 545 | result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 546 | self.verify_http_server_response(result[0]) |
| 547 | self.verify_expected_headers(result[1:-1]) |
| 548 | self.verify_get_called() |
| 549 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 550 | |
| 551 | def test_with_continue_1_1(self): |
| 552 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 553 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n') |
| 554 | self.assertEqual(result[1], b'HTTP/1.1 200 OK\r\n') |
| 555 | self.verify_expected_headers(result[2:-1]) |
| 556 | self.verify_get_called() |
| 557 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 558 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 559 | def test_header_buffering_of_send_error(self): |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 560 | |
| 561 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 562 | output = AuditableBytesIO() |
| 563 | handler = SocketlessRequestHandler() |
| 564 | handler.rfile = input |
| 565 | handler.wfile = output |
| 566 | handler.request_version = 'HTTP/1.1' |
| 567 | handler.requestline = '' |
| 568 | handler.command = None |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 569 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 570 | handler.send_error(418) |
| 571 | self.assertEqual(output.numWrites, 2) |
| 572 | |
| 573 | def test_header_buffering_of_send_response_only(self): |
| 574 | |
| 575 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 576 | output = AuditableBytesIO() |
| 577 | handler = SocketlessRequestHandler() |
| 578 | handler.rfile = input |
| 579 | handler.wfile = output |
| 580 | handler.request_version = 'HTTP/1.1' |
| 581 | |
| 582 | handler.send_response_only(418) |
| 583 | self.assertEqual(output.numWrites, 0) |
| 584 | handler.end_headers() |
| 585 | self.assertEqual(output.numWrites, 1) |
| 586 | |
| 587 | def test_header_buffering_of_send_header(self): |
| 588 | |
| 589 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 590 | output = AuditableBytesIO() |
| 591 | handler = SocketlessRequestHandler() |
| 592 | handler.rfile = input |
| 593 | handler.wfile = output |
| 594 | handler.request_version = 'HTTP/1.1' |
| 595 | |
| 596 | handler.send_header('Foo', 'foo') |
| 597 | handler.send_header('bar', 'bar') |
| 598 | self.assertEqual(output.numWrites, 0) |
| 599 | handler.end_headers() |
| 600 | self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n') |
| 601 | self.assertEqual(output.numWrites, 1) |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 602 | |
| 603 | def test_header_unbuffered_when_continue(self): |
| 604 | |
| 605 | def _readAndReseek(f): |
| 606 | pos = f.tell() |
| 607 | f.seek(0) |
| 608 | data = f.read() |
| 609 | f.seek(pos) |
| 610 | return data |
| 611 | |
| 612 | input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 613 | output = BytesIO() |
| 614 | self.handler.rfile = input |
| 615 | self.handler.wfile = output |
| 616 | self.handler.request_version = 'HTTP/1.1' |
| 617 | |
| 618 | self.handler.handle_one_request() |
| 619 | self.assertNotEqual(_readAndReseek(output), b'') |
| 620 | result = _readAndReseek(output).split(b'\r\n') |
| 621 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue') |
| 622 | self.assertEqual(result[1], b'HTTP/1.1 200 OK') |
| 623 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 624 | def test_with_continue_rejected(self): |
| 625 | usual_handler = self.handler # Save to avoid breaking any subsequent tests. |
| 626 | self.handler = RejectingSocketlessRequestHandler() |
| 627 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 628 | self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n') |
| 629 | self.verify_expected_headers(result[1:-1]) |
| 630 | # The expect handler should short circuit the usual get method by |
| 631 | # returning false here, so get_called should be false |
| 632 | self.assertFalse(self.handler.get_called) |
| 633 | self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1) |
| 634 | self.handler = usual_handler # Restore to avoid breaking any subsequent tests. |
| 635 | |
Antoine Pitrou | c492437 | 2010-12-16 16:48:36 +0000 | [diff] [blame] | 636 | def test_request_length(self): |
| 637 | # Issue #10714: huge request lines are discarded, to avoid Denial |
| 638 | # of Service attacks. |
| 639 | result = self.send_typical_request(b'GET ' + b'x' * 65537) |
| 640 | self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n') |
| 641 | self.assertFalse(self.handler.get_called) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 642 | |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 643 | def test_header_length(self): |
| 644 | # Issue #6791: same for headers |
| 645 | result = self.send_typical_request( |
| 646 | b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n') |
| 647 | self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n') |
| 648 | self.assertFalse(self.handler.get_called) |
| 649 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 650 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 651 | """ Test url parsing """ |
| 652 | def setUp(self): |
| 653 | self.translated = os.getcwd() |
| 654 | self.translated = os.path.join(self.translated, 'filename') |
| 655 | self.handler = SocketlessRequestHandler() |
| 656 | |
| 657 | def test_query_arguments(self): |
| 658 | path = self.handler.translate_path('/filename') |
| 659 | self.assertEqual(path, self.translated) |
| 660 | path = self.handler.translate_path('/filename?foo=bar') |
| 661 | self.assertEqual(path, self.translated) |
| 662 | path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
| 663 | self.assertEqual(path, self.translated) |
| 664 | |
| 665 | def test_start_with_double_slash(self): |
| 666 | path = self.handler.translate_path('//filename') |
| 667 | self.assertEqual(path, self.translated) |
| 668 | path = self.handler.translate_path('//filename?foo=bar') |
| 669 | self.assertEqual(path, self.translated) |
| 670 | |
| 671 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 672 | def test_main(verbose=None): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 673 | cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 674 | try: |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 675 | support.run_unittest( |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 676 | BaseHTTPRequestHandlerTestCase, |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 677 | BaseHTTPServerTestCase, |
| 678 | SimpleHTTPServerTestCase, |
| 679 | CGIHTTPServerTestCase, |
| 680 | SimpleHTTPRequestHandlerTestCase, |
| 681 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 682 | finally: |
| 683 | os.chdir(cwd) |
| 684 | |
| 685 | if __name__ == '__main__': |
| 686 | test_main() |