annotate Lib/test/test_httpservers.py @ 60961:5ac0aae28f4c legacy-trunk

Replace a Lock with a better suited Event.
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 25 Apr 2010 21:15:50 +0000
parents f0014554166f
children 3a0484200034
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 """Unittests for the various HTTPServer modules.
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 """
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
6
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 from SimpleHTTPServer import SimpleHTTPRequestHandler
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
9 from CGIHTTPServer import CGIHTTPRequestHandler
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
10 import CGIHTTPServer
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
11
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
12 import os
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 import sys
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 import base64
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 import shutil
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 import urllib
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 import httplib
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
18 import tempfile
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 import threading
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
20
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 import unittest
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
22 from test import test_support
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
23
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
24
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 class NoLogRequestHandler:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 def log_message(self, *args):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 # don't write log messages to stderr
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
29
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
30
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 class TestServerThread(threading.Thread):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
32 def __init__(self, test_object, request_handler):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 threading.Thread.__init__(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
34 self.request_handler = request_handler
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 self.test_object = test_object
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
36
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 def run(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 self.server = HTTPServer(('', 0), self.request_handler)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 self.test_object.PORT = self.server.socket.getsockname()[1]
60961
5ac0aae28f4c Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59914
diff changeset
40 self.test_object.server_started.set()
5ac0aae28f4c Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59914
diff changeset
41 self.test_object = None
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
42 try:
60961
5ac0aae28f4c Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59914
diff changeset
43 self.server.serve_forever(0.05)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 self.server.server_close()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
46
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 def stop(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 self.server.shutdown()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
49
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
50
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 class BaseTestCase(unittest.TestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 def setUp(self):
56870
269f1d9c86cd (Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56565
diff changeset
53 self._threads = test_support.threading_setup()
56564
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
54 os.environ = test_support.EnvironmentVarGuard()
60961
5ac0aae28f4c Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59914
diff changeset
55 self.server_started = threading.Event()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
56 self.thread = TestServerThread(self, self.request_handler)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 self.thread.start()
60961
5ac0aae28f4c Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59914
diff changeset
58 self.server_started.wait()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
59
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
60 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 self.thread.stop()
56564
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
62 os.environ.__exit__()
56870
269f1d9c86cd (Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56565
diff changeset
63 test_support.threading_cleanup(*self._threads)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
64
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 def request(self, uri, method='GET', body=None, headers={}):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 self.connection = httplib.HTTPConnection('localhost', self.PORT)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 self.connection.request(method, uri, body, headers)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 return self.connection.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
69
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
70
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 class BaseHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
72 class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
73 protocol_version = 'HTTP/1.1'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
74 default_request_version = 'HTTP/1.1'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
75
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
76 def do_TEST(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
77 self.send_response(204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
78 self.send_header('Content-Type', 'text/html')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
79 self.send_header('Connection', 'close')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
81
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
82 def do_KEEP(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
83 self.send_response(204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 self.send_header('Content-Type', 'text/html')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 self.send_header('Connection', 'keep-alive')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
87
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 def do_KEYERROR(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 self.send_error(999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
90
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
91 def do_CUSTOM(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 self.send_response(999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 self.send_header('Content-Type', 'text/html')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 self.send_header('Connection', 'close')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
96
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
98 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
99 self.con = httplib.HTTPConnection('localhost', self.PORT)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 self.con.connect()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
101
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
102 def test_command(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
103 self.con.request('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
104 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
105 self.assertEqual(res.status, 501)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
106
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 def test_request_line_trimming(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 self.con._http_vsn_str = 'HTTP/1.1\n'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
109 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
110 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
111 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
112 self.assertEqual(res.status, 501)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
113
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
114 def test_version_bogus(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 self.con._http_vsn_str = 'FUBAR'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
116 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
118 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
119 self.assertEqual(res.status, 400)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
120
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
121 def test_version_digits(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
122 self.con._http_vsn_str = 'HTTP/9.9.9'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
123 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
124 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
125 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
126 self.assertEqual(res.status, 400)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
127
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
128 def test_version_none_get(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
129 self.con._http_vsn_str = ''
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
130 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
132 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
133 self.assertEqual(res.status, 501)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
134
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 def test_version_none(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 self.con._http_vsn_str = ''
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 self.con.putrequest('PUT', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
138 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
139 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
140 self.assertEqual(res.status, 400)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
141
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
142 def test_version_invalid(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 self.con._http_vsn = 99
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 self.con._http_vsn_str = 'HTTP/9.9'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
145 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
146 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
147 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
148 self.assertEqual(res.status, 505)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
149
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
150 def test_send_blank(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
151 self.con._http_vsn_str = ''
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
152 self.con.putrequest('', '')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
154 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
155 self.assertEqual(res.status, 400)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
156
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
157 def test_header_close(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
158 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
159 self.con.putheader('Connection', 'close')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
160 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
161 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
162 self.assertEqual(res.status, 501)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
163
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
164 def test_head_keep_alive(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
165 self.con._http_vsn_str = 'HTTP/1.1'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
166 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
167 self.con.putheader('Connection', 'keep-alive')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
168 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
169 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
170 self.assertEqual(res.status, 501)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
171
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
172 def test_handler(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
173 self.con.request('TEST', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
174 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
175 self.assertEqual(res.status, 204)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
176
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
177 def test_return_header_keep_alive(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
178 self.con.request('KEEP', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
179 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
180 self.assertEqual(res.getheader('Connection'), 'keep-alive')
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 self.con.request('TEST', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
182
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
183 def test_internal_key_error(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
184 self.con.request('KEYERROR', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
185 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
186 self.assertEqual(res.status, 999)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
187
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
188 def test_return_custom_status(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
189 self.con.request('CUSTOM', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
190 res = self.con.getresponse()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
191 self.assertEqual(res.status, 999)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
192
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
193
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
194 class SimpleHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
195 class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
196 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
197
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
198 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
199 BaseTestCase.setUp(self)
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
200 self.cwd = os.getcwd()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
201 basetempdir = tempfile.gettempdir()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
202 os.chdir(basetempdir)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
203 self.data = 'We are the knights who say Ni!'
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
204 self.tempdir = tempfile.mkdtemp(dir=basetempdir)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
205 self.tempdir_name = os.path.basename(self.tempdir)
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
206 temp = open(os.path.join(self.tempdir, 'test'), 'wb')
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
207 temp.write(self.data)
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
208 temp.close()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
209
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
212 os.chdir(self.cwd)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 shutil.rmtree(self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 except:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
216 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
217 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
219
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
220 def check_status_and_reason(self, response, status, data=None):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 body = response.read()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52929
diff changeset
222 self.assertTrue(response)
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
223 self.assertEqual(response.status, status)
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
224 self.assertIsNotNone(response.reason)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 if data:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 self.assertEqual(data, body)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
227
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
228 def test_get(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
229 #constructs the path relative to the root directory of the HTTPServer
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
230 response = self.request(self.tempdir_name + '/test')
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
231 self.check_status_and_reason(response, 200, data=self.data)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
232 response = self.request(self.tempdir_name + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
233 self.check_status_and_reason(response, 200)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 response = self.request(self.tempdir_name)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
235 self.check_status_and_reason(response, 301)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 response = self.request('/ThisDoesNotExist')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
237 self.check_status_and_reason(response, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
238 response = self.request('/' + 'ThisDoesNotExist' + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
239 self.check_status_and_reason(response, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 f = open(os.path.join(self.tempdir_name, 'index.html'), 'w')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
241 response = self.request('/' + self.tempdir_name + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
242 self.check_status_and_reason(response, 200)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
243 if os.name == 'posix':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
244 # chmod won't work as expected on Windows platforms
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
245 os.chmod(self.tempdir, 0)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
246 response = self.request(self.tempdir_name + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
247 self.check_status_and_reason(response, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
248 os.chmod(self.tempdir, 0755)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
249
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
250 def test_head(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
251 response = self.request(
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
252 self.tempdir_name + '/test', method='HEAD')
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
253 self.check_status_and_reason(response, 200)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
254 self.assertEqual(response.getheader('content-length'),
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
255 str(len(self.data)))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
256 self.assertEqual(response.getheader('content-type'),
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
257 'application/octet-stream')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
258
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
259 def test_invalid_requests(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
260 response = self.request('/', method='FOO')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
261 self.check_status_and_reason(response, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
262 # requests must be case sensitive,so this should fail too
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
263 response = self.request('/', method='get')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
264 self.check_status_and_reason(response, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
265 response = self.request('/', method='GETs')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
266 self.check_status_and_reason(response, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
267
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
268
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
269 cgi_file1 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
270 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
271
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
272 print "Content-type: text/html"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
273 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
274 print "Hello World"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
275 """
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
276
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
277 cgi_file2 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
278 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
279 import cgi
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
280
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
281 print "Content-type: text/html"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
282 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
283
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
284 form = cgi.FieldStorage()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
285 print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
286 form.getfirst("bacon"))
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
287 """
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
288
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
289 class CGIHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
290 class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
291 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
292
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
293 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
294 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
295 self.parent_dir = tempfile.mkdtemp()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
296 self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
297 os.mkdir(self.cgi_dir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
298
59914
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
299 # The shebang line should be pure ASCII: use symlink if possible.
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
300 # See issue #7668.
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
301 if hasattr(os, 'symlink'):
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
302 self.pythonexe = os.path.join(self.parent_dir, 'python')
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
303 os.symlink(sys.executable, self.pythonexe)
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
304 else:
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
305 self.pythonexe = sys.executable
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
306
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
307 self.file1_path = os.path.join(self.cgi_dir, 'file1.py')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
308 with open(self.file1_path, 'w') as file1:
59914
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
309 file1.write(cgi_file1 % self.pythonexe)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
310 os.chmod(self.file1_path, 0777)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
311
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
312 self.file2_path = os.path.join(self.cgi_dir, 'file2.py')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
313 with open(self.file2_path, 'w') as file2:
59914
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
314 file2.write(cgi_file2 % self.pythonexe)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
315 os.chmod(self.file2_path, 0777)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
316
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
317 self.cwd = os.getcwd()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
318 os.chdir(self.parent_dir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
319
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
320 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
321 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
322 os.chdir(self.cwd)
59914
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
323 if self.pythonexe != sys.executable:
f0014554166f #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59726
diff changeset
324 os.remove(self.pythonexe)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
325 os.remove(self.file1_path)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
326 os.remove(self.file2_path)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
327 os.rmdir(self.cgi_dir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
328 os.rmdir(self.parent_dir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
329 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
330 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
331
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
332 def test_url_collapse_path_split(self):
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
333 test_vectors = {
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
334 '': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
335 '..': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
336 '/.//..': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
337 '/': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
338 '//': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
339 '/\\': ('/', '\\'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
340 '/.//': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
341 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
342 '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
343 'a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
344 '/a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
345 '//a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
346 './a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
347 './C:/': ('/C:', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
348 '/a/b': ('/a', 'b'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
349 '/a/b/': ('/a/b', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
350 '/a/b/c/..': ('/a/b', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
351 '/a/b/c/../d': ('/a/b', 'd'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
352 '/a/b/c/../d/e/../f': ('/a/b/d', 'f'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
353 '/a/b/c/../d/e/../../f': ('/a/b', 'f'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
354 '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
355 '../a/b/c/../d/e/.././././..//f': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
356 '/a/b/c/../d/e/../../../f': ('/a', 'f'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
357 '/a/b/c/../d/e/../../../../f': ('/', 'f'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
358 '/a/b/c/../d/e/../../../../../f': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
359 '/a/b/c/../d/e/../../../../f/..': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
360 }
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
361 for path, expected in test_vectors.iteritems():
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
362 if isinstance(expected, type) and issubclass(expected, Exception):
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
363 self.assertRaises(expected,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
364 CGIHTTPServer._url_collapse_path_split, path)
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
365 else:
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
366 actual = CGIHTTPServer._url_collapse_path_split(path)
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
367 self.assertEqual(expected, actual,
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
368 msg='path = %r\nGot: %r\nWanted: %r' %
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
369 (path, actual, expected))
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
370
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
371 def test_headers_and_content(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
372 res = self.request('/cgi-bin/file1.py')
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
373 self.assertEqual(('Hello World\n', 'text/html', 200),
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
374 (res.read(), res.getheader('Content-type'), res.status))
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
375
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
376 def test_post(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
377 params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456})
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
378 headers = {'Content-type' : 'application/x-www-form-urlencoded'}
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
379 res = self.request('/cgi-bin/file2.py', 'POST', params, headers)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
380
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
381 self.assertEqual(res.read(), '1, python, 123456\n')
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
382
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
383 def test_invaliduri(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
384 res = self.request('/cgi-bin/invalid')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
385 res.read()
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
386 self.assertEqual(res.status, 404)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
387
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
388 def test_authorization(self):
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
389 headers = {'Authorization' : 'Basic %s' %
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
390 base64.b64encode('username:pass')}
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
391 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
392 self.assertEqual(('Hello World\n', 'text/html', 200),
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
393 (res.read(), res.getheader('Content-type'), res.status))
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
394
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
395 def test_no_leading_slash(self):
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
396 # http://bugs.python.org/issue2254
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
397 res = self.request('cgi-bin/file1.py')
59726
07c7664f21a1 Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56870
diff changeset
398 self.assertEqual(('Hello World\n', 'text/html', 200),
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
399 (res.read(), res.getheader('Content-type'), res.status))
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
400
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
401
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
402 def test_main(verbose=None):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
403 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
404 cwd = os.getcwd()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
405 test_support.run_unittest(BaseHTTPServerTestCase,
56564
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
406 SimpleHTTPServerTestCase,
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
407 CGIHTTPServerTestCase
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
408 )
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
409 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
410 os.chdir(cwd)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
411
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
412 if __name__ == '__main__':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
413 test_main()