annotate Lib/test/test_httpservers.py @ 47153:efe22eef26be legacy-trunk

GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
author Georg Brandl <georg@python.org>
date Sun, 18 May 2008 09:12:20 +0000
parents
children d17fac896a0b
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)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
196 os.chdir(tempfile.gettempdir())
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
197 self.data = 'We are the knights who say Ni!'
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
198 self.tempdir = tempfile.mkdtemp(dir=tempfile.gettempdir())
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
199 self.tempdir_name = os.path.basename(self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
200 self.tempfile = tempfile.NamedTemporaryFile(dir=self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
201 self.tempfile.file.write(self.data)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
202 self.tempfile.file.flush()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
203 self.tempfile_name = os.path.basename(self.tempfile.name)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
204
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
205 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 self.tempfile.close()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
209 shutil.rmtree(self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
210 except:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
213 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
214
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 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
216 body = response.read()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
217 self.assert_(response)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 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
219 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
220 if data:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 self.assertEqual(data, body)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
222
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
223 def test_get(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 #constructs the path relative to the root directory of the HTTPServer
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 response = self.request(self.tempdir_name + '/' + self.tempfile_name)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 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
227 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
228 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
229 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
230 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
231 response = self.request('/ThisDoesNotExist')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
232 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
233 response = self.request('/' + 'ThisDoesNotExist' + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 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
235 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
236 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
237 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
238 if os.name == 'posix':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
239 # 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
240 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
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, 404)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
243 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
244
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
245 def test_head(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
246 response = self.request(
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
247 self.tempdir_name + '/'+ self.tempfile_name, method='HEAD')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
248 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
249 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
250 str(len(self.data)))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
251 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
252 'application/octet-stream')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
253
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
254 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
255 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
256 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
257 # 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
258 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
259 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
260 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
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
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 cgi_file1 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
265 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
266
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
267 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
268 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
269 print "Hello World"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
270 """
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 cgi_file2 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
273 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
274 import cgi
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 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
277 print
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
278
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
279 form = cgi.FieldStorage()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
280 print "%%s, %%s, %%s" % (form.getfirst("spam"), form.getfirst("eggs"),\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
281 form.getfirst("bacon"))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
282 """
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 class CGIHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
285 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
286 pass
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 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
289 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
290 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
291 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
292 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
293
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
294 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
295 with open(self.file1_path, 'w') as file1:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
296 file1.write(cgi_file1 % sys.executable)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
297 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
298
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
299 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
300 with open(self.file2_path, 'w') as file2:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
301 file2.write(cgi_file2 % sys.executable)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
302 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
303
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
304 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
305
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
306 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
307 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
308 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
309 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
310 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
311 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
312 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
313 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
314
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
315 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
316 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
317 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
318 (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
319
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
320 def test_post(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
321 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
322 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
323 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
324
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
325 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
326
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
327 def test_invaliduri(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
328 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
329 res.read()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
330 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
331
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
332 def test_authorization(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
333 headers = {'Authorization' : 'Basic %s' % \
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
334 base64.b64encode('username:pass')}
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
335 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
336 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
337 (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
338
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_main(verbose=None):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
341 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
342 cwd = os.getcwd()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
343 test_support.run_unittest(BaseHTTPServerTestCase,
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
344 #SimpleHTTPServerTestCase,
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
345 #CGIHTTPServerTestCase
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
346 )
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
347 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
348 os.chdir(cwd)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
349
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
350 if __name__ == '__main__':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
351 test_main()