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): |
| 41 | self.server = HTTPServer(('', 0), self.request_handler) |
| 42 | self.test_object.PORT = self.server.socket.getsockname()[1] |
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() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 65 | os.environ.__exit__() |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 66 | support.threading_cleanup(*self._threads) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 67 | |
| 68 | def request(self, uri, method='GET', body=None, headers={}): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 69 | self.connection = http.client.HTTPConnection('localhost', self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 70 | self.connection.request(method, uri, body, headers) |
| 71 | return self.connection.getresponse() |
| 72 | |
| 73 | |
| 74 | class BaseHTTPServerTestCase(BaseTestCase): |
| 75 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 76 | protocol_version = 'HTTP/1.1' |
| 77 | default_request_version = 'HTTP/1.1' |
| 78 | |
| 79 | def do_TEST(self): |
| 80 | self.send_response(204) |
| 81 | self.send_header('Content-Type', 'text/html') |
| 82 | self.send_header('Connection', 'close') |
| 83 | self.end_headers() |
| 84 | |
| 85 | def do_KEEP(self): |
| 86 | self.send_response(204) |
| 87 | self.send_header('Content-Type', 'text/html') |
| 88 | self.send_header('Connection', 'keep-alive') |
| 89 | self.end_headers() |
| 90 | |
| 91 | def do_KEYERROR(self): |
| 92 | self.send_error(999) |
| 93 | |
| 94 | def do_CUSTOM(self): |
| 95 | self.send_response(999) |
| 96 | self.send_header('Content-Type', 'text/html') |
| 97 | self.send_header('Connection', 'close') |
| 98 | self.end_headers() |
| 99 | |
| 100 | def setUp(self): |
| 101 | BaseTestCase.setUp(self) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 102 | self.con = http.client.HTTPConnection('localhost', self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 103 | self.con.connect() |
| 104 | |
| 105 | def test_command(self): |
| 106 | self.con.request('GET', '/') |
| 107 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 108 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 109 | |
| 110 | def test_request_line_trimming(self): |
| 111 | self.con._http_vsn_str = 'HTTP/1.1\n' |
| 112 | self.con.putrequest('GET', '/') |
| 113 | self.con.endheaders() |
| 114 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 115 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 116 | |
| 117 | def test_version_bogus(self): |
| 118 | self.con._http_vsn_str = 'FUBAR' |
| 119 | self.con.putrequest('GET', '/') |
| 120 | self.con.endheaders() |
| 121 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 122 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 123 | |
| 124 | def test_version_digits(self): |
| 125 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 126 | self.con.putrequest('GET', '/') |
| 127 | self.con.endheaders() |
| 128 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 129 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 130 | |
| 131 | def test_version_none_get(self): |
| 132 | self.con._http_vsn_str = '' |
| 133 | self.con.putrequest('GET', '/') |
| 134 | self.con.endheaders() |
| 135 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 136 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 137 | |
| 138 | def test_version_none(self): |
| 139 | self.con._http_vsn_str = '' |
| 140 | self.con.putrequest('PUT', '/') |
| 141 | self.con.endheaders() |
| 142 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 143 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 144 | |
| 145 | def test_version_invalid(self): |
| 146 | self.con._http_vsn = 99 |
| 147 | self.con._http_vsn_str = 'HTTP/9.9' |
| 148 | self.con.putrequest('GET', '/') |
| 149 | self.con.endheaders() |
| 150 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 151 | self.assertEqual(res.status, 505) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 152 | |
| 153 | def test_send_blank(self): |
| 154 | self.con._http_vsn_str = '' |
| 155 | self.con.putrequest('', '') |
| 156 | self.con.endheaders() |
| 157 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 158 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 159 | |
| 160 | def test_header_close(self): |
| 161 | self.con.putrequest('GET', '/') |
| 162 | self.con.putheader('Connection', 'close') |
| 163 | self.con.endheaders() |
| 164 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 165 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 166 | |
| 167 | def test_head_keep_alive(self): |
| 168 | self.con._http_vsn_str = 'HTTP/1.1' |
| 169 | self.con.putrequest('GET', '/') |
| 170 | self.con.putheader('Connection', 'keep-alive') |
| 171 | self.con.endheaders() |
| 172 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 173 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 174 | |
| 175 | def test_handler(self): |
| 176 | self.con.request('TEST', '/') |
| 177 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 178 | self.assertEqual(res.status, 204) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 179 | |
| 180 | def test_return_header_keep_alive(self): |
| 181 | self.con.request('KEEP', '/') |
| 182 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 183 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 184 | self.con.request('TEST', '/') |
Brian Curtin | 61d0d60 | 2010-10-31 00:34:23 +0000 | [diff] [blame] | 185 | self.addCleanup(self.con.close) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 186 | |
| 187 | def test_internal_key_error(self): |
| 188 | self.con.request('KEYERROR', '/') |
| 189 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 190 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 191 | |
| 192 | def test_return_custom_status(self): |
| 193 | self.con.request('CUSTOM', '/') |
| 194 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 195 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 199 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 200 | pass |
| 201 | |
| 202 | def setUp(self): |
| 203 | BaseTestCase.setUp(self) |
| 204 | self.cwd = os.getcwd() |
| 205 | basetempdir = tempfile.gettempdir() |
| 206 | os.chdir(basetempdir) |
| 207 | self.data = b'We are the knights who say Ni!' |
| 208 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 209 | self.tempdir_name = os.path.basename(self.tempdir) |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 210 | with open(os.path.join(self.tempdir, 'test'), 'wb') as temp: |
| 211 | temp.write(self.data) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 212 | |
| 213 | def tearDown(self): |
| 214 | try: |
| 215 | os.chdir(self.cwd) |
| 216 | try: |
| 217 | shutil.rmtree(self.tempdir) |
| 218 | except: |
| 219 | pass |
| 220 | finally: |
| 221 | BaseTestCase.tearDown(self) |
| 222 | |
| 223 | def check_status_and_reason(self, response, status, data=None): |
| 224 | body = response.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 225 | self.assertTrue(response) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 226 | self.assertEqual(response.status, status) |
| 227 | self.assertIsNotNone(response.reason) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 228 | if data: |
| 229 | self.assertEqual(data, body) |
| 230 | |
| 231 | def test_get(self): |
| 232 | #constructs the path relative to the root directory of the HTTPServer |
| 233 | response = self.request(self.tempdir_name + '/test') |
| 234 | self.check_status_and_reason(response, 200, data=self.data) |
| 235 | response = self.request(self.tempdir_name + '/') |
| 236 | self.check_status_and_reason(response, 200) |
| 237 | response = self.request(self.tempdir_name) |
| 238 | self.check_status_and_reason(response, 301) |
| 239 | response = self.request('/ThisDoesNotExist') |
| 240 | self.check_status_and_reason(response, 404) |
| 241 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
| 242 | self.check_status_and_reason(response, 404) |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 243 | with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as f: |
| 244 | response = self.request('/' + self.tempdir_name + '/') |
| 245 | self.check_status_and_reason(response, 200) |
| 246 | if os.name == 'posix': |
| 247 | # chmod won't work as expected on Windows platforms |
| 248 | os.chmod(self.tempdir, 0) |
| 249 | response = self.request(self.tempdir_name + '/') |
| 250 | self.check_status_and_reason(response, 404) |
| 251 | os.chmod(self.tempdir, 0o755) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 252 | |
| 253 | def test_head(self): |
| 254 | response = self.request( |
| 255 | self.tempdir_name + '/test', method='HEAD') |
| 256 | self.check_status_and_reason(response, 200) |
| 257 | self.assertEqual(response.getheader('content-length'), |
| 258 | str(len(self.data))) |
| 259 | self.assertEqual(response.getheader('content-type'), |
| 260 | 'application/octet-stream') |
| 261 | |
| 262 | def test_invalid_requests(self): |
| 263 | response = self.request('/', method='FOO') |
| 264 | self.check_status_and_reason(response, 501) |
| 265 | # requests must be case sensitive,so this should fail too |
| 266 | response = self.request('/', method='get') |
| 267 | self.check_status_and_reason(response, 501) |
| 268 | response = self.request('/', method='GETs') |
| 269 | self.check_status_and_reason(response, 501) |
| 270 | |
| 271 | |
| 272 | cgi_file1 = """\ |
| 273 | #!%s |
| 274 | |
| 275 | print("Content-type: text/html") |
| 276 | print() |
| 277 | print("Hello World") |
| 278 | """ |
| 279 | |
| 280 | cgi_file2 = """\ |
| 281 | #!%s |
| 282 | import cgi |
| 283 | |
| 284 | print("Content-type: text/html") |
| 285 | print() |
| 286 | |
| 287 | form = cgi.FieldStorage() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 288 | print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 289 | form.getfirst("bacon"))) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 290 | """ |
| 291 | |
| 292 | class CGIHTTPServerTestCase(BaseTestCase): |
| 293 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 294 | pass |
| 295 | |
| 296 | def setUp(self): |
| 297 | BaseTestCase.setUp(self) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 298 | self.cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 299 | self.parent_dir = tempfile.mkdtemp() |
| 300 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
| 301 | os.mkdir(self.cgi_dir) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 302 | self.file1_path = None |
| 303 | self.file2_path = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 304 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 305 | # The shebang line should be pure ASCII: use symlink if possible. |
| 306 | # See issue #7668. |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 307 | if support.can_symlink(): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 308 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 309 | os.symlink(sys.executable, self.pythonexe) |
| 310 | else: |
| 311 | self.pythonexe = sys.executable |
| 312 | |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 313 | try: |
| 314 | # The python executable path is written as the first line of the |
| 315 | # CGI Python script. The encoding cookie cannot be used, and so the |
| 316 | # path should be encodable to the default script encoding (utf-8) |
| 317 | self.pythonexe.encode('utf-8') |
| 318 | except UnicodeEncodeError: |
| 319 | self.tearDown() |
| 320 | raise self.skipTest( |
| 321 | "Python executable path is not encodable to utf-8") |
| 322 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 323 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 324 | with open(self.file1_path, 'w', encoding='utf-8') as file1: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 325 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 326 | os.chmod(self.file1_path, 0o777) |
| 327 | |
| 328 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 329 | with open(self.file2_path, 'w', encoding='utf-8') as file2: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 330 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 331 | os.chmod(self.file2_path, 0o777) |
| 332 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 333 | os.chdir(self.parent_dir) |
| 334 | |
| 335 | def tearDown(self): |
| 336 | try: |
| 337 | os.chdir(self.cwd) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 338 | if self.pythonexe != sys.executable: |
| 339 | os.remove(self.pythonexe) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 340 | if self.file1_path: |
| 341 | os.remove(self.file1_path) |
| 342 | if self.file2_path: |
| 343 | os.remove(self.file2_path) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 344 | os.rmdir(self.cgi_dir) |
| 345 | os.rmdir(self.parent_dir) |
| 346 | finally: |
| 347 | BaseTestCase.tearDown(self) |
| 348 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 349 | def test_url_collapse_path_split(self): |
| 350 | test_vectors = { |
| 351 | '': ('/', ''), |
| 352 | '..': IndexError, |
| 353 | '/.//..': IndexError, |
| 354 | '/': ('/', ''), |
| 355 | '//': ('/', ''), |
| 356 | '/\\': ('/', '\\'), |
| 357 | '/.//': ('/', ''), |
| 358 | 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
| 359 | '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
| 360 | 'a': ('/', 'a'), |
| 361 | '/a': ('/', 'a'), |
| 362 | '//a': ('/', 'a'), |
| 363 | './a': ('/', 'a'), |
| 364 | './C:/': ('/C:', ''), |
| 365 | '/a/b': ('/a', 'b'), |
| 366 | '/a/b/': ('/a/b', ''), |
| 367 | '/a/b/c/..': ('/a/b', ''), |
| 368 | '/a/b/c/../d': ('/a/b', 'd'), |
| 369 | '/a/b/c/../d/e/../f': ('/a/b/d', 'f'), |
| 370 | '/a/b/c/../d/e/../../f': ('/a/b', 'f'), |
| 371 | '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'), |
| 372 | '../a/b/c/../d/e/.././././..//f': IndexError, |
| 373 | '/a/b/c/../d/e/../../../f': ('/a', 'f'), |
| 374 | '/a/b/c/../d/e/../../../../f': ('/', 'f'), |
| 375 | '/a/b/c/../d/e/../../../../../f': IndexError, |
| 376 | '/a/b/c/../d/e/../../../../f/..': ('/', ''), |
| 377 | } |
| 378 | for path, expected in test_vectors.items(): |
| 379 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 380 | self.assertRaises(expected, |
| 381 | server._url_collapse_path_split, path) |
| 382 | else: |
| 383 | actual = server._url_collapse_path_split(path) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 384 | self.assertEqual(expected, actual, |
| 385 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 386 | (path, actual, expected)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 387 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 388 | def test_headers_and_content(self): |
| 389 | res = self.request('/cgi-bin/file1.py') |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 390 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 391 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 392 | |
| 393 | def test_post(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 394 | params = urllib.parse.urlencode( |
| 395 | {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 396 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 397 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 398 | |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 399 | self.assertEqual(res.read(), b'1, python, 123456\n') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 400 | |
| 401 | def test_invaliduri(self): |
| 402 | res = self.request('/cgi-bin/invalid') |
| 403 | res.read() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 404 | self.assertEqual(res.status, 404) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 405 | |
| 406 | def test_authorization(self): |
| 407 | headers = {b'Authorization' : b'Basic ' + |
| 408 | base64.b64encode(b'username:pass')} |
| 409 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 410 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 411 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 412 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 413 | def test_no_leading_slash(self): |
| 414 | # http://bugs.python.org/issue2254 |
| 415 | res = self.request('cgi-bin/file1.py') |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 416 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 417 | (res.read(), res.getheader('Content-type'), res.status)) |
| 418 | |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 419 | def test_os_environ_is_not_altered(self): |
| 420 | signature = "Test CGI Server" |
| 421 | os.environ['SERVER_SOFTWARE'] = signature |
| 422 | res = self.request('/cgi-bin/file1.py') |
| 423 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 424 | (res.read(), res.getheader('Content-type'), res.status)) |
| 425 | self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
| 426 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 427 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 428 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
| 429 | def __init__(self): |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 430 | self.get_called = False |
| 431 | self.protocol_version = "HTTP/1.1" |
| 432 | |
| 433 | def do_GET(self): |
| 434 | self.get_called = True |
| 435 | self.send_response(200) |
| 436 | self.send_header('Content-Type', 'text/html') |
| 437 | self.end_headers() |
| 438 | self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 439 | |
| 440 | def log_message(self, format, *args): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 441 | pass |
| 442 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 443 | class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
| 444 | def handle_expect_100(self): |
| 445 | self.send_error(417) |
| 446 | return False |
| 447 | |
| 448 | class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
| 449 | """Test the functionaility of the BaseHTTPServer. |
| 450 | |
| 451 | Test the support for the Expect 100-continue header. |
| 452 | """ |
| 453 | |
| 454 | HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
| 455 | |
| 456 | def setUp (self): |
| 457 | self.handler = SocketlessRequestHandler() |
| 458 | |
| 459 | def send_typical_request(self, message): |
| 460 | input = BytesIO(message) |
| 461 | output = BytesIO() |
| 462 | self.handler.rfile = input |
| 463 | self.handler.wfile = output |
| 464 | self.handler.handle_one_request() |
| 465 | output.seek(0) |
| 466 | return output.readlines() |
| 467 | |
| 468 | def verify_get_called(self): |
| 469 | self.assertTrue(self.handler.get_called) |
| 470 | |
| 471 | def verify_expected_headers(self, headers): |
| 472 | for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
| 473 | self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 474 | |
| 475 | def verify_http_server_response(self, response): |
| 476 | match = self.HTTPResponseMatch.search(response) |
| 477 | self.assertTrue(match is not None) |
| 478 | |
| 479 | def test_http_1_1(self): |
| 480 | result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
| 481 | self.verify_http_server_response(result[0]) |
| 482 | self.verify_expected_headers(result[1:-1]) |
| 483 | self.verify_get_called() |
| 484 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 485 | |
| 486 | def test_http_1_0(self): |
| 487 | result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
| 488 | self.verify_http_server_response(result[0]) |
| 489 | self.verify_expected_headers(result[1:-1]) |
| 490 | self.verify_get_called() |
| 491 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 492 | |
| 493 | def test_http_0_9(self): |
| 494 | result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
| 495 | self.assertEqual(len(result), 1) |
| 496 | self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
| 497 | self.verify_get_called() |
| 498 | |
| 499 | def test_with_continue_1_0(self): |
| 500 | result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 501 | self.verify_http_server_response(result[0]) |
| 502 | self.verify_expected_headers(result[1:-1]) |
| 503 | self.verify_get_called() |
| 504 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 505 | |
| 506 | def test_with_continue_1_1(self): |
| 507 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 508 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n') |
| 509 | self.assertEqual(result[1], b'HTTP/1.1 200 OK\r\n') |
| 510 | self.verify_expected_headers(result[2:-1]) |
| 511 | self.verify_get_called() |
| 512 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
| 513 | |
| 514 | def test_with_continue_rejected(self): |
| 515 | usual_handler = self.handler # Save to avoid breaking any subsequent tests. |
| 516 | self.handler = RejectingSocketlessRequestHandler() |
| 517 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 518 | self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n') |
| 519 | self.verify_expected_headers(result[1:-1]) |
| 520 | # The expect handler should short circuit the usual get method by |
| 521 | # returning false here, so get_called should be false |
| 522 | self.assertFalse(self.handler.get_called) |
| 523 | self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1) |
| 524 | self.handler = usual_handler # Restore to avoid breaking any subsequent tests. |
| 525 | |
| 526 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 527 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 528 | """ Test url parsing """ |
| 529 | def setUp(self): |
| 530 | self.translated = os.getcwd() |
| 531 | self.translated = os.path.join(self.translated, 'filename') |
| 532 | self.handler = SocketlessRequestHandler() |
| 533 | |
| 534 | def test_query_arguments(self): |
| 535 | path = self.handler.translate_path('/filename') |
| 536 | self.assertEqual(path, self.translated) |
| 537 | path = self.handler.translate_path('/filename?foo=bar') |
| 538 | self.assertEqual(path, self.translated) |
| 539 | path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
| 540 | self.assertEqual(path, self.translated) |
| 541 | |
| 542 | def test_start_with_double_slash(self): |
| 543 | path = self.handler.translate_path('//filename') |
| 544 | self.assertEqual(path, self.translated) |
| 545 | path = self.handler.translate_path('//filename?foo=bar') |
| 546 | self.assertEqual(path, self.translated) |
| 547 | |
| 548 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 549 | def test_main(verbose=None): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 550 | cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 551 | try: |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 552 | support.run_unittest( |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 553 | BaseHTTPRequestHandlerTestCase, |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 554 | BaseHTTPServerTestCase, |
| 555 | SimpleHTTPServerTestCase, |
| 556 | CGIHTTPServerTestCase, |
| 557 | SimpleHTTPRequestHandlerTestCase, |
| 558 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 559 | finally: |
| 560 | os.chdir(cwd) |
| 561 | |
| 562 | if __name__ == '__main__': |
| 563 | test_main() |