Georg Brandl | f899dfa | 2008-05-18 09:12:20 +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 | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 7 | import os |
| 8 | import sys |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 9 | import re |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 10 | import base64 |
| 11 | import shutil |
| 12 | import urllib |
| 13 | import httplib |
| 14 | import tempfile |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 15 | import unittest |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 16 | import CGIHTTPServer |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 17 | |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 18 | |
| 19 | from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 20 | from SimpleHTTPServer import SimpleHTTPRequestHandler |
| 21 | from CGIHTTPServer import CGIHTTPRequestHandler |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 22 | from StringIO import StringIO |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 23 | from test import test_support |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 24 | |
| 25 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 26 | threading = test_support.import_module('threading') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class NoLogRequestHandler: |
| 30 | def log_message(self, *args): |
| 31 | # don't write log messages to stderr |
| 32 | pass |
| 33 | |
Antoine Pitrou | 2623ae9 | 2010-12-16 17:18:49 +0000 | [diff] [blame] | 34 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 35 | def __init__(self): |
| 36 | self.get_called = False |
| 37 | self.protocol_version = "HTTP/1.1" |
| 38 | |
| 39 | def do_GET(self): |
| 40 | self.get_called = True |
| 41 | self.send_response(200) |
| 42 | self.send_header('Content-Type', 'text/html') |
| 43 | self.end_headers() |
Antoine Pitrou | 2623ae9 | 2010-12-16 17:18:49 +0000 | [diff] [blame] | 44 | self.wfile.write(b'<html><body>Data</body></html>\r\n') |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 45 | |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 46 | def log_message(self, fmt, *args): |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 47 | pass |
| 48 | |
| 49 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 50 | class TestServerThread(threading.Thread): |
| 51 | def __init__(self, test_object, request_handler): |
| 52 | threading.Thread.__init__(self) |
| 53 | self.request_handler = request_handler |
| 54 | self.test_object = test_object |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 55 | |
| 56 | def run(self): |
| 57 | self.server = HTTPServer(('', 0), self.request_handler) |
| 58 | self.test_object.PORT = self.server.socket.getsockname()[1] |
Antoine Pitrou | 1ca8c19 | 2010-04-25 21:15:50 +0000 | [diff] [blame] | 59 | self.test_object.server_started.set() |
| 60 | self.test_object = None |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 61 | try: |
Antoine Pitrou | 1ca8c19 | 2010-04-25 21:15:50 +0000 | [diff] [blame] | 62 | self.server.serve_forever(0.05) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 63 | finally: |
| 64 | self.server.server_close() |
| 65 | |
| 66 | def stop(self): |
| 67 | self.server.shutdown() |
| 68 | |
| 69 | |
| 70 | class BaseTestCase(unittest.TestCase): |
| 71 | def setUp(self): |
Antoine Pitrou | 85bd587 | 2009-10-27 18:50:52 +0000 | [diff] [blame] | 72 | self._threads = test_support.threading_setup() |
Nick Coghlan | 87c03b3 | 2009-10-17 15:23:08 +0000 | [diff] [blame] | 73 | os.environ = test_support.EnvironmentVarGuard() |
Antoine Pitrou | 1ca8c19 | 2010-04-25 21:15:50 +0000 | [diff] [blame] | 74 | self.server_started = threading.Event() |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 75 | self.thread = TestServerThread(self, self.request_handler) |
| 76 | self.thread.start() |
Antoine Pitrou | 1ca8c19 | 2010-04-25 21:15:50 +0000 | [diff] [blame] | 77 | self.server_started.wait() |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 78 | |
| 79 | def tearDown(self): |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 80 | self.thread.stop() |
Nick Coghlan | 87c03b3 | 2009-10-17 15:23:08 +0000 | [diff] [blame] | 81 | os.environ.__exit__() |
Antoine Pitrou | 85bd587 | 2009-10-27 18:50:52 +0000 | [diff] [blame] | 82 | test_support.threading_cleanup(*self._threads) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 83 | |
| 84 | def request(self, uri, method='GET', body=None, headers={}): |
| 85 | self.connection = httplib.HTTPConnection('localhost', self.PORT) |
| 86 | self.connection.request(method, uri, body, headers) |
| 87 | return self.connection.getresponse() |
| 88 | |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 89 | class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 90 | """Test the functionality of the BaseHTTPServer focussing on |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 91 | BaseHTTPRequestHandler. |
| 92 | """ |
| 93 | |
| 94 | HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK') |
| 95 | |
| 96 | def setUp (self): |
| 97 | self.handler = SocketlessRequestHandler() |
| 98 | |
| 99 | def send_typical_request(self, message): |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 100 | input_msg = StringIO(message) |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 101 | output = StringIO() |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 102 | self.handler.rfile = input_msg |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 103 | self.handler.wfile = output |
| 104 | self.handler.handle_one_request() |
| 105 | output.seek(0) |
| 106 | return output.readlines() |
| 107 | |
| 108 | def verify_get_called(self): |
| 109 | self.assertTrue(self.handler.get_called) |
| 110 | |
| 111 | def verify_expected_headers(self, headers): |
| 112 | for fieldName in 'Server: ', 'Date: ', 'Content-Type: ': |
| 113 | self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 114 | |
| 115 | def verify_http_server_response(self, response): |
| 116 | match = self.HTTPResponseMatch.search(response) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 117 | self.assertIsNotNone(match) |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 118 | |
| 119 | def test_http_1_1(self): |
| 120 | result = self.send_typical_request('GET / HTTP/1.1\r\n\r\n') |
| 121 | self.verify_http_server_response(result[0]) |
| 122 | self.verify_expected_headers(result[1:-1]) |
| 123 | self.verify_get_called() |
| 124 | self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
| 125 | |
| 126 | def test_http_1_0(self): |
| 127 | result = self.send_typical_request('GET / HTTP/1.0\r\n\r\n') |
| 128 | self.verify_http_server_response(result[0]) |
| 129 | self.verify_expected_headers(result[1:-1]) |
| 130 | self.verify_get_called() |
| 131 | self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
| 132 | |
| 133 | def test_http_0_9(self): |
| 134 | result = self.send_typical_request('GET / HTTP/0.9\r\n\r\n') |
| 135 | self.assertEqual(len(result), 1) |
| 136 | self.assertEqual(result[0], '<html><body>Data</body></html>\r\n') |
| 137 | self.verify_get_called() |
| 138 | |
| 139 | def test_with_continue_1_0(self): |
| 140 | result = self.send_typical_request('GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 141 | self.verify_http_server_response(result[0]) |
| 142 | self.verify_expected_headers(result[1:-1]) |
| 143 | self.verify_get_called() |
| 144 | self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
| 145 | |
Antoine Pitrou | 2623ae9 | 2010-12-16 17:18:49 +0000 | [diff] [blame] | 146 | def test_request_length(self): |
| 147 | # Issue #10714: huge request lines are discarded, to avoid Denial |
| 148 | # of Service attacks. |
| 149 | result = self.send_typical_request(b'GET ' + b'x' * 65537) |
| 150 | self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n') |
| 151 | self.assertFalse(self.handler.get_called) |
| 152 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 153 | |
| 154 | class BaseHTTPServerTestCase(BaseTestCase): |
| 155 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 156 | protocol_version = 'HTTP/1.1' |
| 157 | default_request_version = 'HTTP/1.1' |
| 158 | |
| 159 | def do_TEST(self): |
| 160 | self.send_response(204) |
| 161 | self.send_header('Content-Type', 'text/html') |
| 162 | self.send_header('Connection', 'close') |
| 163 | self.end_headers() |
| 164 | |
| 165 | def do_KEEP(self): |
| 166 | self.send_response(204) |
| 167 | self.send_header('Content-Type', 'text/html') |
| 168 | self.send_header('Connection', 'keep-alive') |
| 169 | self.end_headers() |
| 170 | |
| 171 | def do_KEYERROR(self): |
| 172 | self.send_error(999) |
| 173 | |
| 174 | def do_CUSTOM(self): |
| 175 | self.send_response(999) |
| 176 | self.send_header('Content-Type', 'text/html') |
| 177 | self.send_header('Connection', 'close') |
| 178 | self.end_headers() |
| 179 | |
| 180 | def setUp(self): |
| 181 | BaseTestCase.setUp(self) |
| 182 | self.con = httplib.HTTPConnection('localhost', self.PORT) |
| 183 | self.con.connect() |
| 184 | |
| 185 | def test_command(self): |
| 186 | self.con.request('GET', '/') |
| 187 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 188 | self.assertEqual(res.status, 501) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 189 | |
| 190 | def test_request_line_trimming(self): |
| 191 | self.con._http_vsn_str = 'HTTP/1.1\n' |
R David Murray | 3eb76fc | 2014-06-24 16:49:24 -0400 | [diff] [blame] | 192 | self.con.putrequest('XYZBOGUS', '/') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 193 | self.con.endheaders() |
| 194 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 195 | self.assertEqual(res.status, 501) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 196 | |
| 197 | def test_version_bogus(self): |
| 198 | self.con._http_vsn_str = 'FUBAR' |
| 199 | self.con.putrequest('GET', '/') |
| 200 | self.con.endheaders() |
| 201 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 202 | self.assertEqual(res.status, 400) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 203 | |
| 204 | def test_version_digits(self): |
| 205 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 206 | self.con.putrequest('GET', '/') |
| 207 | self.con.endheaders() |
| 208 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 209 | self.assertEqual(res.status, 400) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 210 | |
| 211 | def test_version_none_get(self): |
| 212 | self.con._http_vsn_str = '' |
| 213 | self.con.putrequest('GET', '/') |
| 214 | self.con.endheaders() |
| 215 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 216 | self.assertEqual(res.status, 501) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 217 | |
| 218 | def test_version_none(self): |
R David Murray | 3eb76fc | 2014-06-24 16:49:24 -0400 | [diff] [blame] | 219 | # Test that a valid method is rejected when not HTTP/1.x |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 220 | self.con._http_vsn_str = '' |
R David Murray | 3eb76fc | 2014-06-24 16:49:24 -0400 | [diff] [blame] | 221 | self.con.putrequest('CUSTOM', '/') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 222 | self.con.endheaders() |
| 223 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 224 | self.assertEqual(res.status, 400) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 225 | |
| 226 | def test_version_invalid(self): |
| 227 | self.con._http_vsn = 99 |
| 228 | self.con._http_vsn_str = 'HTTP/9.9' |
| 229 | self.con.putrequest('GET', '/') |
| 230 | self.con.endheaders() |
| 231 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 232 | self.assertEqual(res.status, 505) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 233 | |
| 234 | def test_send_blank(self): |
| 235 | self.con._http_vsn_str = '' |
| 236 | self.con.putrequest('', '') |
| 237 | self.con.endheaders() |
| 238 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 239 | self.assertEqual(res.status, 400) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 240 | |
| 241 | def test_header_close(self): |
| 242 | self.con.putrequest('GET', '/') |
| 243 | self.con.putheader('Connection', 'close') |
| 244 | self.con.endheaders() |
| 245 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 246 | self.assertEqual(res.status, 501) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 247 | |
| 248 | def test_head_keep_alive(self): |
| 249 | self.con._http_vsn_str = 'HTTP/1.1' |
| 250 | self.con.putrequest('GET', '/') |
| 251 | self.con.putheader('Connection', 'keep-alive') |
| 252 | self.con.endheaders() |
| 253 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 254 | self.assertEqual(res.status, 501) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 255 | |
| 256 | def test_handler(self): |
| 257 | self.con.request('TEST', '/') |
| 258 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 259 | self.assertEqual(res.status, 204) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 260 | |
| 261 | def test_return_header_keep_alive(self): |
| 262 | self.con.request('KEEP', '/') |
| 263 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 264 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 265 | self.con.request('TEST', '/') |
Brian Curtin | 55b6251 | 2010-10-31 00:36:01 +0000 | [diff] [blame] | 266 | self.addCleanup(self.con.close) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 267 | |
| 268 | def test_internal_key_error(self): |
| 269 | self.con.request('KEYERROR', '/') |
| 270 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 271 | self.assertEqual(res.status, 999) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 272 | |
| 273 | def test_return_custom_status(self): |
| 274 | self.con.request('CUSTOM', '/') |
| 275 | res = self.con.getresponse() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 276 | self.assertEqual(res.status, 999) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 277 | |
| 278 | |
| 279 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 280 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 281 | pass |
| 282 | |
| 283 | def setUp(self): |
| 284 | BaseTestCase.setUp(self) |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 285 | self.cwd = os.getcwd() |
| 286 | basetempdir = tempfile.gettempdir() |
| 287 | os.chdir(basetempdir) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 288 | self.data = 'We are the knights who say Ni!' |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 289 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 290 | self.tempdir_name = os.path.basename(self.tempdir) |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 291 | temp = open(os.path.join(self.tempdir, 'test'), 'wb') |
| 292 | temp.write(self.data) |
| 293 | temp.close() |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 294 | |
| 295 | def tearDown(self): |
| 296 | try: |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 297 | os.chdir(self.cwd) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 298 | try: |
| 299 | shutil.rmtree(self.tempdir) |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 300 | except OSError: |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 301 | pass |
| 302 | finally: |
| 303 | BaseTestCase.tearDown(self) |
| 304 | |
| 305 | def check_status_and_reason(self, response, status, data=None): |
| 306 | body = response.read() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 307 | self.assertTrue(response) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 308 | self.assertEqual(response.status, status) |
| 309 | self.assertIsNotNone(response.reason) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 310 | if data: |
| 311 | self.assertEqual(data, body) |
| 312 | |
| 313 | def test_get(self): |
| 314 | #constructs the path relative to the root directory of the HTTPServer |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 315 | response = self.request(self.tempdir_name + '/test') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 316 | self.check_status_and_reason(response, 200, data=self.data) |
Senthil Kumaran | d4fac04 | 2013-09-13 00:18:55 -0700 | [diff] [blame] | 317 | # check for trailing "/" which should return 404. See Issue17324 |
| 318 | response = self.request(self.tempdir_name + '/test/') |
| 319 | self.check_status_and_reason(response, 404) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 320 | response = self.request(self.tempdir_name + '/') |
| 321 | self.check_status_and_reason(response, 200) |
| 322 | response = self.request(self.tempdir_name) |
| 323 | self.check_status_and_reason(response, 301) |
| 324 | response = self.request('/ThisDoesNotExist') |
| 325 | self.check_status_and_reason(response, 404) |
| 326 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
| 327 | self.check_status_and_reason(response, 404) |
Benjamin Peterson | 3c0027b | 2014-04-04 13:59:33 -0400 | [diff] [blame] | 328 | with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as fp: |
| 329 | response = self.request('/' + self.tempdir_name + '/') |
| 330 | self.check_status_and_reason(response, 200) |
| 331 | # chmod() doesn't work as expected on Windows, and filesystem |
| 332 | # permissions are ignored by root on Unix. |
| 333 | if os.name == 'posix' and os.geteuid() != 0: |
| 334 | os.chmod(self.tempdir, 0) |
| 335 | response = self.request(self.tempdir_name + '/') |
| 336 | self.check_status_and_reason(response, 404) |
| 337 | os.chmod(self.tempdir, 0755) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 338 | |
| 339 | def test_head(self): |
| 340 | response = self.request( |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 341 | self.tempdir_name + '/test', method='HEAD') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 342 | self.check_status_and_reason(response, 200) |
| 343 | self.assertEqual(response.getheader('content-length'), |
| 344 | str(len(self.data))) |
| 345 | self.assertEqual(response.getheader('content-type'), |
| 346 | 'application/octet-stream') |
| 347 | |
| 348 | def test_invalid_requests(self): |
| 349 | response = self.request('/', method='FOO') |
| 350 | self.check_status_and_reason(response, 501) |
| 351 | # requests must be case sensitive,so this should fail too |
| 352 | response = self.request('/', method='get') |
| 353 | self.check_status_and_reason(response, 501) |
| 354 | response = self.request('/', method='GETs') |
| 355 | self.check_status_and_reason(response, 501) |
| 356 | |
| 357 | |
| 358 | cgi_file1 = """\ |
| 359 | #!%s |
| 360 | |
| 361 | print "Content-type: text/html" |
| 362 | print |
| 363 | print "Hello World" |
| 364 | """ |
| 365 | |
| 366 | cgi_file2 = """\ |
| 367 | #!%s |
| 368 | import cgi |
| 369 | |
| 370 | print "Content-type: text/html" |
| 371 | print |
| 372 | |
| 373 | form = cgi.FieldStorage() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 374 | print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 375 | form.getfirst("bacon")) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 376 | """ |
| 377 | |
Charles-François Natali | 09f8714 | 2011-11-02 19:32:54 +0100 | [diff] [blame] | 378 | |
| 379 | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
| 380 | "This test can't be run reliably as root (issue #13308).") |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 381 | class CGIHTTPServerTestCase(BaseTestCase): |
| 382 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 383 | pass |
| 384 | |
| 385 | def setUp(self): |
| 386 | BaseTestCase.setUp(self) |
| 387 | self.parent_dir = tempfile.mkdtemp() |
| 388 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
Ned Deily | c893762 | 2014-07-12 22:01:15 -0700 | [diff] [blame^] | 389 | self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 390 | os.mkdir(self.cgi_dir) |
Ned Deily | c893762 | 2014-07-12 22:01:15 -0700 | [diff] [blame^] | 391 | os.mkdir(self.cgi_child_dir) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 392 | |
Florent Xicluna | 0805e6e | 2010-03-22 17:18:18 +0000 | [diff] [blame] | 393 | # The shebang line should be pure ASCII: use symlink if possible. |
| 394 | # See issue #7668. |
| 395 | if hasattr(os, 'symlink'): |
| 396 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
| 397 | os.symlink(sys.executable, self.pythonexe) |
| 398 | else: |
| 399 | self.pythonexe = sys.executable |
| 400 | |
Benjamin Peterson | 1ef959a | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 401 | self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py') |
| 402 | with open(self.nocgi_path, 'w') as fp: |
| 403 | fp.write(cgi_file1 % self.pythonexe) |
| 404 | os.chmod(self.nocgi_path, 0777) |
| 405 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 406 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
| 407 | with open(self.file1_path, 'w') as file1: |
Florent Xicluna | 0805e6e | 2010-03-22 17:18:18 +0000 | [diff] [blame] | 408 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 409 | os.chmod(self.file1_path, 0777) |
| 410 | |
| 411 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
| 412 | with open(self.file2_path, 'w') as file2: |
Florent Xicluna | 0805e6e | 2010-03-22 17:18:18 +0000 | [diff] [blame] | 413 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 414 | os.chmod(self.file2_path, 0777) |
| 415 | |
Ned Deily | c893762 | 2014-07-12 22:01:15 -0700 | [diff] [blame^] | 416 | self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py') |
| 417 | with open(self.file3_path, 'w') as file3: |
| 418 | file3.write(cgi_file1 % self.pythonexe) |
| 419 | os.chmod(self.file3_path, 0777) |
| 420 | |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 421 | self.cwd = os.getcwd() |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 422 | os.chdir(self.parent_dir) |
| 423 | |
| 424 | def tearDown(self): |
| 425 | try: |
Georg Brandl | 7bb1653 | 2008-05-20 06:47:31 +0000 | [diff] [blame] | 426 | os.chdir(self.cwd) |
Florent Xicluna | 0805e6e | 2010-03-22 17:18:18 +0000 | [diff] [blame] | 427 | if self.pythonexe != sys.executable: |
| 428 | os.remove(self.pythonexe) |
Benjamin Peterson | 1ef959a | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 429 | os.remove(self.nocgi_path) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 430 | os.remove(self.file1_path) |
| 431 | os.remove(self.file2_path) |
Ned Deily | c893762 | 2014-07-12 22:01:15 -0700 | [diff] [blame^] | 432 | os.remove(self.file3_path) |
| 433 | os.rmdir(self.cgi_child_dir) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 434 | os.rmdir(self.cgi_dir) |
| 435 | os.rmdir(self.parent_dir) |
| 436 | finally: |
| 437 | BaseTestCase.tearDown(self) |
| 438 | |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 439 | def test_url_collapse_path(self): |
Senthil Kumaran | fb2e874 | 2012-04-11 03:07:57 +0800 | [diff] [blame] | 440 | # verify tail is the last portion and head is the rest on proper urls |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 441 | test_vectors = { |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 442 | '': '//', |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 443 | '..': IndexError, |
| 444 | '/.//..': IndexError, |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 445 | '/': '//', |
| 446 | '//': '//', |
| 447 | '/\\': '//\\', |
| 448 | '/.//': '//', |
| 449 | 'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 450 | '/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 451 | 'a': '//a', |
| 452 | '/a': '//a', |
| 453 | '//a': '//a', |
| 454 | './a': '//a', |
| 455 | './C:/': '/C:/', |
| 456 | '/a/b': '/a/b', |
| 457 | '/a/b/': '/a/b/', |
| 458 | '/a/b/.': '/a/b/', |
| 459 | '/a/b/c/..': '/a/b/', |
| 460 | '/a/b/c/../d': '/a/b/d', |
| 461 | '/a/b/c/../d/e/../f': '/a/b/d/f', |
| 462 | '/a/b/c/../d/e/../../f': '/a/b/f', |
| 463 | '/a/b/c/../d/e/.././././..//f': '/a/b/f', |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 464 | '../a/b/c/../d/e/.././././..//f': IndexError, |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 465 | '/a/b/c/../d/e/../../../f': '/a/f', |
| 466 | '/a/b/c/../d/e/../../../../f': '//f', |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 467 | '/a/b/c/../d/e/../../../../../f': IndexError, |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 468 | '/a/b/c/../d/e/../../../../f/..': '//', |
| 469 | '/a/b/c/../d/e/../../../../f/../.': '//', |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 470 | } |
| 471 | for path, expected in test_vectors.iteritems(): |
| 472 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 473 | self.assertRaises(expected, |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 474 | CGIHTTPServer._url_collapse_path, path) |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 475 | else: |
Senthil Kumaran | 5f7e734 | 2012-04-12 02:23:23 +0800 | [diff] [blame] | 476 | actual = CGIHTTPServer._url_collapse_path(path) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 477 | self.assertEqual(expected, actual, |
| 478 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 479 | (path, actual, expected)) |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 480 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 481 | def test_headers_and_content(self): |
| 482 | res = self.request('/cgi-bin/file1.py') |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 483 | self.assertEqual(('Hello World\n', 'text/html', 200), |
| 484 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 485 | |
Benjamin Peterson | 1ef959a | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 486 | def test_issue19435(self): |
| 487 | res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh') |
| 488 | self.assertEqual(res.status, 404) |
| 489 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 490 | def test_post(self): |
| 491 | params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
| 492 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 493 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 494 | |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 495 | self.assertEqual(res.read(), '1, python, 123456\n') |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 496 | |
| 497 | def test_invaliduri(self): |
| 498 | res = self.request('/cgi-bin/invalid') |
| 499 | res.read() |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 500 | self.assertEqual(res.status, 404) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 501 | |
| 502 | def test_authorization(self): |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 503 | headers = {'Authorization' : 'Basic %s' % |
| 504 | base64.b64encode('username:pass')} |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 505 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 506 | self.assertEqual(('Hello World\n', 'text/html', 200), |
| 507 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 508 | |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 509 | def test_no_leading_slash(self): |
| 510 | # http://bugs.python.org/issue2254 |
| 511 | res = self.request('cgi-bin/file1.py') |
Florent Xicluna | bc27c6a | 2010-03-19 18:34:55 +0000 | [diff] [blame] | 512 | self.assertEqual(('Hello World\n', 'text/html', 200), |
Gregory P. Smith | 923ba36 | 2009-04-06 06:33:26 +0000 | [diff] [blame] | 513 | (res.read(), res.getheader('Content-type'), res.status)) |
| 514 | |
Senthil Kumaran | a9bd0cc | 2010-10-03 18:16:52 +0000 | [diff] [blame] | 515 | def test_os_environ_is_not_altered(self): |
| 516 | signature = "Test CGI Server" |
| 517 | os.environ['SERVER_SOFTWARE'] = signature |
| 518 | res = self.request('/cgi-bin/file1.py') |
| 519 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 520 | (res.read(), res.getheader('Content-type'), res.status)) |
| 521 | self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 522 | |
Benjamin Peterson | 8d24d77 | 2014-06-14 18:36:29 -0700 | [diff] [blame] | 523 | def test_urlquote_decoding_in_cgi_check(self): |
| 524 | res = self.request('/cgi-bin%2ffile1.py') |
| 525 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 526 | (res.read(), res.getheader('Content-type'), res.status)) |
| 527 | |
Ned Deily | c893762 | 2014-07-12 22:01:15 -0700 | [diff] [blame^] | 528 | def test_nested_cgi_path_issue21323(self): |
| 529 | res = self.request('/cgi-bin/child-dir/file3.py') |
| 530 | self.assertEqual((b'Hello World\n', 'text/html', 200), |
| 531 | (res.read(), res.getheader('Content-type'), res.status)) |
| 532 | |
Antoine Pitrou | 47d9b0e | 2010-12-16 17:11:34 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | 47d9b0e | 2010-12-16 17:11:34 +0000 | [diff] [blame] | 534 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 535 | """ Test url parsing """ |
| 536 | def setUp(self): |
| 537 | self.translated = os.getcwd() |
| 538 | self.translated = os.path.join(self.translated, 'filename') |
| 539 | self.handler = SocketlessRequestHandler() |
| 540 | |
| 541 | def test_query_arguments(self): |
| 542 | path = self.handler.translate_path('/filename') |
| 543 | self.assertEqual(path, self.translated) |
| 544 | path = self.handler.translate_path('/filename?foo=bar') |
| 545 | self.assertEqual(path, self.translated) |
| 546 | path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
| 547 | self.assertEqual(path, self.translated) |
| 548 | |
| 549 | def test_start_with_double_slash(self): |
| 550 | path = self.handler.translate_path('//filename') |
| 551 | self.assertEqual(path, self.translated) |
| 552 | path = self.handler.translate_path('//filename?foo=bar') |
| 553 | self.assertEqual(path, self.translated) |
| 554 | |
| 555 | |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 556 | def test_main(verbose=None): |
| 557 | try: |
| 558 | cwd = os.getcwd() |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 559 | test_support.run_unittest(BaseHTTPRequestHandlerTestCase, |
Antoine Pitrou | 47d9b0e | 2010-12-16 17:11:34 +0000 | [diff] [blame] | 560 | SimpleHTTPRequestHandlerTestCase, |
Senthil Kumaran | b643fc6 | 2010-09-30 06:40:56 +0000 | [diff] [blame] | 561 | BaseHTTPServerTestCase, |
| 562 | SimpleHTTPServerTestCase, |
| 563 | CGIHTTPServerTestCase |
| 564 | ) |
Georg Brandl | f899dfa | 2008-05-18 09:12:20 +0000 | [diff] [blame] | 565 | finally: |
| 566 | os.chdir(cwd) |
| 567 | |
| 568 | if __name__ == '__main__': |
| 569 | test_main() |