annotate Lib/test/test_httpservers.py @ 59953:f84f83963a78 2.6

Merged revisions 79297,79307 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r79297 | florent.xicluna | 2010-03-22 18:18:18 +0100 (lun, 22 mar 2010) | 2 lines #7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes. ........ r79307 | florent.xicluna | 2010-03-22 23:45:50 +0100 (lun, 22 mar 2010) | 2 lines #7667: Fix doctest failures with non-ASCII paths. ........
author Florent Xicluna <florent.xicluna@gmail.com>
date Tue, 23 Mar 2010 13:30:12 +0000
parents 3201fab35220
children c02144b1c98c
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
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
10
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 import os
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
12 import sys
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 import base64
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 import shutil
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 import urllib
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 import httplib
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 import tempfile
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
18 import threading
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
19
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 import unittest
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 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
22
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 class NoLogRequestHandler:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 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
26 # 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
27 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
28
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 class TestServerThread(threading.Thread):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 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
32 threading.Thread.__init__(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 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
34 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
35 self.test_object.lock.acquire()
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]
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
40 self.test_object.lock.release()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
42 self.server.serve_forever()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 self.server.server_close()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
45
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 def stop(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 self.server.shutdown()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
48
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 class BaseTestCase(unittest.TestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 self.lock = threading.Lock()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 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
54 self.thread.start()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
55 self.lock.acquire()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
56
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
58 self.lock.release()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
59 self.thread.stop()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
60
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 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
62 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
63 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
64 return self.connection.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
65
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
66
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 class BaseHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 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
69 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
70 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
71
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
72 def do_TEST(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
73 self.send_response(204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
74 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
75 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
76 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
77
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
78 def do_KEEP(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
79 self.send_response(204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 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
81 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
82 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
83
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 def do_KEYERROR(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 self.send_error(999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
86
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 def do_CUSTOM(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 self.send_response(999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 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
90 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
91 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
92
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 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
96 self.con.connect()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
97
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
98 def test_command(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
99 self.con.request('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
101 self.assertEquals(res.status, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
102
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
103 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
104 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
105 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
106 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 self.assertEquals(res.status, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
109
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
110 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
111 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
112 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
113 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
114 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 self.assertEquals(res.status, 400)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
116
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 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
118 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
119 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
120 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
121 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
122 self.assertEquals(res.status, 400)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
123
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
124 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
125 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
126 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
127 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
128 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
129 self.assertEquals(res.status, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
130
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 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
132 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
133 self.con.putrequest('PUT', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
134 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
135 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 self.assertEquals(res.status, 400)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
137
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
138 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
139 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
140 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
141 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
142 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
143 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
144 self.assertEquals(res.status, 505)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
145
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
146 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
147 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
148 self.con.putrequest('', '')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
149 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
150 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
151 self.assertEquals(res.status, 400)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
152
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 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
154 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 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
156 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
157 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
158 self.assertEquals(res.status, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
159
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
160 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
161 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
162 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
163 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
164 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
165 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
166 self.assertEquals(res.status, 501)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
167
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
168 def test_handler(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
169 self.con.request('TEST', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
170 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
171 self.assertEquals(res.status, 204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
172
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
173 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
174 self.con.request('KEEP', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
175 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
176 self.assertEquals(res.getheader('Connection'), 'keep-alive')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
177 self.con.request('TEST', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
178
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
179 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
180 self.con.request('KEYERROR', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
182 self.assertEquals(res.status, 999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
183
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
184 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
185 self.con.request('CUSTOM', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
186 res = self.con.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
187 self.assertEquals(res.status, 999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
188
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
189
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
190 class SimpleHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
191 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
192 pass
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 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
195 BaseTestCase.setUp(self)
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
196 self.cwd = os.getcwd()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
197 basetempdir = tempfile.gettempdir()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
198 os.chdir(basetempdir)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
199 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
200 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
201 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
202 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
203 temp.write(self.data)
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
204 temp.close()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
205
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
208 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
209 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 shutil.rmtree(self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 except:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
215
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
216 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
217 body = response.read()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 self.assert_(response)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
219 self.assertEquals(response.status, status)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
220 self.assert_(response.reason != None)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 if data:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
222 self.assertEqual(data, body)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
223
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 def test_get(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 #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
226 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
227 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
228 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
229 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
230 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
231 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
232 response = self.request('/ThisDoesNotExist')
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, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 response = self.request('/' + 'ThisDoesNotExist' + '/')
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, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 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
237 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
238 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
239 if os.name == 'posix':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 # 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
241 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
242 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
243 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
244 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
245
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
246 def test_head(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
247 response = self.request(
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
248 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
249 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
250 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
251 str(len(self.data)))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
252 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
253 'application/octet-stream')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
254
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
255 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
256 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
257 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
258 # 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
259 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
260 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
261 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
262 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
263
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
264
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
265 cgi_file1 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
266 #!%s
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 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
269 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
270 print "Hello World"
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
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
273 cgi_file2 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
274 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
275 import cgi
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 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
278 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
279
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
280 form = cgi.FieldStorage()
47179
d17fac896a0b Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
281 print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),\
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
282 form.getfirst("bacon"))
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
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
285 class CGIHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
286 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
287 pass
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 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
290 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
291 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
292 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
293 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
294
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
295 # The shebang line should be pure ASCII: use symlink if possible.
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
296 # See issue #7668.
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
297 if hasattr(os, 'symlink'):
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
298 self.pythonexe = os.path.join(self.parent_dir, 'python')
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
299 os.symlink(sys.executable, self.pythonexe)
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
300 else:
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
301 self.pythonexe = sys.executable
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
302
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
303 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
304 with open(self.file1_path, 'w') as file1:
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
305 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
306 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
307
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
308 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
309 with open(self.file2_path, 'w') as file2:
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
310 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
311 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
312
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
313 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
314 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
315
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
316 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
317 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
318 os.chdir(self.cwd)
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
319 if self.pythonexe != sys.executable:
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
320 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
321 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
322 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
323 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
324 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
325 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
326 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
327
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
328 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
329 res = self.request('/cgi-bin/file1.py')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
330 self.assertEquals(('Hello World\n', 'text/html', 200), \
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
331 (res.read(), res.getheader('Content-type'), res.status))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
332
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
333 def test_post(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
334 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
335 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
336 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
337
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
338 self.assertEquals(res.read(), '1, python, 123456\n')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
339
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
340 def test_invaliduri(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
341 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
342 res.read()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
343 self.assertEquals(res.status, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
344
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
345 def test_authorization(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
346 headers = {'Authorization' : 'Basic %s' % \
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
347 base64.b64encode('username:pass')}
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
348 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
349 self.assertEquals(('Hello World\n', 'text/html', 200), \
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
350 (res.read(), res.getheader('Content-type'), res.status))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
351
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
352
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
353 def test_main(verbose=None):
57084
3201fab35220 Try to fix test_wsgiref failures due to test_httpservers modifying the environment
Antoine Pitrou <solipsis@pitrou.net>
parents: 47180
diff changeset
354 cwd = os.getcwd()
3201fab35220 Try to fix test_wsgiref failures due to test_httpservers modifying the environment
Antoine Pitrou <solipsis@pitrou.net>
parents: 47180
diff changeset
355 env = os.environ.copy()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
356 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
357 test_support.run_unittest(BaseHTTPServerTestCase,
47179
d17fac896a0b Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
358 SimpleHTTPServerTestCase,
d17fac896a0b Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
359 CGIHTTPServerTestCase
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
360 )
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
361 finally:
57084
3201fab35220 Try to fix test_wsgiref failures due to test_httpservers modifying the environment
Antoine Pitrou <solipsis@pitrou.net>
parents: 47180
diff changeset
362 test_support.reap_children()
3201fab35220 Try to fix test_wsgiref failures due to test_httpservers modifying the environment
Antoine Pitrou <solipsis@pitrou.net>
parents: 47180
diff changeset
363 os.environ.clear()
3201fab35220 Try to fix test_wsgiref failures due to test_httpservers modifying the environment
Antoine Pitrou <solipsis@pitrou.net>
parents: 47180
diff changeset
364 os.environ.update(env)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
365 os.chdir(cwd)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
366
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
367 if __name__ == '__main__':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
368 test_main()