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