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 |
| 13 | import base64 |
| 14 | import shutil |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 15 | import urllib.parse |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 16 | import http.client |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 17 | import tempfile |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 18 | |
| 19 | import unittest |
| 20 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 21 | threading = support.import_module('threading') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 22 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 23 | class NoLogRequestHandler: |
| 24 | def log_message(self, *args): |
| 25 | # don't write log messages to stderr |
| 26 | pass |
| 27 | |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 28 | def read(self, n=None): |
| 29 | return '' |
| 30 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 31 | |
| 32 | class TestServerThread(threading.Thread): |
| 33 | def __init__(self, test_object, request_handler): |
| 34 | threading.Thread.__init__(self) |
| 35 | self.request_handler = request_handler |
| 36 | self.test_object = test_object |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 37 | |
| 38 | def run(self): |
| 39 | self.server = HTTPServer(('', 0), self.request_handler) |
| 40 | self.test_object.PORT = self.server.socket.getsockname()[1] |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 41 | self.test_object.server_started.set() |
| 42 | self.test_object = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 43 | try: |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 44 | self.server.serve_forever(0.05) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 45 | finally: |
| 46 | self.server.server_close() |
| 47 | |
| 48 | def stop(self): |
| 49 | self.server.shutdown() |
| 50 | |
| 51 | |
| 52 | class BaseTestCase(unittest.TestCase): |
| 53 | def setUp(self): |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 54 | self._threads = support.threading_setup() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 55 | os.environ = support.EnvironmentVarGuard() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 56 | self.server_started = threading.Event() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 57 | self.thread = TestServerThread(self, self.request_handler) |
| 58 | self.thread.start() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 59 | self.server_started.wait() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 60 | |
| 61 | def tearDown(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 62 | self.thread.stop() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 63 | os.environ.__exit__() |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 64 | support.threading_cleanup(*self._threads) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 65 | |
| 66 | def request(self, uri, method='GET', body=None, headers={}): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 67 | self.connection = http.client.HTTPConnection('localhost', self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 68 | self.connection.request(method, uri, body, headers) |
| 69 | return self.connection.getresponse() |
| 70 | |
| 71 | |
| 72 | class BaseHTTPServerTestCase(BaseTestCase): |
| 73 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 74 | protocol_version = 'HTTP/1.1' |
| 75 | default_request_version = 'HTTP/1.1' |
| 76 | |
| 77 | def do_TEST(self): |
| 78 | self.send_response(204) |
| 79 | self.send_header('Content-Type', 'text/html') |
| 80 | self.send_header('Connection', 'close') |
| 81 | self.end_headers() |
| 82 | |
| 83 | def do_KEEP(self): |
| 84 | self.send_response(204) |
| 85 | self.send_header('Content-Type', 'text/html') |
| 86 | self.send_header('Connection', 'keep-alive') |
| 87 | self.end_headers() |
| 88 | |
| 89 | def do_KEYERROR(self): |
| 90 | self.send_error(999) |
| 91 | |
| 92 | def do_CUSTOM(self): |
| 93 | self.send_response(999) |
| 94 | self.send_header('Content-Type', 'text/html') |
| 95 | self.send_header('Connection', 'close') |
| 96 | self.end_headers() |
| 97 | |
| 98 | def setUp(self): |
| 99 | BaseTestCase.setUp(self) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 100 | self.con = http.client.HTTPConnection('localhost', self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 101 | self.con.connect() |
| 102 | |
| 103 | def test_command(self): |
| 104 | self.con.request('GET', '/') |
| 105 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 106 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 107 | |
| 108 | def test_request_line_trimming(self): |
| 109 | self.con._http_vsn_str = 'HTTP/1.1\n' |
| 110 | self.con.putrequest('GET', '/') |
| 111 | self.con.endheaders() |
| 112 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 113 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 114 | |
| 115 | def test_version_bogus(self): |
| 116 | self.con._http_vsn_str = 'FUBAR' |
| 117 | self.con.putrequest('GET', '/') |
| 118 | self.con.endheaders() |
| 119 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 120 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 121 | |
| 122 | def test_version_digits(self): |
| 123 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 124 | self.con.putrequest('GET', '/') |
| 125 | self.con.endheaders() |
| 126 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 127 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 128 | |
| 129 | def test_version_none_get(self): |
| 130 | self.con._http_vsn_str = '' |
| 131 | self.con.putrequest('GET', '/') |
| 132 | self.con.endheaders() |
| 133 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 134 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 135 | |
| 136 | def test_version_none(self): |
| 137 | self.con._http_vsn_str = '' |
| 138 | self.con.putrequest('PUT', '/') |
| 139 | self.con.endheaders() |
| 140 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 141 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 142 | |
| 143 | def test_version_invalid(self): |
| 144 | self.con._http_vsn = 99 |
| 145 | self.con._http_vsn_str = 'HTTP/9.9' |
| 146 | self.con.putrequest('GET', '/') |
| 147 | self.con.endheaders() |
| 148 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 149 | self.assertEqual(res.status, 505) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 150 | |
| 151 | def test_send_blank(self): |
| 152 | self.con._http_vsn_str = '' |
| 153 | self.con.putrequest('', '') |
| 154 | self.con.endheaders() |
| 155 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 156 | self.assertEqual(res.status, 400) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 157 | |
| 158 | def test_header_close(self): |
| 159 | self.con.putrequest('GET', '/') |
| 160 | self.con.putheader('Connection', 'close') |
| 161 | self.con.endheaders() |
| 162 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 163 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 164 | |
| 165 | def test_head_keep_alive(self): |
| 166 | self.con._http_vsn_str = 'HTTP/1.1' |
| 167 | self.con.putrequest('GET', '/') |
| 168 | self.con.putheader('Connection', 'keep-alive') |
| 169 | self.con.endheaders() |
| 170 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 171 | self.assertEqual(res.status, 501) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 172 | |
| 173 | def test_handler(self): |
| 174 | self.con.request('TEST', '/') |
| 175 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 176 | self.assertEqual(res.status, 204) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 177 | |
| 178 | def test_return_header_keep_alive(self): |
| 179 | self.con.request('KEEP', '/') |
| 180 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 181 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 182 | self.con.request('TEST', '/') |
| 183 | |
| 184 | def test_internal_key_error(self): |
| 185 | self.con.request('KEYERROR', '/') |
| 186 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 187 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 188 | |
| 189 | def test_return_custom_status(self): |
| 190 | self.con.request('CUSTOM', '/') |
| 191 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 192 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 196 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 197 | pass |
| 198 | |
| 199 | def setUp(self): |
| 200 | BaseTestCase.setUp(self) |
| 201 | self.cwd = os.getcwd() |
| 202 | basetempdir = tempfile.gettempdir() |
| 203 | os.chdir(basetempdir) |
| 204 | self.data = b'We are the knights who say Ni!' |
| 205 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 206 | self.tempdir_name = os.path.basename(self.tempdir) |
| 207 | temp = open(os.path.join(self.tempdir, 'test'), 'wb') |
| 208 | temp.write(self.data) |
| 209 | temp.close() |
| 210 | |
| 211 | def tearDown(self): |
| 212 | try: |
| 213 | os.chdir(self.cwd) |
| 214 | try: |
| 215 | shutil.rmtree(self.tempdir) |
| 216 | except: |
| 217 | pass |
| 218 | finally: |
| 219 | BaseTestCase.tearDown(self) |
| 220 | |
| 221 | def check_status_and_reason(self, response, status, data=None): |
| 222 | body = response.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 223 | self.assertTrue(response) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 224 | self.assertEqual(response.status, status) |
| 225 | self.assertIsNotNone(response.reason) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 226 | if data: |
| 227 | self.assertEqual(data, body) |
| 228 | |
| 229 | def test_get(self): |
| 230 | #constructs the path relative to the root directory of the HTTPServer |
| 231 | response = self.request(self.tempdir_name + '/test') |
| 232 | self.check_status_and_reason(response, 200, data=self.data) |
| 233 | response = self.request(self.tempdir_name + '/') |
| 234 | self.check_status_and_reason(response, 200) |
| 235 | response = self.request(self.tempdir_name) |
| 236 | self.check_status_and_reason(response, 301) |
| 237 | response = self.request('/ThisDoesNotExist') |
| 238 | self.check_status_and_reason(response, 404) |
| 239 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
| 240 | self.check_status_and_reason(response, 404) |
| 241 | f = open(os.path.join(self.tempdir_name, 'index.html'), 'w') |
| 242 | response = self.request('/' + self.tempdir_name + '/') |
| 243 | self.check_status_and_reason(response, 200) |
| 244 | if os.name == 'posix': |
| 245 | # chmod won't work as expected on Windows platforms |
| 246 | os.chmod(self.tempdir, 0) |
| 247 | response = self.request(self.tempdir_name + '/') |
| 248 | self.check_status_and_reason(response, 404) |
| 249 | os.chmod(self.tempdir, 0o755) |
| 250 | |
| 251 | def test_head(self): |
| 252 | response = self.request( |
| 253 | self.tempdir_name + '/test', method='HEAD') |
| 254 | self.check_status_and_reason(response, 200) |
| 255 | self.assertEqual(response.getheader('content-length'), |
| 256 | str(len(self.data))) |
| 257 | self.assertEqual(response.getheader('content-type'), |
| 258 | 'application/octet-stream') |
| 259 | |
| 260 | def test_invalid_requests(self): |
| 261 | response = self.request('/', method='FOO') |
| 262 | self.check_status_and_reason(response, 501) |
| 263 | # requests must be case sensitive,so this should fail too |
| 264 | response = self.request('/', method='get') |
| 265 | self.check_status_and_reason(response, 501) |
| 266 | response = self.request('/', method='GETs') |
| 267 | self.check_status_and_reason(response, 501) |
| 268 | |
| 269 | |
| 270 | cgi_file1 = """\ |
| 271 | #!%s |
| 272 | |
| 273 | print("Content-type: text/html") |
| 274 | print() |
| 275 | print("Hello World") |
| 276 | """ |
| 277 | |
| 278 | cgi_file2 = """\ |
| 279 | #!%s |
| 280 | import cgi |
| 281 | |
| 282 | print("Content-type: text/html") |
| 283 | print() |
| 284 | |
| 285 | form = cgi.FieldStorage() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 286 | print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 287 | form.getfirst("bacon"))) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 288 | """ |
| 289 | |
| 290 | class CGIHTTPServerTestCase(BaseTestCase): |
| 291 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 292 | pass |
| 293 | |
| 294 | def setUp(self): |
| 295 | BaseTestCase.setUp(self) |
| 296 | self.parent_dir = tempfile.mkdtemp() |
| 297 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
| 298 | os.mkdir(self.cgi_dir) |
| 299 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 300 | # The shebang line should be pure ASCII: use symlink if possible. |
| 301 | # See issue #7668. |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 302 | if support.can_symlink(): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 303 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 304 | os.symlink(sys.executable, self.pythonexe) |
| 305 | else: |
| 306 | self.pythonexe = sys.executable |
| 307 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 308 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
| 309 | with open(self.file1_path, 'w') as file1: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 310 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 311 | os.chmod(self.file1_path, 0o777) |
| 312 | |
| 313 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
| 314 | with open(self.file2_path, 'w') as file2: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 315 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 316 | os.chmod(self.file2_path, 0o777) |
| 317 | |
| 318 | self.cwd = os.getcwd() |
| 319 | os.chdir(self.parent_dir) |
| 320 | |
| 321 | def tearDown(self): |
| 322 | try: |
| 323 | os.chdir(self.cwd) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 324 | if self.pythonexe != sys.executable: |
| 325 | os.remove(self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 326 | os.remove(self.file1_path) |
| 327 | os.remove(self.file2_path) |
| 328 | os.rmdir(self.cgi_dir) |
| 329 | os.rmdir(self.parent_dir) |
| 330 | finally: |
| 331 | BaseTestCase.tearDown(self) |
| 332 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 333 | def test_url_collapse_path_split(self): |
| 334 | test_vectors = { |
| 335 | '': ('/', ''), |
| 336 | '..': IndexError, |
| 337 | '/.//..': IndexError, |
| 338 | '/': ('/', ''), |
| 339 | '//': ('/', ''), |
| 340 | '/\\': ('/', '\\'), |
| 341 | '/.//': ('/', ''), |
| 342 | 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
| 343 | '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
| 344 | 'a': ('/', 'a'), |
| 345 | '/a': ('/', 'a'), |
| 346 | '//a': ('/', 'a'), |
| 347 | './a': ('/', 'a'), |
| 348 | './C:/': ('/C:', ''), |
| 349 | '/a/b': ('/a', 'b'), |
| 350 | '/a/b/': ('/a/b', ''), |
| 351 | '/a/b/c/..': ('/a/b', ''), |
| 352 | '/a/b/c/../d': ('/a/b', 'd'), |
| 353 | '/a/b/c/../d/e/../f': ('/a/b/d', 'f'), |
| 354 | '/a/b/c/../d/e/../../f': ('/a/b', 'f'), |
| 355 | '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'), |
| 356 | '../a/b/c/../d/e/.././././..//f': IndexError, |
| 357 | '/a/b/c/../d/e/../../../f': ('/a', 'f'), |
| 358 | '/a/b/c/../d/e/../../../../f': ('/', 'f'), |
| 359 | '/a/b/c/../d/e/../../../../../f': IndexError, |
| 360 | '/a/b/c/../d/e/../../../../f/..': ('/', ''), |
| 361 | } |
| 362 | for path, expected in test_vectors.items(): |
| 363 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 364 | self.assertRaises(expected, |
| 365 | server._url_collapse_path_split, path) |
| 366 | else: |
| 367 | actual = server._url_collapse_path_split(path) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 368 | self.assertEqual(expected, actual, |
| 369 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 370 | (path, actual, expected)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 371 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 372 | def test_headers_and_content(self): |
| 373 | res = self.request('/cgi-bin/file1.py') |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 374 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 375 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 376 | |
| 377 | def test_post(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 378 | params = urllib.parse.urlencode( |
| 379 | {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 380 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 381 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 382 | |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 383 | self.assertEqual(res.read(), b'1, python, 123456\n') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 384 | |
| 385 | def test_invaliduri(self): |
| 386 | res = self.request('/cgi-bin/invalid') |
| 387 | res.read() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 388 | self.assertEqual(res.status, 404) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 389 | |
| 390 | def test_authorization(self): |
| 391 | headers = {b'Authorization' : b'Basic ' + |
| 392 | base64.b64encode(b'username:pass')} |
| 393 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 394 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 395 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 397 | def test_no_leading_slash(self): |
| 398 | # http://bugs.python.org/issue2254 |
| 399 | res = self.request('cgi-bin/file1.py') |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 400 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 401 | (res.read(), res.getheader('Content-type'), res.status)) |
| 402 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 403 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 404 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
| 405 | def __init__(self): |
| 406 | pass |
| 407 | |
| 408 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 409 | """ Test url parsing """ |
| 410 | def setUp(self): |
| 411 | self.translated = os.getcwd() |
| 412 | self.translated = os.path.join(self.translated, 'filename') |
| 413 | self.handler = SocketlessRequestHandler() |
| 414 | |
| 415 | def test_query_arguments(self): |
| 416 | path = self.handler.translate_path('/filename') |
| 417 | self.assertEqual(path, self.translated) |
| 418 | path = self.handler.translate_path('/filename?foo=bar') |
| 419 | self.assertEqual(path, self.translated) |
| 420 | path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
| 421 | self.assertEqual(path, self.translated) |
| 422 | |
| 423 | def test_start_with_double_slash(self): |
| 424 | path = self.handler.translate_path('//filename') |
| 425 | self.assertEqual(path, self.translated) |
| 426 | path = self.handler.translate_path('//filename?foo=bar') |
| 427 | self.assertEqual(path, self.translated) |
| 428 | |
| 429 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 430 | def test_main(verbose=None): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 431 | cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 432 | try: |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 433 | support.run_unittest( |
| 434 | BaseHTTPServerTestCase, |
| 435 | SimpleHTTPServerTestCase, |
| 436 | CGIHTTPServerTestCase, |
| 437 | SimpleHTTPRequestHandlerTestCase, |
| 438 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 439 | finally: |
| 440 | os.chdir(cwd) |
| 441 | |
| 442 | if __name__ == '__main__': |
| 443 | test_main() |