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 |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 9 | from http import server, HTTPStatus |
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 |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 17 | import html |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 18 | import http.client |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 19 | import tempfile |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 20 | from io import BytesIO |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 21 | |
| 22 | import unittest |
| 23 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 24 | threading = support.import_module('threading') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 25 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 26 | class NoLogRequestHandler: |
| 27 | def log_message(self, *args): |
| 28 | # don't write log messages to stderr |
| 29 | pass |
| 30 | |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 31 | def read(self, n=None): |
| 32 | return '' |
| 33 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 34 | |
| 35 | class TestServerThread(threading.Thread): |
| 36 | def __init__(self, test_object, request_handler): |
| 37 | threading.Thread.__init__(self) |
| 38 | self.request_handler = request_handler |
| 39 | self.test_object = test_object |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 40 | |
| 41 | def run(self): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 42 | self.server = HTTPServer(('localhost', 0), self.request_handler) |
| 43 | self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 44 | self.test_object.server_started.set() |
| 45 | self.test_object = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 46 | try: |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 47 | self.server.serve_forever(0.05) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 48 | finally: |
| 49 | self.server.server_close() |
| 50 | |
| 51 | def stop(self): |
| 52 | self.server.shutdown() |
| 53 | |
| 54 | |
| 55 | class BaseTestCase(unittest.TestCase): |
| 56 | def setUp(self): |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 57 | self._threads = support.threading_setup() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 58 | os.environ = support.EnvironmentVarGuard() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 59 | self.server_started = threading.Event() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 60 | self.thread = TestServerThread(self, self.request_handler) |
| 61 | self.thread.start() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 62 | self.server_started.wait() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 63 | |
| 64 | def tearDown(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 65 | self.thread.stop() |
Antoine Pitrou | f727082 | 2012-09-30 01:05:30 +0200 | [diff] [blame] | 66 | self.thread = None |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 67 | os.environ.__exit__() |
Antoine Pitrou | 45ebeb8 | 2009-10-27 18:52:30 +0000 | [diff] [blame] | 68 | support.threading_cleanup(*self._threads) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 69 | |
| 70 | def request(self, uri, method='GET', body=None, headers={}): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 71 | self.connection = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 72 | self.connection.request(method, uri, body, headers) |
| 73 | return self.connection.getresponse() |
| 74 | |
| 75 | |
| 76 | class BaseHTTPServerTestCase(BaseTestCase): |
| 77 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 78 | protocol_version = 'HTTP/1.1' |
| 79 | default_request_version = 'HTTP/1.1' |
| 80 | |
| 81 | def do_TEST(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 82 | self.send_response(HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 83 | self.send_header('Content-Type', 'text/html') |
| 84 | self.send_header('Connection', 'close') |
| 85 | self.end_headers() |
| 86 | |
| 87 | def do_KEEP(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 88 | self.send_response(HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 89 | self.send_header('Content-Type', 'text/html') |
| 90 | self.send_header('Connection', 'keep-alive') |
| 91 | self.end_headers() |
| 92 | |
| 93 | def do_KEYERROR(self): |
| 94 | self.send_error(999) |
| 95 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 96 | def do_NOTFOUND(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 97 | self.send_error(HTTPStatus.NOT_FOUND) |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 98 | |
Senthil Kumaran | 2688644 | 2013-03-15 07:53:21 -0700 | [diff] [blame] | 99 | def do_EXPLAINERROR(self): |
| 100 | self.send_error(999, "Short Message", |
| 101 | "This is a long \n explaination") |
| 102 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 103 | def do_CUSTOM(self): |
| 104 | self.send_response(999) |
| 105 | self.send_header('Content-Type', 'text/html') |
| 106 | self.send_header('Connection', 'close') |
| 107 | self.end_headers() |
| 108 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 109 | def do_LATINONEHEADER(self): |
| 110 | self.send_response(999) |
| 111 | self.send_header('X-Special', 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 112 | self.send_header('Connection', 'close') |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 113 | self.end_headers() |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 114 | body = self.headers['x-special-incoming'].encode('utf-8') |
| 115 | self.wfile.write(body) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 116 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 117 | def setUp(self): |
| 118 | BaseTestCase.setUp(self) |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 119 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 120 | self.con.connect() |
| 121 | |
| 122 | def test_command(self): |
| 123 | self.con.request('GET', '/') |
| 124 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 125 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 126 | |
| 127 | def test_request_line_trimming(self): |
| 128 | self.con._http_vsn_str = 'HTTP/1.1\n' |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 129 | self.con.putrequest('XYZBOGUS', '/') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 130 | self.con.endheaders() |
| 131 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 132 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 133 | |
| 134 | def test_version_bogus(self): |
| 135 | self.con._http_vsn_str = 'FUBAR' |
| 136 | self.con.putrequest('GET', '/') |
| 137 | self.con.endheaders() |
| 138 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 139 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 140 | |
| 141 | def test_version_digits(self): |
| 142 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 143 | self.con.putrequest('GET', '/') |
| 144 | self.con.endheaders() |
| 145 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 146 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 147 | |
| 148 | def test_version_none_get(self): |
| 149 | self.con._http_vsn_str = '' |
| 150 | self.con.putrequest('GET', '/') |
| 151 | self.con.endheaders() |
| 152 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 153 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 154 | |
| 155 | def test_version_none(self): |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 156 | # Test that a valid method is rejected when not HTTP/1.x |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 157 | self.con._http_vsn_str = '' |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 158 | self.con.putrequest('CUSTOM', '/') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 159 | self.con.endheaders() |
| 160 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 161 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 162 | |
| 163 | def test_version_invalid(self): |
| 164 | self.con._http_vsn = 99 |
| 165 | self.con._http_vsn_str = 'HTTP/9.9' |
| 166 | self.con.putrequest('GET', '/') |
| 167 | self.con.endheaders() |
| 168 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 169 | self.assertEqual(res.status, HTTPStatus.HTTP_VERSION_NOT_SUPPORTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 170 | |
| 171 | def test_send_blank(self): |
| 172 | self.con._http_vsn_str = '' |
| 173 | self.con.putrequest('', '') |
| 174 | self.con.endheaders() |
| 175 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 176 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 177 | |
| 178 | def test_header_close(self): |
| 179 | self.con.putrequest('GET', '/') |
| 180 | self.con.putheader('Connection', 'close') |
| 181 | self.con.endheaders() |
| 182 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 183 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 184 | |
| 185 | def test_head_keep_alive(self): |
| 186 | self.con._http_vsn_str = 'HTTP/1.1' |
| 187 | self.con.putrequest('GET', '/') |
| 188 | self.con.putheader('Connection', 'keep-alive') |
| 189 | self.con.endheaders() |
| 190 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 191 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 192 | |
| 193 | def test_handler(self): |
| 194 | self.con.request('TEST', '/') |
| 195 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 196 | self.assertEqual(res.status, HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 197 | |
| 198 | def test_return_header_keep_alive(self): |
| 199 | self.con.request('KEEP', '/') |
| 200 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 201 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 202 | self.con.request('TEST', '/') |
Brian Curtin | 61d0d60 | 2010-10-31 00:34:23 +0000 | [diff] [blame] | 203 | self.addCleanup(self.con.close) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 204 | |
| 205 | def test_internal_key_error(self): |
| 206 | self.con.request('KEYERROR', '/') |
| 207 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 208 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 209 | |
| 210 | def test_return_custom_status(self): |
| 211 | self.con.request('CUSTOM', '/') |
| 212 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 213 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 214 | |
Senthil Kumaran | 2688644 | 2013-03-15 07:53:21 -0700 | [diff] [blame] | 215 | def test_return_explain_error(self): |
| 216 | self.con.request('EXPLAINERROR', '/') |
| 217 | res = self.con.getresponse() |
| 218 | self.assertEqual(res.status, 999) |
| 219 | self.assertTrue(int(res.getheader('Content-Length'))) |
| 220 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 221 | def test_latin1_header(self): |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 222 | self.con.request('LATINONEHEADER', '/', headers={ |
| 223 | 'X-Special-Incoming': 'Ärger mit Unicode' |
| 224 | }) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 225 | res = self.con.getresponse() |
| 226 | self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 227 | self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8')) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 228 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 229 | def test_error_content_length(self): |
| 230 | # Issue #16088: standard error responses should have a content-length |
| 231 | self.con.request('NOTFOUND', '/') |
| 232 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 233 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
| 234 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 235 | data = res.read() |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 236 | self.assertEqual(int(res.getheader('Content-Length')), len(data)) |
| 237 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 238 | |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 239 | class RequestHandlerLoggingTestCase(BaseTestCase): |
| 240 | class request_handler(BaseHTTPRequestHandler): |
| 241 | protocol_version = 'HTTP/1.1' |
| 242 | default_request_version = 'HTTP/1.1' |
| 243 | |
| 244 | def do_GET(self): |
| 245 | self.send_response(HTTPStatus.OK) |
| 246 | self.end_headers() |
| 247 | |
| 248 | def do_ERROR(self): |
| 249 | self.send_error(HTTPStatus.NOT_FOUND, 'File not found') |
| 250 | |
| 251 | def test_get(self): |
| 252 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 253 | self.con.connect() |
| 254 | |
| 255 | with support.captured_stderr() as err: |
| 256 | self.con.request('GET', '/') |
| 257 | self.con.getresponse() |
| 258 | |
| 259 | self.assertTrue( |
| 260 | err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n')) |
| 261 | |
| 262 | def test_err(self): |
| 263 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 264 | self.con.connect() |
| 265 | |
| 266 | with support.captured_stderr() as err: |
| 267 | self.con.request('ERROR', '/') |
| 268 | self.con.getresponse() |
| 269 | |
| 270 | lines = err.getvalue().split('\n') |
| 271 | self.assertTrue(lines[0].endswith('code 404, message File not found')) |
| 272 | self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -')) |
| 273 | |
| 274 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 275 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 276 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 277 | pass |
| 278 | |
| 279 | def setUp(self): |
| 280 | BaseTestCase.setUp(self) |
| 281 | self.cwd = os.getcwd() |
| 282 | basetempdir = tempfile.gettempdir() |
| 283 | os.chdir(basetempdir) |
| 284 | self.data = b'We are the knights who say Ni!' |
| 285 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 286 | self.tempdir_name = os.path.basename(self.tempdir) |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 287 | with open(os.path.join(self.tempdir, 'test'), 'wb') as temp: |
| 288 | temp.write(self.data) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 289 | |
| 290 | def tearDown(self): |
| 291 | try: |
| 292 | os.chdir(self.cwd) |
| 293 | try: |
| 294 | shutil.rmtree(self.tempdir) |
| 295 | except: |
| 296 | pass |
| 297 | finally: |
| 298 | BaseTestCase.tearDown(self) |
| 299 | |
| 300 | def check_status_and_reason(self, response, status, data=None): |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 301 | def close_conn(): |
| 302 | """Don't close reader yet so we can check if there was leftover |
| 303 | buffered input""" |
| 304 | nonlocal reader |
| 305 | reader = response.fp |
| 306 | response.fp = None |
| 307 | reader = None |
| 308 | response._close_conn = close_conn |
| 309 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 310 | body = response.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 311 | self.assertTrue(response) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 312 | self.assertEqual(response.status, status) |
| 313 | self.assertIsNotNone(response.reason) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 314 | if data: |
| 315 | self.assertEqual(data, body) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 316 | # Ensure the server has not set up a persistent connection, and has |
| 317 | # not sent any extra data |
| 318 | self.assertEqual(response.version, 10) |
| 319 | self.assertEqual(response.msg.get("Connection", "close"), "close") |
| 320 | self.assertEqual(reader.read(30), b'', 'Connection should be closed') |
| 321 | |
| 322 | reader.close() |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 323 | return body |
| 324 | |
Ned Deily | 1418320 | 2015-01-05 01:02:30 -0800 | [diff] [blame] | 325 | @support.requires_mac_ver(10, 5) |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 326 | @unittest.skipUnless(support.TESTFN_UNDECODABLE, |
| 327 | 'need support.TESTFN_UNDECODABLE') |
| 328 | def test_undecodable_filename(self): |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 329 | enc = sys.getfilesystemencoding() |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 330 | filename = os.fsdecode(support.TESTFN_UNDECODABLE) + '.txt' |
| 331 | with open(os.path.join(self.tempdir, filename), 'wb') as f: |
| 332 | f.write(support.TESTFN_UNDECODABLE) |
| 333 | response = self.request(self.tempdir_name + '/') |
Serhiy Storchaka | d9e9528 | 2014-08-17 16:57:39 +0300 | [diff] [blame] | 334 | if sys.platform == 'darwin': |
| 335 | # On Mac OS the HFS+ filesystem replaces bytes that aren't valid |
| 336 | # UTF-8 into a percent-encoded value. |
| 337 | for name in os.listdir(self.tempdir): |
| 338 | if name != 'test': # Ignore a filename created in setUp(). |
| 339 | filename = name |
| 340 | break |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 341 | body = self.check_status_and_reason(response, HTTPStatus.OK) |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 342 | quotedname = urllib.parse.quote(filename, errors='surrogatepass') |
| 343 | self.assertIn(('href="%s"' % quotedname) |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 344 | .encode(enc, 'surrogateescape'), body) |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 345 | self.assertIn(('>%s<' % html.escape(filename)) |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 346 | .encode(enc, 'surrogateescape'), body) |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 347 | response = self.request(self.tempdir_name + '/' + quotedname) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 348 | self.check_status_and_reason(response, HTTPStatus.OK, |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 349 | data=support.TESTFN_UNDECODABLE) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 350 | |
| 351 | def test_get(self): |
| 352 | #constructs the path relative to the root directory of the HTTPServer |
| 353 | response = self.request(self.tempdir_name + '/test') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 354 | self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
Senthil Kumaran | 72c238e | 2013-09-13 00:21:18 -0700 | [diff] [blame] | 355 | # check for trailing "/" which should return 404. See Issue17324 |
| 356 | response = self.request(self.tempdir_name + '/test/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 357 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 358 | response = self.request(self.tempdir_name + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 359 | self.check_status_and_reason(response, HTTPStatus.OK) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 360 | response = self.request(self.tempdir_name) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 361 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
Benjamin Peterson | 94cb7a2 | 2014-12-26 10:53:43 -0600 | [diff] [blame] | 362 | response = self.request(self.tempdir_name + '/?hi=2') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 363 | self.check_status_and_reason(response, HTTPStatus.OK) |
Benjamin Peterson | 94cb7a2 | 2014-12-26 10:53:43 -0600 | [diff] [blame] | 364 | response = self.request(self.tempdir_name + '?hi=1') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 365 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
Benjamin Peterson | 94cb7a2 | 2014-12-26 10:53:43 -0600 | [diff] [blame] | 366 | self.assertEqual(response.getheader("Location"), |
| 367 | self.tempdir_name + "/?hi=1") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 368 | response = self.request('/ThisDoesNotExist') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 369 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 370 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 371 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 372 | |
| 373 | data = b"Dummy index file\r\n" |
| 374 | with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f: |
| 375 | f.write(data) |
| 376 | response = self.request('/' + self.tempdir_name + '/') |
| 377 | self.check_status_and_reason(response, HTTPStatus.OK, data) |
| 378 | |
| 379 | # chmod() doesn't work as expected on Windows, and filesystem |
| 380 | # permissions are ignored by root on Unix. |
| 381 | if os.name == 'posix' and os.geteuid() != 0: |
| 382 | os.chmod(self.tempdir, 0) |
| 383 | try: |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 384 | response = self.request(self.tempdir_name + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 385 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 386 | finally: |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 387 | os.chmod(self.tempdir, 0o755) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 388 | |
| 389 | def test_head(self): |
| 390 | response = self.request( |
| 391 | self.tempdir_name + '/test', method='HEAD') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 392 | self.check_status_and_reason(response, HTTPStatus.OK) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 393 | self.assertEqual(response.getheader('content-length'), |
| 394 | str(len(self.data))) |
| 395 | self.assertEqual(response.getheader('content-type'), |
| 396 | 'application/octet-stream') |
| 397 | |
| 398 | def test_invalid_requests(self): |
| 399 | response = self.request('/', method='FOO') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 400 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 401 | # requests must be case sensitive,so this should fail too |
Terry Jan Reedy | dd09efd | 2014-10-18 17:10:09 -0400 | [diff] [blame] | 402 | response = self.request('/', method='custom') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 403 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 404 | response = self.request('/', method='GETs') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 405 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 406 | |
| 407 | |
| 408 | cgi_file1 = """\ |
| 409 | #!%s |
| 410 | |
| 411 | print("Content-type: text/html") |
| 412 | print() |
| 413 | print("Hello World") |
| 414 | """ |
| 415 | |
| 416 | cgi_file2 = """\ |
| 417 | #!%s |
| 418 | import cgi |
| 419 | |
| 420 | print("Content-type: text/html") |
| 421 | print() |
| 422 | |
| 423 | form = cgi.FieldStorage() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 424 | print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 425 | form.getfirst("bacon"))) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 426 | """ |
| 427 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 428 | cgi_file4 = """\ |
| 429 | #!%s |
| 430 | import os |
| 431 | |
| 432 | print("Content-type: text/html") |
| 433 | print() |
| 434 | |
| 435 | print(os.environ["%s"]) |
| 436 | """ |
| 437 | |
Charles-François Natali | f7ed9fc | 2011-11-02 19:35:14 +0100 | [diff] [blame] | 438 | |
| 439 | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
| 440 | "This test can't be run reliably as root (issue #13308).") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 441 | class CGIHTTPServerTestCase(BaseTestCase): |
| 442 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 443 | pass |
| 444 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 445 | linesep = os.linesep.encode('ascii') |
| 446 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 447 | def setUp(self): |
| 448 | BaseTestCase.setUp(self) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 449 | self.cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 450 | self.parent_dir = tempfile.mkdtemp() |
| 451 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 452 | self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 453 | os.mkdir(self.cgi_dir) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 454 | os.mkdir(self.cgi_child_dir) |
Benjamin Peterson | 35aca89 | 2013-10-30 12:48:59 -0400 | [diff] [blame] | 455 | self.nocgi_path = None |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 456 | self.file1_path = None |
| 457 | self.file2_path = None |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 458 | self.file3_path = None |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 459 | self.file4_path = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 460 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 461 | # The shebang line should be pure ASCII: use symlink if possible. |
| 462 | # See issue #7668. |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 463 | if support.can_symlink(): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 464 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 465 | os.symlink(sys.executable, self.pythonexe) |
| 466 | else: |
| 467 | self.pythonexe = sys.executable |
| 468 | |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 469 | try: |
| 470 | # The python executable path is written as the first line of the |
| 471 | # CGI Python script. The encoding cookie cannot be used, and so the |
| 472 | # path should be encodable to the default script encoding (utf-8) |
| 473 | self.pythonexe.encode('utf-8') |
| 474 | except UnicodeEncodeError: |
| 475 | self.tearDown() |
Serhiy Storchaka | 0b4591e | 2013-02-04 15:45:00 +0200 | [diff] [blame] | 476 | self.skipTest("Python executable path is not encodable to utf-8") |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 477 | |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 478 | self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py') |
| 479 | with open(self.nocgi_path, 'w') as fp: |
| 480 | fp.write(cgi_file1 % self.pythonexe) |
| 481 | os.chmod(self.nocgi_path, 0o777) |
| 482 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 483 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 484 | with open(self.file1_path, 'w', encoding='utf-8') as file1: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 485 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 486 | os.chmod(self.file1_path, 0o777) |
| 487 | |
| 488 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 489 | with open(self.file2_path, 'w', encoding='utf-8') as file2: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 490 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 491 | os.chmod(self.file2_path, 0o777) |
| 492 | |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 493 | self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py') |
| 494 | with open(self.file3_path, 'w', encoding='utf-8') as file3: |
| 495 | file3.write(cgi_file1 % self.pythonexe) |
| 496 | os.chmod(self.file3_path, 0o777) |
| 497 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 498 | self.file4_path = os.path.join(self.cgi_dir, 'file4.py') |
| 499 | with open(self.file4_path, 'w', encoding='utf-8') as file4: |
| 500 | file4.write(cgi_file4 % (self.pythonexe, 'QUERY_STRING')) |
| 501 | os.chmod(self.file4_path, 0o777) |
| 502 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 503 | os.chdir(self.parent_dir) |
| 504 | |
| 505 | def tearDown(self): |
| 506 | try: |
| 507 | os.chdir(self.cwd) |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 508 | if self.pythonexe != sys.executable: |
| 509 | os.remove(self.pythonexe) |
Benjamin Peterson | 35aca89 | 2013-10-30 12:48:59 -0400 | [diff] [blame] | 510 | if self.nocgi_path: |
| 511 | os.remove(self.nocgi_path) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 512 | if self.file1_path: |
| 513 | os.remove(self.file1_path) |
| 514 | if self.file2_path: |
| 515 | os.remove(self.file2_path) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 516 | if self.file3_path: |
| 517 | os.remove(self.file3_path) |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 518 | if self.file4_path: |
| 519 | os.remove(self.file4_path) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 520 | os.rmdir(self.cgi_child_dir) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 521 | os.rmdir(self.cgi_dir) |
| 522 | os.rmdir(self.parent_dir) |
| 523 | finally: |
| 524 | BaseTestCase.tearDown(self) |
| 525 | |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 526 | def test_url_collapse_path(self): |
| 527 | # 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] | 528 | test_vectors = { |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 529 | '': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 530 | '..': IndexError, |
| 531 | '/.//..': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 532 | '/': '//', |
| 533 | '//': '//', |
| 534 | '/\\': '//\\', |
| 535 | '/.//': '//', |
| 536 | 'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 537 | '/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 538 | 'a': '//a', |
| 539 | '/a': '//a', |
| 540 | '//a': '//a', |
| 541 | './a': '//a', |
| 542 | './C:/': '/C:/', |
| 543 | '/a/b': '/a/b', |
| 544 | '/a/b/': '/a/b/', |
| 545 | '/a/b/.': '/a/b/', |
| 546 | '/a/b/c/..': '/a/b/', |
| 547 | '/a/b/c/../d': '/a/b/d', |
| 548 | '/a/b/c/../d/e/../f': '/a/b/d/f', |
| 549 | '/a/b/c/../d/e/../../f': '/a/b/f', |
| 550 | '/a/b/c/../d/e/.././././..//f': '/a/b/f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 551 | '../a/b/c/../d/e/.././././..//f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 552 | '/a/b/c/../d/e/../../../f': '/a/f', |
| 553 | '/a/b/c/../d/e/../../../../f': '//f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 554 | '/a/b/c/../d/e/../../../../../f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 555 | '/a/b/c/../d/e/../../../../f/..': '//', |
| 556 | '/a/b/c/../d/e/../../../../f/../.': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 557 | } |
| 558 | for path, expected in test_vectors.items(): |
| 559 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 560 | self.assertRaises(expected, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 561 | server._url_collapse_path, path) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 562 | else: |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 563 | actual = server._url_collapse_path(path) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 564 | self.assertEqual(expected, actual, |
| 565 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 566 | (path, actual, expected)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 567 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 568 | def test_headers_and_content(self): |
| 569 | res = self.request('/cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 570 | self.assertEqual( |
| 571 | (res.read(), res.getheader('Content-type'), res.status), |
| 572 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 573 | |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 574 | def test_issue19435(self): |
| 575 | res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 576 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 577 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 578 | def test_post(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 579 | params = urllib.parse.urlencode( |
| 580 | {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 581 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 582 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 583 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 584 | self.assertEqual(res.read(), b'1, python, 123456' + self.linesep) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 585 | |
| 586 | def test_invaliduri(self): |
| 587 | res = self.request('/cgi-bin/invalid') |
| 588 | res.read() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 589 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 590 | |
| 591 | def test_authorization(self): |
| 592 | headers = {b'Authorization' : b'Basic ' + |
| 593 | base64.b64encode(b'username:pass')} |
| 594 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 595 | self.assertEqual( |
| 596 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 597 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 598 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 599 | def test_no_leading_slash(self): |
| 600 | # http://bugs.python.org/issue2254 |
| 601 | res = self.request('cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 602 | self.assertEqual( |
| 603 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 604 | (res.read(), res.getheader('Content-type'), res.status)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 605 | |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 606 | def test_os_environ_is_not_altered(self): |
| 607 | signature = "Test CGI Server" |
| 608 | os.environ['SERVER_SOFTWARE'] = signature |
| 609 | res = self.request('/cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 610 | self.assertEqual( |
| 611 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 612 | (res.read(), res.getheader('Content-type'), res.status)) |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 613 | self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
| 614 | |
Benjamin Peterson | 73b8b1c | 2014-06-14 18:36:29 -0700 | [diff] [blame] | 615 | def test_urlquote_decoding_in_cgi_check(self): |
| 616 | res = self.request('/cgi-bin%2ffile1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 617 | self.assertEqual( |
| 618 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 619 | (res.read(), res.getheader('Content-type'), res.status)) |
Benjamin Peterson | 73b8b1c | 2014-06-14 18:36:29 -0700 | [diff] [blame] | 620 | |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 621 | def test_nested_cgi_path_issue21323(self): |
| 622 | res = self.request('/cgi-bin/child-dir/file3.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 623 | self.assertEqual( |
| 624 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 625 | (res.read(), res.getheader('Content-type'), res.status)) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 626 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 627 | def test_query_with_multiple_question_mark(self): |
| 628 | res = self.request('/cgi-bin/file4.py?a=b?c=d') |
| 629 | self.assertEqual( |
Martin Panter | eb1fee9 | 2015-10-03 06:07:22 +0000 | [diff] [blame] | 630 | (b'a=b?c=d' + self.linesep, 'text/html', HTTPStatus.OK), |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 631 | (res.read(), res.getheader('Content-type'), res.status)) |
| 632 | |
Martin Panter | cb29e8c | 2015-10-03 05:55:46 +0000 | [diff] [blame] | 633 | def test_query_with_continuous_slashes(self): |
| 634 | res = self.request('/cgi-bin/file4.py?k=aa%2F%2Fbb&//q//p//=//a//b//') |
| 635 | self.assertEqual( |
| 636 | (b'k=aa%2F%2Fbb&//q//p//=//a//b//' + self.linesep, |
Martin Panter | eb1fee9 | 2015-10-03 06:07:22 +0000 | [diff] [blame] | 637 | 'text/html', HTTPStatus.OK), |
Martin Panter | cb29e8c | 2015-10-03 05:55:46 +0000 | [diff] [blame] | 638 | (res.read(), res.getheader('Content-type'), res.status)) |
| 639 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 640 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 641 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
| 642 | def __init__(self): |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 643 | self.get_called = False |
| 644 | self.protocol_version = "HTTP/1.1" |
| 645 | |
| 646 | def do_GET(self): |
| 647 | self.get_called = True |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 648 | self.send_response(HTTPStatus.OK) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 649 | self.send_header('Content-Type', 'text/html') |
| 650 | self.end_headers() |
| 651 | self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 652 | |
| 653 | def log_message(self, format, *args): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 654 | pass |
| 655 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 656 | class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
| 657 | def handle_expect_100(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 658 | self.send_error(HTTPStatus.EXPECTATION_FAILED) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 659 | return False |
| 660 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 661 | |
| 662 | class AuditableBytesIO: |
| 663 | |
| 664 | def __init__(self): |
| 665 | self.datas = [] |
| 666 | |
| 667 | def write(self, data): |
| 668 | self.datas.append(data) |
| 669 | |
| 670 | def getData(self): |
| 671 | return b''.join(self.datas) |
| 672 | |
| 673 | @property |
| 674 | def numWrites(self): |
| 675 | return len(self.datas) |
| 676 | |
| 677 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 678 | class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
Ezio Melotti | 3b3499b | 2011-03-16 11:35:38 +0200 | [diff] [blame] | 679 | """Test the functionality of the BaseHTTPServer. |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 680 | |
| 681 | Test the support for the Expect 100-continue header. |
| 682 | """ |
| 683 | |
| 684 | HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
| 685 | |
| 686 | def setUp (self): |
| 687 | self.handler = SocketlessRequestHandler() |
| 688 | |
| 689 | def send_typical_request(self, message): |
| 690 | input = BytesIO(message) |
| 691 | output = BytesIO() |
| 692 | self.handler.rfile = input |
| 693 | self.handler.wfile = output |
| 694 | self.handler.handle_one_request() |
| 695 | output.seek(0) |
| 696 | return output.readlines() |
| 697 | |
| 698 | def verify_get_called(self): |
| 699 | self.assertTrue(self.handler.get_called) |
| 700 | |
| 701 | def verify_expected_headers(self, headers): |
| 702 | for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
| 703 | self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 704 | |
| 705 | def verify_http_server_response(self, response): |
| 706 | match = self.HTTPResponseMatch.search(response) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 707 | self.assertIsNotNone(match) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 708 | |
| 709 | def test_http_1_1(self): |
| 710 | result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
| 711 | self.verify_http_server_response(result[0]) |
| 712 | self.verify_expected_headers(result[1:-1]) |
| 713 | self.verify_get_called() |
| 714 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 715 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 716 | self.assertEqual(self.handler.command, 'GET') |
| 717 | self.assertEqual(self.handler.path, '/') |
| 718 | self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 719 | self.assertSequenceEqual(self.handler.headers.items(), ()) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 720 | |
| 721 | def test_http_1_0(self): |
| 722 | result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
| 723 | self.verify_http_server_response(result[0]) |
| 724 | self.verify_expected_headers(result[1:-1]) |
| 725 | self.verify_get_called() |
| 726 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 727 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 728 | self.assertEqual(self.handler.command, 'GET') |
| 729 | self.assertEqual(self.handler.path, '/') |
| 730 | self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 731 | self.assertSequenceEqual(self.handler.headers.items(), ()) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 732 | |
| 733 | def test_http_0_9(self): |
| 734 | result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
| 735 | self.assertEqual(len(result), 1) |
| 736 | self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
| 737 | self.verify_get_called() |
| 738 | |
| 739 | def test_with_continue_1_0(self): |
| 740 | result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 741 | self.verify_http_server_response(result[0]) |
| 742 | self.verify_expected_headers(result[1:-1]) |
| 743 | self.verify_get_called() |
| 744 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 745 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 746 | self.assertEqual(self.handler.command, 'GET') |
| 747 | self.assertEqual(self.handler.path, '/') |
| 748 | self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 749 | headers = (("Expect", "100-continue"),) |
| 750 | self.assertSequenceEqual(self.handler.headers.items(), headers) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 751 | |
| 752 | def test_with_continue_1_1(self): |
| 753 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 754 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n') |
Benjamin Peterson | 0442423 | 2014-01-18 21:50:18 -0500 | [diff] [blame] | 755 | self.assertEqual(result[1], b'\r\n') |
| 756 | self.assertEqual(result[2], b'HTTP/1.1 200 OK\r\n') |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 757 | self.verify_expected_headers(result[2:-1]) |
| 758 | self.verify_get_called() |
| 759 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 760 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 761 | self.assertEqual(self.handler.command, 'GET') |
| 762 | self.assertEqual(self.handler.path, '/') |
| 763 | self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 764 | headers = (("Expect", "100-continue"),) |
| 765 | self.assertSequenceEqual(self.handler.headers.items(), headers) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 766 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 767 | def test_header_buffering_of_send_error(self): |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 768 | |
| 769 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 770 | output = AuditableBytesIO() |
| 771 | handler = SocketlessRequestHandler() |
| 772 | handler.rfile = input |
| 773 | handler.wfile = output |
| 774 | handler.request_version = 'HTTP/1.1' |
| 775 | handler.requestline = '' |
| 776 | handler.command = None |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 777 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 778 | handler.send_error(418) |
| 779 | self.assertEqual(output.numWrites, 2) |
| 780 | |
| 781 | def test_header_buffering_of_send_response_only(self): |
| 782 | |
| 783 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 784 | output = AuditableBytesIO() |
| 785 | handler = SocketlessRequestHandler() |
| 786 | handler.rfile = input |
| 787 | handler.wfile = output |
| 788 | handler.request_version = 'HTTP/1.1' |
| 789 | |
| 790 | handler.send_response_only(418) |
| 791 | self.assertEqual(output.numWrites, 0) |
| 792 | handler.end_headers() |
| 793 | self.assertEqual(output.numWrites, 1) |
| 794 | |
| 795 | def test_header_buffering_of_send_header(self): |
| 796 | |
| 797 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 798 | output = AuditableBytesIO() |
| 799 | handler = SocketlessRequestHandler() |
| 800 | handler.rfile = input |
| 801 | handler.wfile = output |
| 802 | handler.request_version = 'HTTP/1.1' |
| 803 | |
| 804 | handler.send_header('Foo', 'foo') |
| 805 | handler.send_header('bar', 'bar') |
| 806 | self.assertEqual(output.numWrites, 0) |
| 807 | handler.end_headers() |
| 808 | self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n') |
| 809 | self.assertEqual(output.numWrites, 1) |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 810 | |
| 811 | def test_header_unbuffered_when_continue(self): |
| 812 | |
| 813 | def _readAndReseek(f): |
| 814 | pos = f.tell() |
| 815 | f.seek(0) |
| 816 | data = f.read() |
| 817 | f.seek(pos) |
| 818 | return data |
| 819 | |
| 820 | input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 821 | output = BytesIO() |
| 822 | self.handler.rfile = input |
| 823 | self.handler.wfile = output |
| 824 | self.handler.request_version = 'HTTP/1.1' |
| 825 | |
| 826 | self.handler.handle_one_request() |
| 827 | self.assertNotEqual(_readAndReseek(output), b'') |
| 828 | result = _readAndReseek(output).split(b'\r\n') |
| 829 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue') |
Benjamin Peterson | 0442423 | 2014-01-18 21:50:18 -0500 | [diff] [blame] | 830 | self.assertEqual(result[1], b'') |
| 831 | self.assertEqual(result[2], b'HTTP/1.1 200 OK') |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 832 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 833 | def test_with_continue_rejected(self): |
| 834 | usual_handler = self.handler # Save to avoid breaking any subsequent tests. |
| 835 | self.handler = RejectingSocketlessRequestHandler() |
| 836 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 837 | self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n') |
| 838 | self.verify_expected_headers(result[1:-1]) |
| 839 | # The expect handler should short circuit the usual get method by |
| 840 | # returning false here, so get_called should be false |
| 841 | self.assertFalse(self.handler.get_called) |
| 842 | self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1) |
| 843 | self.handler = usual_handler # Restore to avoid breaking any subsequent tests. |
| 844 | |
Antoine Pitrou | c492437 | 2010-12-16 16:48:36 +0000 | [diff] [blame] | 845 | def test_request_length(self): |
| 846 | # Issue #10714: huge request lines are discarded, to avoid Denial |
| 847 | # of Service attacks. |
| 848 | result = self.send_typical_request(b'GET ' + b'x' * 65537) |
| 849 | self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n') |
| 850 | self.assertFalse(self.handler.get_called) |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 851 | self.assertIsInstance(self.handler.requestline, str) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 852 | |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 853 | def test_header_length(self): |
| 854 | # Issue #6791: same for headers |
| 855 | result = self.send_typical_request( |
| 856 | b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n') |
| 857 | self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n') |
| 858 | self.assertFalse(self.handler.get_called) |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 859 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 860 | |
| 861 | def test_close_connection(self): |
| 862 | # handle_one_request() should be repeatedly called until |
| 863 | # it sets close_connection |
| 864 | def handle_one_request(): |
| 865 | self.handler.close_connection = next(close_values) |
| 866 | self.handler.handle_one_request = handle_one_request |
| 867 | |
| 868 | close_values = iter((True,)) |
| 869 | self.handler.handle() |
| 870 | self.assertRaises(StopIteration, next, close_values) |
| 871 | |
| 872 | close_values = iter((False, False, True)) |
| 873 | self.handler.handle() |
| 874 | self.assertRaises(StopIteration, next, close_values) |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 875 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 876 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 877 | """ Test url parsing """ |
| 878 | def setUp(self): |
| 879 | self.translated = os.getcwd() |
| 880 | self.translated = os.path.join(self.translated, 'filename') |
| 881 | self.handler = SocketlessRequestHandler() |
| 882 | |
| 883 | def test_query_arguments(self): |
| 884 | path = self.handler.translate_path('/filename') |
| 885 | self.assertEqual(path, self.translated) |
| 886 | path = self.handler.translate_path('/filename?foo=bar') |
| 887 | self.assertEqual(path, self.translated) |
| 888 | path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
| 889 | self.assertEqual(path, self.translated) |
| 890 | |
| 891 | def test_start_with_double_slash(self): |
| 892 | path = self.handler.translate_path('//filename') |
| 893 | self.assertEqual(path, self.translated) |
| 894 | path = self.handler.translate_path('//filename?foo=bar') |
| 895 | self.assertEqual(path, self.translated) |
| 896 | |
| 897 | |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 898 | class MiscTestCase(unittest.TestCase): |
| 899 | def test_all(self): |
| 900 | expected = [] |
| 901 | blacklist = {'executable', 'nobody_uid', 'test'} |
| 902 | for name in dir(server): |
| 903 | if name.startswith('_') or name in blacklist: |
| 904 | continue |
| 905 | module_object = getattr(server, name) |
| 906 | if getattr(module_object, '__module__', None) == 'http.server': |
| 907 | expected.append(name) |
| 908 | self.assertCountEqual(server.__all__, expected) |
| 909 | |
| 910 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 911 | def test_main(verbose=None): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 912 | cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 913 | try: |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 914 | support.run_unittest( |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 915 | RequestHandlerLoggingTestCase, |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 916 | BaseHTTPRequestHandlerTestCase, |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 917 | BaseHTTPServerTestCase, |
| 918 | SimpleHTTPServerTestCase, |
| 919 | CGIHTTPServerTestCase, |
| 920 | SimpleHTTPRequestHandlerTestCase, |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 921 | MiscTestCase, |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 922 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 923 | finally: |
| 924 | os.chdir(cwd) |
| 925 | |
| 926 | if __name__ == '__main__': |
| 927 | test_main() |