blob: 31d3184b2a705402ca99a76f1c089ab24034853e [file] [log] [blame]
Jeremy Hylton636950f2009-03-28 04:34:21 +00001import errno
Jeremy Hylton7c1692d2009-03-27 21:31:03 +00002from http import client
Jeremy Hylton8fff7922007-08-03 20:56:14 +00003import io
Antoine Pitrou803e6d62010-10-13 10:36:15 +00004import os
Antoine Pitrouead1d622009-09-29 18:44:53 +00005import array
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import socket
Jeremy Hylton121d34a2003-07-08 12:36:58 +00007
Gregory P. Smithb4066372010-01-03 03:28:29 +00008import unittest
9TestCase = unittest.TestCase
Jeremy Hylton2c178252004-08-07 16:28:14 +000010
Benjamin Petersonee8712c2008-05-20 21:35:26 +000011from test import support
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000012
Antoine Pitrou803e6d62010-10-13 10:36:15 +000013here = os.path.dirname(__file__)
14# Self-signed cert file for 'localhost'
15CERT_localhost = os.path.join(here, 'keycert.pem')
16# Self-signed cert file for 'fakehostname'
17CERT_fakehostname = os.path.join(here, 'keycert2.pem')
Georg Brandlfbaf9312014-11-05 20:37:40 +010018# Self-signed cert file for self-signed.pythontest.net
19CERT_selfsigned_pythontestdotnet = os.path.join(here, 'selfsigned_pythontestdotnet.pem')
Antoine Pitrou803e6d62010-10-13 10:36:15 +000020
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +000021# constants for testing chunked encoding
22chunked_start = (
23 'HTTP/1.1 200 OK\r\n'
24 'Transfer-Encoding: chunked\r\n\r\n'
25 'a\r\n'
26 'hello worl\r\n'
27 '3\r\n'
28 'd! \r\n'
29 '8\r\n'
30 'and now \r\n'
31 '22\r\n'
32 'for something completely different\r\n'
33)
34chunked_expected = b'hello world! and now for something completely different'
35chunk_extension = ";foo=bar"
36last_chunk = "0\r\n"
37last_chunk_extended = "0" + chunk_extension + "\r\n"
38trailers = "X-Dummy: foo\r\nX-Dumm2: bar\r\n"
39chunked_end = "\r\n"
40
Benjamin Petersonee8712c2008-05-20 21:35:26 +000041HOST = support.HOST
Christian Heimes5e696852008-04-09 08:37:03 +000042
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000043class FakeSocket:
Senthil Kumaran9da047b2014-04-14 13:07:56 -040044 def __init__(self, text, fileclass=io.BytesIO, host=None, port=None):
Jeremy Hylton8fff7922007-08-03 20:56:14 +000045 if isinstance(text, str):
Guido van Rossum39478e82007-08-27 17:23:59 +000046 text = text.encode("ascii")
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000047 self.text = text
Jeremy Hylton121d34a2003-07-08 12:36:58 +000048 self.fileclass = fileclass
Martin v. Löwisdd5a8602007-06-30 09:22:09 +000049 self.data = b''
Antoine Pitrou90e47742013-01-02 22:10:47 +010050 self.sendall_calls = 0
Serhiy Storchakab491e052014-12-01 13:07:45 +020051 self.file_closed = False
Senthil Kumaran9da047b2014-04-14 13:07:56 -040052 self.host = host
53 self.port = port
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000054
Jeremy Hylton2c178252004-08-07 16:28:14 +000055 def sendall(self, data):
Antoine Pitrou90e47742013-01-02 22:10:47 +010056 self.sendall_calls += 1
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 self.data += data
Jeremy Hylton2c178252004-08-07 16:28:14 +000058
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000059 def makefile(self, mode, bufsize=None):
60 if mode != 'r' and mode != 'rb':
Jeremy Hylton7c1692d2009-03-27 21:31:03 +000061 raise client.UnimplementedFileMode()
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +000062 # keep the file around so we can check how much was read from it
63 self.file = self.fileclass(self.text)
Serhiy Storchakab491e052014-12-01 13:07:45 +020064 self.file.close = self.file_close #nerf close ()
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +000065 return self.file
Jeremy Hylton121d34a2003-07-08 12:36:58 +000066
Serhiy Storchakab491e052014-12-01 13:07:45 +020067 def file_close(self):
68 self.file_closed = True
Jeremy Hylton121d34a2003-07-08 12:36:58 +000069
Senthil Kumaran9da047b2014-04-14 13:07:56 -040070 def close(self):
71 pass
72
Benjamin Peterson9d8a3ad2015-01-23 11:02:57 -050073 def setsockopt(self, level, optname, value):
74 pass
75
Jeremy Hylton636950f2009-03-28 04:34:21 +000076class EPipeSocket(FakeSocket):
77
78 def __init__(self, text, pipe_trigger):
79 # When sendall() is called with pipe_trigger, raise EPIPE.
80 FakeSocket.__init__(self, text)
81 self.pipe_trigger = pipe_trigger
82
83 def sendall(self, data):
84 if self.pipe_trigger in data:
Andrew Svetlov0832af62012-12-18 23:10:48 +020085 raise OSError(errno.EPIPE, "gotcha")
Jeremy Hylton636950f2009-03-28 04:34:21 +000086 self.data += data
87
88 def close(self):
89 pass
90
Serhiy Storchaka50254c52013-08-29 11:35:43 +030091class NoEOFBytesIO(io.BytesIO):
92 """Like BytesIO, but raises AssertionError on EOF.
Jeremy Hylton121d34a2003-07-08 12:36:58 +000093
Jeremy Hylton7c1692d2009-03-27 21:31:03 +000094 This is used below to test that http.client doesn't try to read
Jeremy Hylton121d34a2003-07-08 12:36:58 +000095 more from the underlying file than it should.
96 """
97 def read(self, n=-1):
Jeremy Hylton8fff7922007-08-03 20:56:14 +000098 data = io.BytesIO.read(self, n)
Jeremy Hyltonda3f2282007-08-29 17:26:34 +000099 if data == b'':
Jeremy Hylton121d34a2003-07-08 12:36:58 +0000100 raise AssertionError('caller tried to read past EOF')
101 return data
102
103 def readline(self, length=None):
Jeremy Hylton8fff7922007-08-03 20:56:14 +0000104 data = io.BytesIO.readline(self, length)
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000105 if data == b'':
Jeremy Hylton121d34a2003-07-08 12:36:58 +0000106 raise AssertionError('caller tried to read past EOF')
107 return data
Jeremy Hylton79fa2b62001-04-13 14:57:44 +0000108
Jeremy Hylton2c178252004-08-07 16:28:14 +0000109class HeaderTests(TestCase):
110 def test_auto_headers(self):
111 # Some headers are added automatically, but should not be added by
112 # .request() if they are explicitly set.
113
Jeremy Hylton2c178252004-08-07 16:28:14 +0000114 class HeaderCountingBuffer(list):
115 def __init__(self):
116 self.count = {}
117 def append(self, item):
Guido van Rossum022c4742007-08-29 02:00:20 +0000118 kv = item.split(b':')
Jeremy Hylton2c178252004-08-07 16:28:14 +0000119 if len(kv) > 1:
120 # item is a 'Key: Value' header string
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000121 lcKey = kv[0].decode('ascii').lower()
Jeremy Hylton2c178252004-08-07 16:28:14 +0000122 self.count.setdefault(lcKey, 0)
123 self.count[lcKey] += 1
124 list.append(self, item)
125
126 for explicit_header in True, False:
127 for header in 'Content-length', 'Host', 'Accept-encoding':
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000128 conn = client.HTTPConnection('example.com')
Jeremy Hylton2c178252004-08-07 16:28:14 +0000129 conn.sock = FakeSocket('blahblahblah')
130 conn._buffer = HeaderCountingBuffer()
131
132 body = 'spamspamspam'
133 headers = {}
134 if explicit_header:
135 headers[header] = str(len(body))
136 conn.request('POST', '/', body, headers)
137 self.assertEqual(conn._buffer.count[header.lower()], 1)
138
Senthil Kumaran5fa4a892012-05-19 16:58:09 +0800139 def test_content_length_0(self):
140
141 class ContentLengthChecker(list):
142 def __init__(self):
143 list.__init__(self)
144 self.content_length = None
145 def append(self, item):
146 kv = item.split(b':', 1)
147 if len(kv) > 1 and kv[0].lower() == b'content-length':
148 self.content_length = kv[1].strip()
149 list.append(self, item)
150
151 # POST with empty body
152 conn = client.HTTPConnection('example.com')
153 conn.sock = FakeSocket(None)
154 conn._buffer = ContentLengthChecker()
155 conn.request('POST', '/', '')
156 self.assertEqual(conn._buffer.content_length, b'0',
157 'Header Content-Length not set')
158
159 # PUT request with empty body
160 conn = client.HTTPConnection('example.com')
161 conn.sock = FakeSocket(None)
162 conn._buffer = ContentLengthChecker()
163 conn.request('PUT', '/', '')
164 self.assertEqual(conn._buffer.content_length, b'0',
165 'Header Content-Length not set')
166
Senthil Kumaran58d5dbf2010-10-03 18:22:42 +0000167 def test_putheader(self):
168 conn = client.HTTPConnection('example.com')
169 conn.sock = FakeSocket(None)
170 conn.putrequest('GET','/')
171 conn.putheader('Content-length', 42)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200172 self.assertIn(b'Content-length: 42', conn._buffer)
Senthil Kumaran58d5dbf2010-10-03 18:22:42 +0000173
Senthil Kumaran74ebd9e2010-11-13 12:27:49 +0000174 def test_ipv6host_header(self):
175 # Default host header on IPv6 transaction should wrapped by [] if
176 # its actual IPv6 address
177 expected = b'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \
178 b'Accept-Encoding: identity\r\n\r\n'
179 conn = client.HTTPConnection('[2001::]:81')
180 sock = FakeSocket('')
181 conn.sock = sock
182 conn.request('GET', '/foo')
183 self.assertTrue(sock.data.startswith(expected))
184
185 expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
186 b'Accept-Encoding: identity\r\n\r\n'
187 conn = client.HTTPConnection('[2001:102A::]')
188 sock = FakeSocket('')
189 conn.sock = sock
190 conn.request('GET', '/foo')
191 self.assertTrue(sock.data.startswith(expected))
192
Benjamin Peterson155ceaa2015-01-25 23:30:30 -0500193 def test_malformed_headers_coped_with(self):
194 # Issue 19996
195 body = "HTTP/1.1 200 OK\r\nFirst: val\r\n: nval\r\nSecond: val\r\n\r\n"
196 sock = FakeSocket(body)
197 resp = client.HTTPResponse(sock)
198 resp.begin()
199
200 self.assertEqual(resp.getheader('First'), 'val')
201 self.assertEqual(resp.getheader('Second'), 'val')
202
Senthil Kumaran58d5dbf2010-10-03 18:22:42 +0000203
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204class BasicTest(TestCase):
205 def test_status_lines(self):
206 # Test HTTP status lines
Jeremy Hylton79fa2b62001-04-13 14:57:44 +0000207
Thomas Wouters89f507f2006-12-13 04:49:30 +0000208 body = "HTTP/1.1 200 Ok\r\n\r\nText"
209 sock = FakeSocket(body)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000210 resp = client.HTTPResponse(sock)
Jeremy Hyltonba603192003-01-23 18:02:20 +0000211 resp.begin()
Serhiy Storchaka1c84ac12013-12-17 21:50:02 +0200212 self.assertEqual(resp.read(0), b'') # Issue #20007
213 self.assertFalse(resp.isclosed())
214 self.assertFalse(resp.closed)
Jeremy Hylton8fff7922007-08-03 20:56:14 +0000215 self.assertEqual(resp.read(), b"Text")
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000216 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200217 self.assertFalse(resp.closed)
218 resp.close()
219 self.assertTrue(resp.closed)
Jeremy Hyltonba603192003-01-23 18:02:20 +0000220
Thomas Wouters89f507f2006-12-13 04:49:30 +0000221 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
222 sock = FakeSocket(body)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000223 resp = client.HTTPResponse(sock)
224 self.assertRaises(client.BadStatusLine, resp.begin)
Jeremy Hyltonba603192003-01-23 18:02:20 +0000225
Benjamin Peterson11dbfd42010-03-21 22:50:04 +0000226 def test_bad_status_repr(self):
227 exc = client.BadStatusLine('')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000228 self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')
Benjamin Peterson11dbfd42010-03-21 22:50:04 +0000229
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000230 def test_partial_reads(self):
Antoine Pitrou084daa22012-12-15 19:11:54 +0100231 # if we have a length, the system knows when to close itself
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000232 # same behaviour than when we read the whole thing with read()
233 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
234 sock = FakeSocket(body)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000235 resp = client.HTTPResponse(sock)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000236 resp.begin()
237 self.assertEqual(resp.read(2), b'Te')
238 self.assertFalse(resp.isclosed())
239 self.assertEqual(resp.read(2), b'xt')
240 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200241 self.assertFalse(resp.closed)
242 resp.close()
243 self.assertTrue(resp.closed)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000244
Antoine Pitrou38d96432011-12-06 22:33:57 +0100245 def test_partial_readintos(self):
Antoine Pitroud20e7742012-12-15 19:22:30 +0100246 # if we have a length, the system knows when to close itself
Antoine Pitrou38d96432011-12-06 22:33:57 +0100247 # same behaviour than when we read the whole thing with read()
248 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
249 sock = FakeSocket(body)
250 resp = client.HTTPResponse(sock)
251 resp.begin()
252 b = bytearray(2)
253 n = resp.readinto(b)
254 self.assertEqual(n, 2)
255 self.assertEqual(bytes(b), b'Te')
256 self.assertFalse(resp.isclosed())
257 n = resp.readinto(b)
258 self.assertEqual(n, 2)
259 self.assertEqual(bytes(b), b'xt')
260 self.assertTrue(resp.isclosed())
Serhiy Storchakab6c86fd2013-02-06 10:35:40 +0200261 self.assertFalse(resp.closed)
262 resp.close()
263 self.assertTrue(resp.closed)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100264
Antoine Pitrou084daa22012-12-15 19:11:54 +0100265 def test_partial_reads_no_content_length(self):
266 # when no length is present, the socket should be gracefully closed when
267 # all data was read
268 body = "HTTP/1.1 200 Ok\r\n\r\nText"
269 sock = FakeSocket(body)
270 resp = client.HTTPResponse(sock)
271 resp.begin()
272 self.assertEqual(resp.read(2), b'Te')
273 self.assertFalse(resp.isclosed())
274 self.assertEqual(resp.read(2), b'xt')
275 self.assertEqual(resp.read(1), b'')
276 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200277 self.assertFalse(resp.closed)
278 resp.close()
279 self.assertTrue(resp.closed)
Antoine Pitrou084daa22012-12-15 19:11:54 +0100280
Antoine Pitroud20e7742012-12-15 19:22:30 +0100281 def test_partial_readintos_no_content_length(self):
282 # when no length is present, the socket should be gracefully closed when
283 # all data was read
284 body = "HTTP/1.1 200 Ok\r\n\r\nText"
285 sock = FakeSocket(body)
286 resp = client.HTTPResponse(sock)
287 resp.begin()
288 b = bytearray(2)
289 n = resp.readinto(b)
290 self.assertEqual(n, 2)
291 self.assertEqual(bytes(b), b'Te')
292 self.assertFalse(resp.isclosed())
293 n = resp.readinto(b)
294 self.assertEqual(n, 2)
295 self.assertEqual(bytes(b), b'xt')
296 n = resp.readinto(b)
297 self.assertEqual(n, 0)
298 self.assertTrue(resp.isclosed())
299
Antoine Pitroubeec61a2013-02-02 22:49:34 +0100300 def test_partial_reads_incomplete_body(self):
301 # if the server shuts down the connection before the whole
302 # content-length is delivered, the socket is gracefully closed
303 body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
304 sock = FakeSocket(body)
305 resp = client.HTTPResponse(sock)
306 resp.begin()
307 self.assertEqual(resp.read(2), b'Te')
308 self.assertFalse(resp.isclosed())
309 self.assertEqual(resp.read(2), b'xt')
310 self.assertEqual(resp.read(1), b'')
311 self.assertTrue(resp.isclosed())
312
Antoine Pitrou6a35e182013-02-02 23:04:56 +0100313 def test_partial_readintos_incomplete_body(self):
314 # if the server shuts down the connection before the whole
315 # content-length is delivered, the socket is gracefully closed
316 body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
317 sock = FakeSocket(body)
318 resp = client.HTTPResponse(sock)
319 resp.begin()
320 b = bytearray(2)
321 n = resp.readinto(b)
322 self.assertEqual(n, 2)
323 self.assertEqual(bytes(b), b'Te')
324 self.assertFalse(resp.isclosed())
325 n = resp.readinto(b)
326 self.assertEqual(n, 2)
327 self.assertEqual(bytes(b), b'xt')
328 n = resp.readinto(b)
329 self.assertEqual(n, 0)
330 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200331 self.assertFalse(resp.closed)
332 resp.close()
333 self.assertTrue(resp.closed)
Antoine Pitrou6a35e182013-02-02 23:04:56 +0100334
Thomas Wouters89f507f2006-12-13 04:49:30 +0000335 def test_host_port(self):
336 # Check invalid host_port
Jeremy Hyltonba603192003-01-23 18:02:20 +0000337
Łukasz Langaa5a9a9c2011-10-18 21:17:39 +0200338 for hp in ("www.python.org:abc", "user:password@www.python.org"):
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000339 self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000340
Jeremy Hylton3a38c912007-08-14 17:08:07 +0000341 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
342 "fe80::207:e9ff:fe9b", 8000),
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 ("www.python.org:80", "www.python.org", 80),
Łukasz Langaa5a9a9c2011-10-18 21:17:39 +0200344 ("www.python.org:", "www.python.org", 80),
Thomas Wouters89f507f2006-12-13 04:49:30 +0000345 ("www.python.org", "www.python.org", 80),
Łukasz Langaa5a9a9c2011-10-18 21:17:39 +0200346 ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80),
347 ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)):
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000348 c = client.HTTPConnection(hp)
Jeremy Hylton3a38c912007-08-14 17:08:07 +0000349 self.assertEqual(h, c.host)
350 self.assertEqual(p, c.port)
Skip Montanaro10e6e0e2004-09-14 16:32:02 +0000351
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352 def test_response_headers(self):
353 # test response with multiple message headers with the same field name.
354 text = ('HTTP/1.1 200 OK\r\n'
Jeremy Hylton3a38c912007-08-14 17:08:07 +0000355 'Set-Cookie: Customer="WILE_E_COYOTE"; '
356 'Version="1"; Path="/acme"\r\n'
Thomas Wouters89f507f2006-12-13 04:49:30 +0000357 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
358 ' Path="/acme"\r\n'
359 '\r\n'
360 'No body\r\n')
361 hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
362 ', '
363 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
364 s = FakeSocket(text)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000365 r = client.HTTPResponse(s)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 r.begin()
367 cookies = r.getheader("Set-Cookie")
Jeremy Hylton3a38c912007-08-14 17:08:07 +0000368 self.assertEqual(cookies, hdr)
Jeremy Hyltonba603192003-01-23 18:02:20 +0000369
Thomas Wouters89f507f2006-12-13 04:49:30 +0000370 def test_read_head(self):
371 # Test that the library doesn't attempt to read any data
372 # from a HEAD request. (Tickles SF bug #622042.)
373 sock = FakeSocket(
374 'HTTP/1.1 200 OK\r\n'
375 'Content-Length: 14432\r\n'
376 '\r\n',
Serhiy Storchaka50254c52013-08-29 11:35:43 +0300377 NoEOFBytesIO)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000378 resp = client.HTTPResponse(sock, method="HEAD")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000379 resp.begin()
Guido van Rossuma00f1232007-09-12 19:43:09 +0000380 if resp.read():
Thomas Wouters89f507f2006-12-13 04:49:30 +0000381 self.fail("Did not expect response from HEAD request")
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000382
Antoine Pitrou38d96432011-12-06 22:33:57 +0100383 def test_readinto_head(self):
384 # Test that the library doesn't attempt to read any data
385 # from a HEAD request. (Tickles SF bug #622042.)
386 sock = FakeSocket(
387 'HTTP/1.1 200 OK\r\n'
388 'Content-Length: 14432\r\n'
389 '\r\n',
Serhiy Storchaka50254c52013-08-29 11:35:43 +0300390 NoEOFBytesIO)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100391 resp = client.HTTPResponse(sock, method="HEAD")
392 resp.begin()
393 b = bytearray(5)
394 if resp.readinto(b) != 0:
395 self.fail("Did not expect response from HEAD request")
396 self.assertEqual(bytes(b), b'\x00'*5)
397
Georg Brandlbf3f8eb2013-10-27 07:34:48 +0100398 def test_too_many_headers(self):
399 headers = '\r\n'.join('Header%d: foo' % i
400 for i in range(client._MAXHEADERS + 1)) + '\r\n'
401 text = ('HTTP/1.1 200 OK\r\n' + headers)
402 s = FakeSocket(text)
403 r = client.HTTPResponse(s)
404 self.assertRaisesRegex(client.HTTPException,
405 r"got more than \d+ headers", r.begin)
406
Thomas Wouters89f507f2006-12-13 04:49:30 +0000407 def test_send_file(self):
Guido van Rossum022c4742007-08-29 02:00:20 +0000408 expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n'
409 b'Accept-Encoding: identity\r\nContent-Length:')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000410
Brett Cannon77b7de62010-10-29 23:31:11 +0000411 with open(__file__, 'rb') as body:
412 conn = client.HTTPConnection('example.com')
413 sock = FakeSocket(body)
414 conn.sock = sock
415 conn.request('GET', '/foo', body)
416 self.assertTrue(sock.data.startswith(expected), '%r != %r' %
417 (sock.data[:len(expected)], expected))
Jeremy Hylton2c178252004-08-07 16:28:14 +0000418
Antoine Pitrouead1d622009-09-29 18:44:53 +0000419 def test_send(self):
420 expected = b'this is a test this is only a test'
421 conn = client.HTTPConnection('example.com')
422 sock = FakeSocket(None)
423 conn.sock = sock
424 conn.send(expected)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000425 self.assertEqual(expected, sock.data)
Antoine Pitrouead1d622009-09-29 18:44:53 +0000426 sock.data = b''
427 conn.send(array.array('b', expected))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000428 self.assertEqual(expected, sock.data)
Antoine Pitrouead1d622009-09-29 18:44:53 +0000429 sock.data = b''
430 conn.send(io.BytesIO(expected))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000431 self.assertEqual(expected, sock.data)
Antoine Pitrouead1d622009-09-29 18:44:53 +0000432
Andrew Svetlov7b2c8bb2013-04-12 22:49:19 +0300433 def test_send_updating_file(self):
434 def data():
435 yield 'data'
436 yield None
437 yield 'data_two'
438
439 class UpdatingFile():
440 mode = 'r'
441 d = data()
442 def read(self, blocksize=-1):
443 return self.d.__next__()
444
445 expected = b'data'
446
447 conn = client.HTTPConnection('example.com')
448 sock = FakeSocket("")
449 conn.sock = sock
450 conn.send(UpdatingFile())
451 self.assertEqual(sock.data, expected)
452
453
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000454 def test_send_iter(self):
455 expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
456 b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \
457 b'\r\nonetwothree'
458
459 def body():
460 yield b"one"
461 yield b"two"
462 yield b"three"
463
464 conn = client.HTTPConnection('example.com')
465 sock = FakeSocket("")
466 conn.sock = sock
467 conn.request('GET', '/foo', body(), {'Content-Length': '11'})
Victor Stinner04ba9662011-01-04 00:04:46 +0000468 self.assertEqual(sock.data, expected)
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000469
Senthil Kumaraneb71ad42011-08-02 18:33:41 +0800470 def test_send_type_error(self):
471 # See: Issue #12676
472 conn = client.HTTPConnection('example.com')
473 conn.sock = FakeSocket('')
474 with self.assertRaises(TypeError):
475 conn.request('POST', 'test', conn)
476
Christian Heimesa612dc02008-02-24 13:08:18 +0000477 def test_chunked(self):
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000478 expected = chunked_expected
479 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000480 resp = client.HTTPResponse(sock, method="GET")
Christian Heimesa612dc02008-02-24 13:08:18 +0000481 resp.begin()
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100482 self.assertEqual(resp.read(), expected)
Christian Heimesa612dc02008-02-24 13:08:18 +0000483 resp.close()
484
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100485 # Various read sizes
486 for n in range(1, 12):
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000487 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100488 resp = client.HTTPResponse(sock, method="GET")
489 resp.begin()
490 self.assertEqual(resp.read(n) + resp.read(n) + resp.read(), expected)
491 resp.close()
492
Christian Heimesa612dc02008-02-24 13:08:18 +0000493 for x in ('', 'foo\r\n'):
494 sock = FakeSocket(chunked_start + x)
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000495 resp = client.HTTPResponse(sock, method="GET")
Christian Heimesa612dc02008-02-24 13:08:18 +0000496 resp.begin()
497 try:
498 resp.read()
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000499 except client.IncompleteRead as i:
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100500 self.assertEqual(i.partial, expected)
501 expected_message = 'IncompleteRead(%d bytes read)' % len(expected)
502 self.assertEqual(repr(i), expected_message)
503 self.assertEqual(str(i), expected_message)
Christian Heimesa612dc02008-02-24 13:08:18 +0000504 else:
505 self.fail('IncompleteRead expected')
506 finally:
507 resp.close()
508
Antoine Pitrou38d96432011-12-06 22:33:57 +0100509 def test_readinto_chunked(self):
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000510
511 expected = chunked_expected
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100512 nexpected = len(expected)
513 b = bytearray(128)
514
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000515 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100516 resp = client.HTTPResponse(sock, method="GET")
517 resp.begin()
Antoine Pitrou38d96432011-12-06 22:33:57 +0100518 n = resp.readinto(b)
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100519 self.assertEqual(b[:nexpected], expected)
520 self.assertEqual(n, nexpected)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100521 resp.close()
522
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100523 # Various read sizes
524 for n in range(1, 12):
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000525 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100526 resp = client.HTTPResponse(sock, method="GET")
527 resp.begin()
528 m = memoryview(b)
529 i = resp.readinto(m[0:n])
530 i += resp.readinto(m[i:n + i])
531 i += resp.readinto(m[i:])
532 self.assertEqual(b[:nexpected], expected)
533 self.assertEqual(i, nexpected)
534 resp.close()
535
Antoine Pitrou38d96432011-12-06 22:33:57 +0100536 for x in ('', 'foo\r\n'):
537 sock = FakeSocket(chunked_start + x)
538 resp = client.HTTPResponse(sock, method="GET")
539 resp.begin()
540 try:
Antoine Pitrou38d96432011-12-06 22:33:57 +0100541 n = resp.readinto(b)
542 except client.IncompleteRead as i:
Antoine Pitrouf7e78182012-01-04 18:57:22 +0100543 self.assertEqual(i.partial, expected)
544 expected_message = 'IncompleteRead(%d bytes read)' % len(expected)
545 self.assertEqual(repr(i), expected_message)
546 self.assertEqual(str(i), expected_message)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100547 else:
548 self.fail('IncompleteRead expected')
549 finally:
550 resp.close()
551
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000552 def test_chunked_head(self):
553 chunked_start = (
554 'HTTP/1.1 200 OK\r\n'
555 'Transfer-Encoding: chunked\r\n\r\n'
556 'a\r\n'
557 'hello world\r\n'
558 '1\r\n'
559 'd\r\n'
560 )
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000561 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000562 resp = client.HTTPResponse(sock, method="HEAD")
563 resp.begin()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000564 self.assertEqual(resp.read(), b'')
565 self.assertEqual(resp.status, 200)
566 self.assertEqual(resp.reason, 'OK')
Senthil Kumaran0b998832010-06-04 17:27:11 +0000567 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200568 self.assertFalse(resp.closed)
569 resp.close()
570 self.assertTrue(resp.closed)
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000571
Antoine Pitrou38d96432011-12-06 22:33:57 +0100572 def test_readinto_chunked_head(self):
573 chunked_start = (
574 'HTTP/1.1 200 OK\r\n'
575 'Transfer-Encoding: chunked\r\n\r\n'
576 'a\r\n'
577 'hello world\r\n'
578 '1\r\n'
579 'd\r\n'
580 )
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000581 sock = FakeSocket(chunked_start + last_chunk + chunked_end)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100582 resp = client.HTTPResponse(sock, method="HEAD")
583 resp.begin()
584 b = bytearray(5)
585 n = resp.readinto(b)
586 self.assertEqual(n, 0)
587 self.assertEqual(bytes(b), b'\x00'*5)
588 self.assertEqual(resp.status, 200)
589 self.assertEqual(resp.reason, 'OK')
590 self.assertTrue(resp.isclosed())
Serhiy Storchakab6c86fd2013-02-06 10:35:40 +0200591 self.assertFalse(resp.closed)
592 resp.close()
593 self.assertTrue(resp.closed)
Antoine Pitrou38d96432011-12-06 22:33:57 +0100594
Christian Heimesa612dc02008-02-24 13:08:18 +0000595 def test_negative_content_length(self):
Jeremy Hylton82066952008-12-15 03:08:30 +0000596 sock = FakeSocket(
597 'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000598 resp = client.HTTPResponse(sock, method="GET")
Christian Heimesa612dc02008-02-24 13:08:18 +0000599 resp.begin()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000600 self.assertEqual(resp.read(), b'Hello\r\n')
Antoine Pitroubeec61a2013-02-02 22:49:34 +0100601 self.assertTrue(resp.isclosed())
Christian Heimesa612dc02008-02-24 13:08:18 +0000602
Benjamin Peterson6accb982009-03-02 22:50:25 +0000603 def test_incomplete_read(self):
604 sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000605 resp = client.HTTPResponse(sock, method="GET")
Benjamin Peterson6accb982009-03-02 22:50:25 +0000606 resp.begin()
607 try:
608 resp.read()
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000609 except client.IncompleteRead as i:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000610 self.assertEqual(i.partial, b'Hello\r\n')
Benjamin Peterson6accb982009-03-02 22:50:25 +0000611 self.assertEqual(repr(i),
612 "IncompleteRead(7 bytes read, 3 more expected)")
613 self.assertEqual(str(i),
614 "IncompleteRead(7 bytes read, 3 more expected)")
Antoine Pitroubeec61a2013-02-02 22:49:34 +0100615 self.assertTrue(resp.isclosed())
Benjamin Peterson6accb982009-03-02 22:50:25 +0000616 else:
617 self.fail('IncompleteRead expected')
Benjamin Peterson6accb982009-03-02 22:50:25 +0000618
Jeremy Hylton636950f2009-03-28 04:34:21 +0000619 def test_epipe(self):
620 sock = EPipeSocket(
621 "HTTP/1.0 401 Authorization Required\r\n"
622 "Content-type: text/html\r\n"
623 "WWW-Authenticate: Basic realm=\"example\"\r\n",
624 b"Content-Length")
625 conn = client.HTTPConnection("example.com")
626 conn.sock = sock
Andrew Svetlov0832af62012-12-18 23:10:48 +0200627 self.assertRaises(OSError,
Jeremy Hylton636950f2009-03-28 04:34:21 +0000628 lambda: conn.request("PUT", "/url", "body"))
629 resp = conn.getresponse()
630 self.assertEqual(401, resp.status)
631 self.assertEqual("Basic realm=\"example\"",
632 resp.getheader("www-authenticate"))
Christian Heimesa612dc02008-02-24 13:08:18 +0000633
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000634 # Test lines overflowing the max line size (_MAXLINE in http.client)
635
636 def test_overflowing_status_line(self):
637 body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
638 resp = client.HTTPResponse(FakeSocket(body))
639 self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin)
640
641 def test_overflowing_header_line(self):
642 body = (
643 'HTTP/1.1 200 OK\r\n'
644 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n'
645 )
646 resp = client.HTTPResponse(FakeSocket(body))
647 self.assertRaises(client.LineTooLong, resp.begin)
648
649 def test_overflowing_chunked_line(self):
650 body = (
651 'HTTP/1.1 200 OK\r\n'
652 'Transfer-Encoding: chunked\r\n\r\n'
653 + '0' * 65536 + 'a\r\n'
654 'hello world\r\n'
655 '0\r\n'
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000656 '\r\n'
Senthil Kumaran5466bf12010-12-18 16:55:23 +0000657 )
658 resp = client.HTTPResponse(FakeSocket(body))
659 resp.begin()
660 self.assertRaises(client.LineTooLong, resp.read)
661
Senthil Kumaran9c29f862012-04-29 10:20:46 +0800662 def test_early_eof(self):
663 # Test httpresponse with no \r\n termination,
664 body = "HTTP/1.1 200 Ok"
665 sock = FakeSocket(body)
666 resp = client.HTTPResponse(sock)
667 resp.begin()
668 self.assertEqual(resp.read(), b'')
669 self.assertTrue(resp.isclosed())
Serhiy Storchakab5b9c8c2013-02-06 10:31:57 +0200670 self.assertFalse(resp.closed)
671 resp.close()
672 self.assertTrue(resp.closed)
Senthil Kumaran9c29f862012-04-29 10:20:46 +0800673
Serhiy Storchakab491e052014-12-01 13:07:45 +0200674 def test_error_leak(self):
675 # Test that the socket is not leaked if getresponse() fails
676 conn = client.HTTPConnection('example.com')
677 response = None
678 class Response(client.HTTPResponse):
679 def __init__(self, *pos, **kw):
680 nonlocal response
681 response = self # Avoid garbage collector closing the socket
682 client.HTTPResponse.__init__(self, *pos, **kw)
683 conn.response_class = Response
684 conn.sock = FakeSocket('') # Emulate server dropping connection
685 conn.request('GET', '/')
686 self.assertRaises(client.BadStatusLine, conn.getresponse)
687 self.assertTrue(response.closed)
688 self.assertTrue(conn.sock.file_closed)
689
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +0000690 def test_chunked_extension(self):
691 extra = '3;foo=bar\r\n' + 'abc\r\n'
692 expected = chunked_expected + b'abc'
693
694 sock = FakeSocket(chunked_start + extra + last_chunk_extended + chunked_end)
695 resp = client.HTTPResponse(sock, method="GET")
696 resp.begin()
697 self.assertEqual(resp.read(), expected)
698 resp.close()
699
700 def test_chunked_missing_end(self):
701 """some servers may serve up a short chunked encoding stream"""
702 expected = chunked_expected
703 sock = FakeSocket(chunked_start + last_chunk) #no terminating crlf
704 resp = client.HTTPResponse(sock, method="GET")
705 resp.begin()
706 self.assertEqual(resp.read(), expected)
707 resp.close()
708
709 def test_chunked_trailers(self):
710 """See that trailers are read and ignored"""
711 expected = chunked_expected
712 sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end)
713 resp = client.HTTPResponse(sock, method="GET")
714 resp.begin()
715 self.assertEqual(resp.read(), expected)
716 # we should have reached the end of the file
717 self.assertEqual(sock.file.read(100), b"") #we read to the end
718 resp.close()
719
720 def test_chunked_sync(self):
721 """Check that we don't read past the end of the chunked-encoding stream"""
722 expected = chunked_expected
723 extradata = "extradata"
724 sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end + extradata)
725 resp = client.HTTPResponse(sock, method="GET")
726 resp.begin()
727 self.assertEqual(resp.read(), expected)
728 # the file should now have our extradata ready to be read
729 self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end
730 resp.close()
731
732 def test_content_length_sync(self):
733 """Check that we don't read past the end of the Content-Length stream"""
734 extradata = "extradata"
735 expected = b"Hello123\r\n"
736 sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello123\r\n' + extradata)
737 resp = client.HTTPResponse(sock, method="GET")
738 resp.begin()
739 self.assertEqual(resp.read(), expected)
740 # the file should now have our extradata ready to be read
741 self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end
742 resp.close()
743
744class ExtendedReadTest(TestCase):
745 """
746 Test peek(), read1(), readline()
747 """
748 lines = (
749 'HTTP/1.1 200 OK\r\n'
750 '\r\n'
751 'hello world!\n'
752 'and now \n'
753 'for something completely different\n'
754 'foo'
755 )
756 lines_expected = lines[lines.find('hello'):].encode("ascii")
757 lines_chunked = (
758 'HTTP/1.1 200 OK\r\n'
759 'Transfer-Encoding: chunked\r\n\r\n'
760 'a\r\n'
761 'hello worl\r\n'
762 '3\r\n'
763 'd!\n\r\n'
764 '9\r\n'
765 'and now \n\r\n'
766 '23\r\n'
767 'for something completely different\n\r\n'
768 '3\r\n'
769 'foo\r\n'
770 '0\r\n' # terminating chunk
771 '\r\n' # end of trailers
772 )
773
774 def setUp(self):
775 sock = FakeSocket(self.lines)
776 resp = client.HTTPResponse(sock, method="GET")
777 resp.begin()
778 resp.fp = io.BufferedReader(resp.fp)
779 self.resp = resp
780
781
782
783 def test_peek(self):
784 resp = self.resp
785 # patch up the buffered peek so that it returns not too much stuff
786 oldpeek = resp.fp.peek
787 def mypeek(n=-1):
788 p = oldpeek(n)
789 if n >= 0:
790 return p[:n]
791 return p[:10]
792 resp.fp.peek = mypeek
793
794 all = []
795 while True:
796 # try a short peek
797 p = resp.peek(3)
798 if p:
799 self.assertGreater(len(p), 0)
800 # then unbounded peek
801 p2 = resp.peek()
802 self.assertGreaterEqual(len(p2), len(p))
803 self.assertTrue(p2.startswith(p))
804 next = resp.read(len(p2))
805 self.assertEqual(next, p2)
806 else:
807 next = resp.read()
808 self.assertFalse(next)
809 all.append(next)
810 if not next:
811 break
812 self.assertEqual(b"".join(all), self.lines_expected)
813
814 def test_readline(self):
815 resp = self.resp
816 self._verify_readline(self.resp.readline, self.lines_expected)
817
818 def _verify_readline(self, readline, expected):
819 all = []
820 while True:
821 # short readlines
822 line = readline(5)
823 if line and line != b"foo":
824 if len(line) < 5:
825 self.assertTrue(line.endswith(b"\n"))
826 all.append(line)
827 if not line:
828 break
829 self.assertEqual(b"".join(all), expected)
830
831 def test_read1(self):
832 resp = self.resp
833 def r():
834 res = resp.read1(4)
835 self.assertLessEqual(len(res), 4)
836 return res
837 readliner = Readliner(r)
838 self._verify_readline(readliner.readline, self.lines_expected)
839
840 def test_read1_unbounded(self):
841 resp = self.resp
842 all = []
843 while True:
844 data = resp.read1()
845 if not data:
846 break
847 all.append(data)
848 self.assertEqual(b"".join(all), self.lines_expected)
849
850 def test_read1_bounded(self):
851 resp = self.resp
852 all = []
853 while True:
854 data = resp.read1(10)
855 if not data:
856 break
857 self.assertLessEqual(len(data), 10)
858 all.append(data)
859 self.assertEqual(b"".join(all), self.lines_expected)
860
861 def test_read1_0(self):
862 self.assertEqual(self.resp.read1(0), b"")
863
864 def test_peek_0(self):
865 p = self.resp.peek(0)
866 self.assertLessEqual(0, len(p))
867
868class ExtendedReadTestChunked(ExtendedReadTest):
869 """
870 Test peek(), read1(), readline() in chunked mode
871 """
872 lines = (
873 'HTTP/1.1 200 OK\r\n'
874 'Transfer-Encoding: chunked\r\n\r\n'
875 'a\r\n'
876 'hello worl\r\n'
877 '3\r\n'
878 'd!\n\r\n'
879 '9\r\n'
880 'and now \n\r\n'
881 '23\r\n'
882 'for something completely different\n\r\n'
883 '3\r\n'
884 'foo\r\n'
885 '0\r\n' # terminating chunk
886 '\r\n' # end of trailers
887 )
888
889
890class Readliner:
891 """
892 a simple readline class that uses an arbitrary read function and buffering
893 """
894 def __init__(self, readfunc):
895 self.readfunc = readfunc
896 self.remainder = b""
897
898 def readline(self, limit):
899 data = []
900 datalen = 0
901 read = self.remainder
902 try:
903 while True:
904 idx = read.find(b'\n')
905 if idx != -1:
906 break
907 if datalen + len(read) >= limit:
908 idx = limit - datalen - 1
909 # read more data
910 data.append(read)
911 read = self.readfunc()
912 if not read:
913 idx = 0 #eof condition
914 break
915 idx += 1
916 data.append(read[:idx])
917 self.remainder = read[idx:]
918 return b"".join(data)
919 except:
920 self.remainder = b"".join(data)
921 raise
922
Georg Brandl4cbd1e32006-02-17 22:01:08 +0000923class OfflineTest(TestCase):
924 def test_responses(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000925 self.assertEqual(client.responses[client.NOT_FOUND], "Not Found")
Georg Brandl4cbd1e32006-02-17 22:01:08 +0000926
Gregory P. Smithb4066372010-01-03 03:28:29 +0000927
928class SourceAddressTest(TestCase):
929 def setUp(self):
930 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
931 self.port = support.bind_port(self.serv)
932 self.source_port = support.find_unused_port()
Charles-François Natali6e204602014-07-23 19:28:13 +0100933 self.serv.listen()
Gregory P. Smithb4066372010-01-03 03:28:29 +0000934 self.conn = None
935
936 def tearDown(self):
937 if self.conn:
938 self.conn.close()
939 self.conn = None
940 self.serv.close()
941 self.serv = None
942
943 def testHTTPConnectionSourceAddress(self):
944 self.conn = client.HTTPConnection(HOST, self.port,
945 source_address=('', self.source_port))
946 self.conn.connect()
947 self.assertEqual(self.conn.sock.getsockname()[1], self.source_port)
948
949 @unittest.skipIf(not hasattr(client, 'HTTPSConnection'),
950 'http.client.HTTPSConnection not defined')
951 def testHTTPSConnectionSourceAddress(self):
952 self.conn = client.HTTPSConnection(HOST, self.port,
953 source_address=('', self.source_port))
954 # We don't test anything here other the constructor not barfing as
955 # this code doesn't deal with setting up an active running SSL server
956 # for an ssl_wrapped connect() to actually return from.
957
958
Guido van Rossumd8faa362007-04-27 19:54:29 +0000959class TimeoutTest(TestCase):
Christian Heimes5e696852008-04-09 08:37:03 +0000960 PORT = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961
962 def setUp(self):
963 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000964 TimeoutTest.PORT = support.bind_port(self.serv)
Charles-François Natali6e204602014-07-23 19:28:13 +0100965 self.serv.listen()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966
967 def tearDown(self):
968 self.serv.close()
969 self.serv = None
970
971 def testTimeoutAttribute(self):
Jeremy Hylton3a38c912007-08-14 17:08:07 +0000972 # This will prove that the timeout gets through HTTPConnection
973 # and into the socket.
974
Georg Brandlf78e02b2008-06-10 17:40:04 +0000975 # default -- use global socket timeout
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200976 self.assertIsNone(socket.getdefaulttimeout())
Georg Brandlf78e02b2008-06-10 17:40:04 +0000977 socket.setdefaulttimeout(30)
978 try:
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000979 httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000980 httpConn.connect()
981 finally:
982 socket.setdefaulttimeout(None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000983 self.assertEqual(httpConn.sock.gettimeout(), 30)
984 httpConn.close()
985
Georg Brandlf78e02b2008-06-10 17:40:04 +0000986 # no timeout -- do not use global socket default
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +0200987 self.assertIsNone(socket.getdefaulttimeout())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988 socket.setdefaulttimeout(30)
989 try:
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000990 httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT,
Christian Heimes5e696852008-04-09 08:37:03 +0000991 timeout=None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000992 httpConn.connect()
993 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000994 socket.setdefaulttimeout(None)
995 self.assertEqual(httpConn.sock.gettimeout(), None)
996 httpConn.close()
997
998 # a value
Jeremy Hylton7c1692d2009-03-27 21:31:03 +0000999 httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001000 httpConn.connect()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001001 self.assertEqual(httpConn.sock.gettimeout(), 30)
1002 httpConn.close()
1003
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001004
1005class HTTPSTest(TestCase):
1006
1007 def setUp(self):
1008 if not hasattr(client, 'HTTPSConnection'):
1009 self.skipTest('ssl support required')
1010
1011 def make_server(self, certfile):
1012 from test.ssl_servers import make_https_server
Antoine Pitrouda232592013-02-05 21:20:51 +01001013 return make_https_server(self, certfile=certfile)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001014
1015 def test_attributes(self):
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001016 # simple test to check it's storing the timeout
1017 h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30)
1018 self.assertEqual(h.timeout, 30)
1019
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001020 def test_networked(self):
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001021 # Default settings: requires a valid cert from a trusted CA
1022 import ssl
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001023 support.requires('network')
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001024 with support.transient_internet('self-signed.pythontest.net'):
1025 h = client.HTTPSConnection('self-signed.pythontest.net', 443)
1026 with self.assertRaises(ssl.SSLError) as exc_info:
1027 h.request('GET', '/')
1028 self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED')
1029
1030 def test_networked_noverification(self):
1031 # Switch off cert verification
1032 import ssl
1033 support.requires('network')
1034 with support.transient_internet('self-signed.pythontest.net'):
1035 context = ssl._create_unverified_context()
1036 h = client.HTTPSConnection('self-signed.pythontest.net', 443,
1037 context=context)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001038 h.request('GET', '/')
1039 resp = h.getresponse()
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001040 self.assertIn('nginx', resp.getheader('server'))
1041
Benjamin Peterson2615e9e2014-11-25 15:16:55 -06001042 @support.system_must_validate_cert
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001043 def test_networked_trusted_by_default_cert(self):
1044 # Default settings: requires a valid cert from a trusted CA
1045 support.requires('network')
1046 with support.transient_internet('www.python.org'):
1047 h = client.HTTPSConnection('www.python.org', 443)
1048 h.request('GET', '/')
1049 resp = h.getresponse()
1050 content_type = resp.getheader('content-type')
1051 self.assertIn('text/html', content_type)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001052
1053 def test_networked_good_cert(self):
Georg Brandlfbaf9312014-11-05 20:37:40 +01001054 # We feed the server's cert as a validating cert
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001055 import ssl
1056 support.requires('network')
Georg Brandlfbaf9312014-11-05 20:37:40 +01001057 with support.transient_internet('self-signed.pythontest.net'):
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001058 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1059 context.verify_mode = ssl.CERT_REQUIRED
Georg Brandlfbaf9312014-11-05 20:37:40 +01001060 context.load_verify_locations(CERT_selfsigned_pythontestdotnet)
1061 h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001062 h.request('GET', '/')
1063 resp = h.getresponse()
Georg Brandlfbaf9312014-11-05 20:37:40 +01001064 server_string = resp.getheader('server')
1065 self.assertIn('nginx', server_string)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001066
1067 def test_networked_bad_cert(self):
1068 # We feed a "CA" cert that is unrelated to the server's cert
1069 import ssl
1070 support.requires('network')
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001071 with support.transient_internet('self-signed.pythontest.net'):
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001072 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1073 context.verify_mode = ssl.CERT_REQUIRED
1074 context.load_verify_locations(CERT_localhost)
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001075 h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context)
1076 with self.assertRaises(ssl.SSLError) as exc_info:
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001077 h.request('GET', '/')
Benjamin Peterson4ffb0752014-11-03 14:29:33 -05001078 self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED')
1079
1080 def test_local_unknown_cert(self):
1081 # The custom cert isn't known to the default trust bundle
1082 import ssl
1083 server = self.make_server(CERT_localhost)
1084 h = client.HTTPSConnection('localhost', server.port)
1085 with self.assertRaises(ssl.SSLError) as exc_info:
1086 h.request('GET', '/')
1087 self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED')
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001088
1089 def test_local_good_hostname(self):
1090 # The (valid) cert validates the HTTP hostname
1091 import ssl
Brett Cannon252365b2011-08-04 22:43:11 -07001092 server = self.make_server(CERT_localhost)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001093 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1094 context.verify_mode = ssl.CERT_REQUIRED
1095 context.load_verify_locations(CERT_localhost)
1096 h = client.HTTPSConnection('localhost', server.port, context=context)
1097 h.request('GET', '/nonexistent')
1098 resp = h.getresponse()
1099 self.assertEqual(resp.status, 404)
1100
1101 def test_local_bad_hostname(self):
1102 # The (valid) cert doesn't validate the HTTP hostname
1103 import ssl
Brett Cannon252365b2011-08-04 22:43:11 -07001104 server = self.make_server(CERT_fakehostname)
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001105 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
1106 context.verify_mode = ssl.CERT_REQUIRED
Benjamin Petersona090f012014-12-07 13:18:25 -05001107 context.check_hostname = True
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001108 context.load_verify_locations(CERT_fakehostname)
1109 h = client.HTTPSConnection('localhost', server.port, context=context)
1110 with self.assertRaises(ssl.CertificateError):
1111 h.request('GET', '/')
1112 # Same with explicit check_hostname=True
1113 h = client.HTTPSConnection('localhost', server.port, context=context,
1114 check_hostname=True)
1115 with self.assertRaises(ssl.CertificateError):
1116 h.request('GET', '/')
1117 # With check_hostname=False, the mismatching is ignored
Benjamin Petersona090f012014-12-07 13:18:25 -05001118 context.check_hostname = False
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001119 h = client.HTTPSConnection('localhost', server.port, context=context,
1120 check_hostname=False)
1121 h.request('GET', '/nonexistent')
1122 resp = h.getresponse()
1123 self.assertEqual(resp.status, 404)
Benjamin Petersona090f012014-12-07 13:18:25 -05001124 # The context's check_hostname setting is used if one isn't passed to
1125 # HTTPSConnection.
1126 context.check_hostname = False
1127 h = client.HTTPSConnection('localhost', server.port, context=context)
1128 h.request('GET', '/nonexistent')
1129 self.assertEqual(h.getresponse().status, 404)
1130 # Passing check_hostname to HTTPSConnection should override the
1131 # context's setting.
1132 h = client.HTTPSConnection('localhost', server.port, context=context,
1133 check_hostname=True)
1134 with self.assertRaises(ssl.CertificateError):
1135 h.request('GET', '/')
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001136
Petri Lehtinene119c402011-10-26 21:29:15 +03001137 @unittest.skipIf(not hasattr(client, 'HTTPSConnection'),
1138 'http.client.HTTPSConnection not available')
Łukasz Langaa5a9a9c2011-10-18 21:17:39 +02001139 def test_host_port(self):
1140 # Check invalid host_port
1141
1142 for hp in ("www.python.org:abc", "user:password@www.python.org"):
1143 self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp)
1144
1145 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
1146 "fe80::207:e9ff:fe9b", 8000),
1147 ("www.python.org:443", "www.python.org", 443),
1148 ("www.python.org:", "www.python.org", 443),
1149 ("www.python.org", "www.python.org", 443),
1150 ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443),
1151 ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b",
1152 443)):
1153 c = client.HTTPSConnection(hp)
1154 self.assertEqual(h, c.host)
1155 self.assertEqual(p, c.port)
1156
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001157
Jeremy Hylton236654b2009-03-27 20:24:34 +00001158class RequestBodyTest(TestCase):
1159 """Test cases where a request includes a message body."""
1160
1161 def setUp(self):
Jeremy Hylton7c1692d2009-03-27 21:31:03 +00001162 self.conn = client.HTTPConnection('example.com')
Jeremy Hylton636950f2009-03-28 04:34:21 +00001163 self.conn.sock = self.sock = FakeSocket("")
Jeremy Hylton236654b2009-03-27 20:24:34 +00001164 self.conn.sock = self.sock
1165
1166 def get_headers_and_fp(self):
1167 f = io.BytesIO(self.sock.data)
1168 f.readline() # read the request line
Jeremy Hylton7c1692d2009-03-27 21:31:03 +00001169 message = client.parse_headers(f)
Jeremy Hylton236654b2009-03-27 20:24:34 +00001170 return message, f
1171
1172 def test_manual_content_length(self):
1173 # Set an incorrect content-length so that we can verify that
1174 # it will not be over-ridden by the library.
1175 self.conn.request("PUT", "/url", "body",
1176 {"Content-Length": "42"})
1177 message, f = self.get_headers_and_fp()
1178 self.assertEqual("42", message.get("content-length"))
1179 self.assertEqual(4, len(f.read()))
1180
1181 def test_ascii_body(self):
1182 self.conn.request("PUT", "/url", "body")
1183 message, f = self.get_headers_and_fp()
1184 self.assertEqual("text/plain", message.get_content_type())
Raymond Hettinger7beae8a2011-01-06 05:34:17 +00001185 self.assertIsNone(message.get_charset())
Jeremy Hylton236654b2009-03-27 20:24:34 +00001186 self.assertEqual("4", message.get("content-length"))
1187 self.assertEqual(b'body', f.read())
1188
1189 def test_latin1_body(self):
1190 self.conn.request("PUT", "/url", "body\xc1")
1191 message, f = self.get_headers_and_fp()
1192 self.assertEqual("text/plain", message.get_content_type())
Raymond Hettinger7beae8a2011-01-06 05:34:17 +00001193 self.assertIsNone(message.get_charset())
Jeremy Hylton236654b2009-03-27 20:24:34 +00001194 self.assertEqual("5", message.get("content-length"))
1195 self.assertEqual(b'body\xc1', f.read())
1196
1197 def test_bytes_body(self):
1198 self.conn.request("PUT", "/url", b"body\xc1")
1199 message, f = self.get_headers_and_fp()
1200 self.assertEqual("text/plain", message.get_content_type())
Raymond Hettinger7beae8a2011-01-06 05:34:17 +00001201 self.assertIsNone(message.get_charset())
Jeremy Hylton236654b2009-03-27 20:24:34 +00001202 self.assertEqual("5", message.get("content-length"))
1203 self.assertEqual(b'body\xc1', f.read())
1204
1205 def test_file_body(self):
Victor Stinner18d15cb2011-09-21 01:09:04 +02001206 self.addCleanup(support.unlink, support.TESTFN)
Brett Cannon77b7de62010-10-29 23:31:11 +00001207 with open(support.TESTFN, "w") as f:
1208 f.write("body")
1209 with open(support.TESTFN) as f:
1210 self.conn.request("PUT", "/url", f)
1211 message, f = self.get_headers_and_fp()
1212 self.assertEqual("text/plain", message.get_content_type())
Raymond Hettinger7beae8a2011-01-06 05:34:17 +00001213 self.assertIsNone(message.get_charset())
Brett Cannon77b7de62010-10-29 23:31:11 +00001214 self.assertEqual("4", message.get("content-length"))
1215 self.assertEqual(b'body', f.read())
Jeremy Hylton236654b2009-03-27 20:24:34 +00001216
1217 def test_binary_file_body(self):
Victor Stinner18d15cb2011-09-21 01:09:04 +02001218 self.addCleanup(support.unlink, support.TESTFN)
Brett Cannon77b7de62010-10-29 23:31:11 +00001219 with open(support.TESTFN, "wb") as f:
1220 f.write(b"body\xc1")
1221 with open(support.TESTFN, "rb") as f:
1222 self.conn.request("PUT", "/url", f)
1223 message, f = self.get_headers_and_fp()
1224 self.assertEqual("text/plain", message.get_content_type())
Raymond Hettinger7beae8a2011-01-06 05:34:17 +00001225 self.assertIsNone(message.get_charset())
Brett Cannon77b7de62010-10-29 23:31:11 +00001226 self.assertEqual("5", message.get("content-length"))
1227 self.assertEqual(b'body\xc1', f.read())
Jeremy Hylton236654b2009-03-27 20:24:34 +00001228
Senthil Kumaran9f8dc442010-08-02 11:04:58 +00001229
1230class HTTPResponseTest(TestCase):
1231
1232 def setUp(self):
1233 body = "HTTP/1.1 200 Ok\r\nMy-Header: first-value\r\nMy-Header: \
1234 second-value\r\n\r\nText"
1235 sock = FakeSocket(body)
1236 self.resp = client.HTTPResponse(sock)
1237 self.resp.begin()
1238
1239 def test_getting_header(self):
1240 header = self.resp.getheader('My-Header')
1241 self.assertEqual(header, 'first-value, second-value')
1242
1243 header = self.resp.getheader('My-Header', 'some default')
1244 self.assertEqual(header, 'first-value, second-value')
1245
1246 def test_getting_nonexistent_header_with_string_default(self):
1247 header = self.resp.getheader('No-Such-Header', 'default-value')
1248 self.assertEqual(header, 'default-value')
1249
1250 def test_getting_nonexistent_header_with_iterable_default(self):
1251 header = self.resp.getheader('No-Such-Header', ['default', 'values'])
1252 self.assertEqual(header, 'default, values')
1253
1254 header = self.resp.getheader('No-Such-Header', ('default', 'values'))
1255 self.assertEqual(header, 'default, values')
1256
1257 def test_getting_nonexistent_header_without_default(self):
1258 header = self.resp.getheader('No-Such-Header')
1259 self.assertEqual(header, None)
1260
1261 def test_getting_header_defaultint(self):
1262 header = self.resp.getheader('No-Such-Header',default=42)
1263 self.assertEqual(header, 42)
1264
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001265class TunnelTests(TestCase):
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001266 def setUp(self):
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001267 response_text = (
1268 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT
1269 'HTTP/1.1 200 OK\r\n' # Reply to HEAD
1270 'Content-Length: 42\r\n\r\n'
1271 )
1272
1273 def create_connection(address, timeout=None, source_address=None):
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001274 return FakeSocket(response_text, host=address[0], port=address[1])
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001275
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001276 self.host = 'proxy.com'
1277 self.conn = client.HTTPConnection(self.host)
1278 self.conn._create_connection = create_connection
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001279
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001280 def tearDown(self):
1281 self.conn.close()
1282
1283 def test_set_tunnel_host_port_headers(self):
1284 tunnel_host = 'destination.com'
1285 tunnel_port = 8888
1286 tunnel_headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11)'}
1287 self.conn.set_tunnel(tunnel_host, port=tunnel_port,
1288 headers=tunnel_headers)
1289 self.conn.request('HEAD', '/', '')
1290 self.assertEqual(self.conn.sock.host, self.host)
1291 self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
1292 self.assertEqual(self.conn._tunnel_host, tunnel_host)
1293 self.assertEqual(self.conn._tunnel_port, tunnel_port)
1294 self.assertEqual(self.conn._tunnel_headers, tunnel_headers)
1295
1296 def test_disallow_set_tunnel_after_connect(self):
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001297 # Once connected, we shouldn't be able to tunnel anymore
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001298 self.conn.connect()
1299 self.assertRaises(RuntimeError, self.conn.set_tunnel,
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001300 'destination.com')
1301
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001302 def test_connect_with_tunnel(self):
1303 self.conn.set_tunnel('destination.com')
1304 self.conn.request('HEAD', '/', '')
1305 self.assertEqual(self.conn.sock.host, self.host)
1306 self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
1307 self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
Serhiy Storchaka4ac7ed92014-12-12 09:29:15 +02001308 # issue22095
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001309 self.assertNotIn(b'Host: destination.com:None', self.conn.sock.data)
1310 self.assertIn(b'Host: destination.com', self.conn.sock.data)
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001311
1312 # This test should be removed when CONNECT gets the HTTP/1.1 blessing
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001313 self.assertNotIn(b'Host: proxy.com', self.conn.sock.data)
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001314
Senthil Kumarane6cc7012015-01-24 19:24:59 -08001315 def test_connect_put_request(self):
1316 self.conn.set_tunnel('destination.com')
1317 self.conn.request('PUT', '/', '')
1318 self.assertEqual(self.conn.sock.host, self.host)
1319 self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
1320 self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
1321 self.assertIn(b'Host: destination.com', self.conn.sock.data)
1322
1323
Senthil Kumaran9da047b2014-04-14 13:07:56 -04001324
Benjamin Peterson9566de12014-12-13 16:13:24 -05001325@support.reap_threads
Jeremy Hylton2c178252004-08-07 16:28:14 +00001326def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001327 support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
Antoine Pitrou803e6d62010-10-13 10:36:15 +00001328 HTTPSTest, RequestBodyTest, SourceAddressTest,
Kristján Valur Jónsson8e5d0ca2014-03-19 10:07:26 +00001329 HTTPResponseTest, ExtendedReadTest,
Senthil Kumaran166214c2014-04-14 13:10:05 -04001330 ExtendedReadTestChunked, TunnelTests)
Jeremy Hylton2c178252004-08-07 16:28:14 +00001331
Thomas Wouters89f507f2006-12-13 04:49:30 +00001332if __name__ == '__main__':
1333 test_main()