Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 1 | import errno |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 2 | from http import client |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 3 | import io |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 4 | import os |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 5 | import array |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import socket |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 7 | |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | TestCase = unittest.TestCase |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 13 | here = os.path.dirname(__file__) |
| 14 | # Self-signed cert file for 'localhost' |
| 15 | CERT_localhost = os.path.join(here, 'keycert.pem') |
| 16 | # Self-signed cert file for 'fakehostname' |
| 17 | CERT_fakehostname = os.path.join(here, 'keycert2.pem') |
| 18 | # Root cert file (CA) for svn.python.org's cert |
| 19 | CACERT_svn_python_org = os.path.join(here, 'https_svn_python_org_root.pem') |
| 20 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 21 | HOST = support.HOST |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 22 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 23 | class FakeSocket: |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 24 | def __init__(self, text, fileclass=io.BytesIO): |
| 25 | if isinstance(text, str): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 26 | text = text.encode("ascii") |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 27 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 28 | self.fileclass = fileclass |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 29 | self.data = b'' |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 30 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 31 | def sendall(self, data): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | self.data += data |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 34 | def makefile(self, mode, bufsize=None): |
| 35 | if mode != 'r' and mode != 'rb': |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 36 | raise client.UnimplementedFileMode() |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 37 | return self.fileclass(self.text) |
| 38 | |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 39 | class EPipeSocket(FakeSocket): |
| 40 | |
| 41 | def __init__(self, text, pipe_trigger): |
| 42 | # When sendall() is called with pipe_trigger, raise EPIPE. |
| 43 | FakeSocket.__init__(self, text) |
| 44 | self.pipe_trigger = pipe_trigger |
| 45 | |
| 46 | def sendall(self, data): |
| 47 | if self.pipe_trigger in data: |
| 48 | raise socket.error(errno.EPIPE, "gotcha") |
| 49 | self.data += data |
| 50 | |
| 51 | def close(self): |
| 52 | pass |
| 53 | |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 54 | class NoEOFStringIO(io.BytesIO): |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 55 | """Like StringIO, but raises AssertionError on EOF. |
| 56 | |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 57 | This is used below to test that http.client doesn't try to read |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 58 | more from the underlying file than it should. |
| 59 | """ |
| 60 | def read(self, n=-1): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 61 | data = io.BytesIO.read(self, n) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 62 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 63 | raise AssertionError('caller tried to read past EOF') |
| 64 | return data |
| 65 | |
| 66 | def readline(self, length=None): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 67 | data = io.BytesIO.readline(self, length) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 68 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 69 | raise AssertionError('caller tried to read past EOF') |
| 70 | return data |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 71 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 72 | class HeaderTests(TestCase): |
| 73 | def test_auto_headers(self): |
| 74 | # Some headers are added automatically, but should not be added by |
| 75 | # .request() if they are explicitly set. |
| 76 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 77 | class HeaderCountingBuffer(list): |
| 78 | def __init__(self): |
| 79 | self.count = {} |
| 80 | def append(self, item): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 81 | kv = item.split(b':') |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 82 | if len(kv) > 1: |
| 83 | # item is a 'Key: Value' header string |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 84 | lcKey = kv[0].decode('ascii').lower() |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 85 | self.count.setdefault(lcKey, 0) |
| 86 | self.count[lcKey] += 1 |
| 87 | list.append(self, item) |
| 88 | |
| 89 | for explicit_header in True, False: |
| 90 | for header in 'Content-length', 'Host', 'Accept-encoding': |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 91 | conn = client.HTTPConnection('example.com') |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 92 | conn.sock = FakeSocket('blahblahblah') |
| 93 | conn._buffer = HeaderCountingBuffer() |
| 94 | |
| 95 | body = 'spamspamspam' |
| 96 | headers = {} |
| 97 | if explicit_header: |
| 98 | headers[header] = str(len(body)) |
| 99 | conn.request('POST', '/', body, headers) |
| 100 | self.assertEqual(conn._buffer.count[header.lower()], 1) |
| 101 | |
Senthil Kumaran | 5fa4a89 | 2012-05-19 16:58:09 +0800 | [diff] [blame] | 102 | def test_content_length_0(self): |
| 103 | |
| 104 | class ContentLengthChecker(list): |
| 105 | def __init__(self): |
| 106 | list.__init__(self) |
| 107 | self.content_length = None |
| 108 | def append(self, item): |
| 109 | kv = item.split(b':', 1) |
| 110 | if len(kv) > 1 and kv[0].lower() == b'content-length': |
| 111 | self.content_length = kv[1].strip() |
| 112 | list.append(self, item) |
| 113 | |
| 114 | # POST with empty body |
| 115 | conn = client.HTTPConnection('example.com') |
| 116 | conn.sock = FakeSocket(None) |
| 117 | conn._buffer = ContentLengthChecker() |
| 118 | conn.request('POST', '/', '') |
| 119 | self.assertEqual(conn._buffer.content_length, b'0', |
| 120 | 'Header Content-Length not set') |
| 121 | |
| 122 | # PUT request with empty body |
| 123 | conn = client.HTTPConnection('example.com') |
| 124 | conn.sock = FakeSocket(None) |
| 125 | conn._buffer = ContentLengthChecker() |
| 126 | conn.request('PUT', '/', '') |
| 127 | self.assertEqual(conn._buffer.content_length, b'0', |
| 128 | 'Header Content-Length not set') |
| 129 | |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 130 | def test_putheader(self): |
| 131 | conn = client.HTTPConnection('example.com') |
| 132 | conn.sock = FakeSocket(None) |
| 133 | conn.putrequest('GET','/') |
| 134 | conn.putheader('Content-length', 42) |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 135 | self.assertTrue(b'Content-length: 42' in conn._buffer) |
| 136 | |
Senthil Kumaran | 74ebd9e | 2010-11-13 12:27:49 +0000 | [diff] [blame] | 137 | def test_ipv6host_header(self): |
| 138 | # Default host header on IPv6 transaction should wrapped by [] if |
| 139 | # its actual IPv6 address |
| 140 | expected = b'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \ |
| 141 | b'Accept-Encoding: identity\r\n\r\n' |
| 142 | conn = client.HTTPConnection('[2001::]:81') |
| 143 | sock = FakeSocket('') |
| 144 | conn.sock = sock |
| 145 | conn.request('GET', '/foo') |
| 146 | self.assertTrue(sock.data.startswith(expected)) |
| 147 | |
| 148 | expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \ |
| 149 | b'Accept-Encoding: identity\r\n\r\n' |
| 150 | conn = client.HTTPConnection('[2001:102A::]') |
| 151 | sock = FakeSocket('') |
| 152 | conn.sock = sock |
| 153 | conn.request('GET', '/foo') |
| 154 | self.assertTrue(sock.data.startswith(expected)) |
| 155 | |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | class BasicTest(TestCase): |
| 158 | def test_status_lines(self): |
| 159 | # Test HTTP status lines |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 160 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 161 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 162 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 163 | resp = client.HTTPResponse(sock) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 164 | resp.begin() |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 165 | self.assertEqual(resp.read(), b"Text") |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 166 | self.assertTrue(resp.isclosed()) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 167 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 168 | body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
| 169 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 170 | resp = client.HTTPResponse(sock) |
| 171 | self.assertRaises(client.BadStatusLine, resp.begin) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 172 | |
Benjamin Peterson | 11dbfd4 | 2010-03-21 22:50:04 +0000 | [diff] [blame] | 173 | def test_bad_status_repr(self): |
| 174 | exc = client.BadStatusLine('') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 175 | self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') |
Benjamin Peterson | 11dbfd4 | 2010-03-21 22:50:04 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 177 | def test_partial_reads(self): |
Antoine Pitrou | 084daa2 | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 178 | # if we have a length, the system knows when to close itself |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 179 | # same behaviour than when we read the whole thing with read() |
| 180 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 181 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 182 | resp = client.HTTPResponse(sock) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 183 | resp.begin() |
| 184 | self.assertEqual(resp.read(2), b'Te') |
| 185 | self.assertFalse(resp.isclosed()) |
| 186 | self.assertEqual(resp.read(2), b'xt') |
| 187 | self.assertTrue(resp.isclosed()) |
| 188 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 189 | def test_partial_readintos(self): |
Antoine Pitrou | d20e774 | 2012-12-15 19:22:30 +0100 | [diff] [blame] | 190 | # if we have a length, the system knows when to close itself |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 191 | # same behaviour than when we read the whole thing with read() |
| 192 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 193 | sock = FakeSocket(body) |
| 194 | resp = client.HTTPResponse(sock) |
| 195 | resp.begin() |
| 196 | b = bytearray(2) |
| 197 | n = resp.readinto(b) |
| 198 | self.assertEqual(n, 2) |
| 199 | self.assertEqual(bytes(b), b'Te') |
| 200 | self.assertFalse(resp.isclosed()) |
| 201 | n = resp.readinto(b) |
| 202 | self.assertEqual(n, 2) |
| 203 | self.assertEqual(bytes(b), b'xt') |
| 204 | self.assertTrue(resp.isclosed()) |
| 205 | |
Antoine Pitrou | 084daa2 | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 206 | def test_partial_reads_no_content_length(self): |
| 207 | # when no length is present, the socket should be gracefully closed when |
| 208 | # all data was read |
| 209 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 210 | sock = FakeSocket(body) |
| 211 | resp = client.HTTPResponse(sock) |
| 212 | resp.begin() |
| 213 | self.assertEqual(resp.read(2), b'Te') |
| 214 | self.assertFalse(resp.isclosed()) |
| 215 | self.assertEqual(resp.read(2), b'xt') |
| 216 | self.assertEqual(resp.read(1), b'') |
| 217 | self.assertTrue(resp.isclosed()) |
| 218 | |
Antoine Pitrou | d20e774 | 2012-12-15 19:22:30 +0100 | [diff] [blame] | 219 | def test_partial_readintos_no_content_length(self): |
| 220 | # when no length is present, the socket should be gracefully closed when |
| 221 | # all data was read |
| 222 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 223 | sock = FakeSocket(body) |
| 224 | resp = client.HTTPResponse(sock) |
| 225 | resp.begin() |
| 226 | b = bytearray(2) |
| 227 | n = resp.readinto(b) |
| 228 | self.assertEqual(n, 2) |
| 229 | self.assertEqual(bytes(b), b'Te') |
| 230 | self.assertFalse(resp.isclosed()) |
| 231 | n = resp.readinto(b) |
| 232 | self.assertEqual(n, 2) |
| 233 | self.assertEqual(bytes(b), b'xt') |
| 234 | n = resp.readinto(b) |
| 235 | self.assertEqual(n, 0) |
| 236 | self.assertTrue(resp.isclosed()) |
| 237 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 238 | def test_host_port(self): |
| 239 | # Check invalid host_port |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 240 | |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 241 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 242 | self.assertRaises(client.InvalidURL, client.HTTPConnection, hp) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 243 | |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 244 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
| 245 | "fe80::207:e9ff:fe9b", 8000), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 246 | ("www.python.org:80", "www.python.org", 80), |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 247 | ("www.python.org:", "www.python.org", 80), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 248 | ("www.python.org", "www.python.org", 80), |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 249 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80), |
| 250 | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 251 | c = client.HTTPConnection(hp) |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 252 | self.assertEqual(h, c.host) |
| 253 | self.assertEqual(p, c.port) |
Skip Montanaro | 10e6e0e | 2004-09-14 16:32:02 +0000 | [diff] [blame] | 254 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 255 | def test_response_headers(self): |
| 256 | # test response with multiple message headers with the same field name. |
| 257 | text = ('HTTP/1.1 200 OK\r\n' |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 258 | 'Set-Cookie: Customer="WILE_E_COYOTE"; ' |
| 259 | 'Version="1"; Path="/acme"\r\n' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 260 | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
| 261 | ' Path="/acme"\r\n' |
| 262 | '\r\n' |
| 263 | 'No body\r\n') |
| 264 | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
| 265 | ', ' |
| 266 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
| 267 | s = FakeSocket(text) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 268 | r = client.HTTPResponse(s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 269 | r.begin() |
| 270 | cookies = r.getheader("Set-Cookie") |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 271 | self.assertEqual(cookies, hdr) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 272 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 273 | def test_read_head(self): |
| 274 | # Test that the library doesn't attempt to read any data |
| 275 | # from a HEAD request. (Tickles SF bug #622042.) |
| 276 | sock = FakeSocket( |
| 277 | 'HTTP/1.1 200 OK\r\n' |
| 278 | 'Content-Length: 14432\r\n' |
| 279 | '\r\n', |
| 280 | NoEOFStringIO) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 281 | resp = client.HTTPResponse(sock, method="HEAD") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 282 | resp.begin() |
Guido van Rossum | a00f123 | 2007-09-12 19:43:09 +0000 | [diff] [blame] | 283 | if resp.read(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 284 | self.fail("Did not expect response from HEAD request") |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 286 | def test_readinto_head(self): |
| 287 | # Test that the library doesn't attempt to read any data |
| 288 | # from a HEAD request. (Tickles SF bug #622042.) |
| 289 | sock = FakeSocket( |
| 290 | 'HTTP/1.1 200 OK\r\n' |
| 291 | 'Content-Length: 14432\r\n' |
| 292 | '\r\n', |
| 293 | NoEOFStringIO) |
| 294 | resp = client.HTTPResponse(sock, method="HEAD") |
| 295 | resp.begin() |
| 296 | b = bytearray(5) |
| 297 | if resp.readinto(b) != 0: |
| 298 | self.fail("Did not expect response from HEAD request") |
| 299 | self.assertEqual(bytes(b), b'\x00'*5) |
| 300 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 301 | def test_send_file(self): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 302 | expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' |
| 303 | b'Accept-Encoding: identity\r\nContent-Length:') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 304 | |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 305 | with open(__file__, 'rb') as body: |
| 306 | conn = client.HTTPConnection('example.com') |
| 307 | sock = FakeSocket(body) |
| 308 | conn.sock = sock |
| 309 | conn.request('GET', '/foo', body) |
| 310 | self.assertTrue(sock.data.startswith(expected), '%r != %r' % |
| 311 | (sock.data[:len(expected)], expected)) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 313 | def test_send(self): |
| 314 | expected = b'this is a test this is only a test' |
| 315 | conn = client.HTTPConnection('example.com') |
| 316 | sock = FakeSocket(None) |
| 317 | conn.sock = sock |
| 318 | conn.send(expected) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 319 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 320 | sock.data = b'' |
| 321 | conn.send(array.array('b', expected)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 322 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 323 | sock.data = b'' |
| 324 | conn.send(io.BytesIO(expected)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 325 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 326 | |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 327 | def test_send_iter(self): |
| 328 | expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
| 329 | b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \ |
| 330 | b'\r\nonetwothree' |
| 331 | |
| 332 | def body(): |
| 333 | yield b"one" |
| 334 | yield b"two" |
| 335 | yield b"three" |
| 336 | |
| 337 | conn = client.HTTPConnection('example.com') |
| 338 | sock = FakeSocket("") |
| 339 | conn.sock = sock |
| 340 | conn.request('GET', '/foo', body(), {'Content-Length': '11'}) |
Victor Stinner | 04ba966 | 2011-01-04 00:04:46 +0000 | [diff] [blame] | 341 | self.assertEqual(sock.data, expected) |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 342 | |
Senthil Kumaran | eb71ad4 | 2011-08-02 18:33:41 +0800 | [diff] [blame] | 343 | def test_send_type_error(self): |
| 344 | # See: Issue #12676 |
| 345 | conn = client.HTTPConnection('example.com') |
| 346 | conn.sock = FakeSocket('') |
| 347 | with self.assertRaises(TypeError): |
| 348 | conn.request('POST', 'test', conn) |
| 349 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 350 | def test_chunked(self): |
| 351 | chunked_start = ( |
| 352 | 'HTTP/1.1 200 OK\r\n' |
| 353 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 354 | 'a\r\n' |
| 355 | 'hello worl\r\n' |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 356 | '3\r\n' |
| 357 | 'd! \r\n' |
| 358 | '8\r\n' |
| 359 | 'and now \r\n' |
| 360 | '22\r\n' |
| 361 | 'for something completely different\r\n' |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 362 | ) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 363 | expected = b'hello world! and now for something completely different' |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 364 | sock = FakeSocket(chunked_start + '0\r\n') |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 365 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 366 | resp.begin() |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 367 | self.assertEqual(resp.read(), expected) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 368 | resp.close() |
| 369 | |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 370 | # Various read sizes |
| 371 | for n in range(1, 12): |
| 372 | sock = FakeSocket(chunked_start + '0\r\n') |
| 373 | resp = client.HTTPResponse(sock, method="GET") |
| 374 | resp.begin() |
| 375 | self.assertEqual(resp.read(n) + resp.read(n) + resp.read(), expected) |
| 376 | resp.close() |
| 377 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 378 | for x in ('', 'foo\r\n'): |
| 379 | sock = FakeSocket(chunked_start + x) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 380 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 381 | resp.begin() |
| 382 | try: |
| 383 | resp.read() |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 384 | except client.IncompleteRead as i: |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 385 | self.assertEqual(i.partial, expected) |
| 386 | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
| 387 | self.assertEqual(repr(i), expected_message) |
| 388 | self.assertEqual(str(i), expected_message) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 389 | else: |
| 390 | self.fail('IncompleteRead expected') |
| 391 | finally: |
| 392 | resp.close() |
| 393 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 394 | def test_readinto_chunked(self): |
| 395 | chunked_start = ( |
| 396 | 'HTTP/1.1 200 OK\r\n' |
| 397 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 398 | 'a\r\n' |
| 399 | 'hello worl\r\n' |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 400 | '3\r\n' |
| 401 | 'd! \r\n' |
| 402 | '8\r\n' |
| 403 | 'and now \r\n' |
| 404 | '22\r\n' |
| 405 | 'for something completely different\r\n' |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 406 | ) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 407 | expected = b'hello world! and now for something completely different' |
| 408 | nexpected = len(expected) |
| 409 | b = bytearray(128) |
| 410 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 411 | sock = FakeSocket(chunked_start + '0\r\n') |
| 412 | resp = client.HTTPResponse(sock, method="GET") |
| 413 | resp.begin() |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 414 | n = resp.readinto(b) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 415 | self.assertEqual(b[:nexpected], expected) |
| 416 | self.assertEqual(n, nexpected) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 417 | resp.close() |
| 418 | |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 419 | # Various read sizes |
| 420 | for n in range(1, 12): |
| 421 | sock = FakeSocket(chunked_start + '0\r\n') |
| 422 | resp = client.HTTPResponse(sock, method="GET") |
| 423 | resp.begin() |
| 424 | m = memoryview(b) |
| 425 | i = resp.readinto(m[0:n]) |
| 426 | i += resp.readinto(m[i:n + i]) |
| 427 | i += resp.readinto(m[i:]) |
| 428 | self.assertEqual(b[:nexpected], expected) |
| 429 | self.assertEqual(i, nexpected) |
| 430 | resp.close() |
| 431 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 432 | for x in ('', 'foo\r\n'): |
| 433 | sock = FakeSocket(chunked_start + x) |
| 434 | resp = client.HTTPResponse(sock, method="GET") |
| 435 | resp.begin() |
| 436 | try: |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 437 | n = resp.readinto(b) |
| 438 | except client.IncompleteRead as i: |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 439 | self.assertEqual(i.partial, expected) |
| 440 | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
| 441 | self.assertEqual(repr(i), expected_message) |
| 442 | self.assertEqual(str(i), expected_message) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 443 | else: |
| 444 | self.fail('IncompleteRead expected') |
| 445 | finally: |
| 446 | resp.close() |
| 447 | |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 448 | def test_chunked_head(self): |
| 449 | chunked_start = ( |
| 450 | 'HTTP/1.1 200 OK\r\n' |
| 451 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 452 | 'a\r\n' |
| 453 | 'hello world\r\n' |
| 454 | '1\r\n' |
| 455 | 'd\r\n' |
| 456 | ) |
| 457 | sock = FakeSocket(chunked_start + '0\r\n') |
| 458 | resp = client.HTTPResponse(sock, method="HEAD") |
| 459 | resp.begin() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 460 | self.assertEqual(resp.read(), b'') |
| 461 | self.assertEqual(resp.status, 200) |
| 462 | self.assertEqual(resp.reason, 'OK') |
Senthil Kumaran | 0b99883 | 2010-06-04 17:27:11 +0000 | [diff] [blame] | 463 | self.assertTrue(resp.isclosed()) |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 465 | def test_readinto_chunked_head(self): |
| 466 | chunked_start = ( |
| 467 | 'HTTP/1.1 200 OK\r\n' |
| 468 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 469 | 'a\r\n' |
| 470 | 'hello world\r\n' |
| 471 | '1\r\n' |
| 472 | 'd\r\n' |
| 473 | ) |
| 474 | sock = FakeSocket(chunked_start + '0\r\n') |
| 475 | resp = client.HTTPResponse(sock, method="HEAD") |
| 476 | resp.begin() |
| 477 | b = bytearray(5) |
| 478 | n = resp.readinto(b) |
| 479 | self.assertEqual(n, 0) |
| 480 | self.assertEqual(bytes(b), b'\x00'*5) |
| 481 | self.assertEqual(resp.status, 200) |
| 482 | self.assertEqual(resp.reason, 'OK') |
| 483 | self.assertTrue(resp.isclosed()) |
| 484 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 485 | def test_negative_content_length(self): |
Jeremy Hylton | 8206695 | 2008-12-15 03:08:30 +0000 | [diff] [blame] | 486 | sock = FakeSocket( |
| 487 | 'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n') |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 488 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 489 | resp.begin() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 490 | self.assertEqual(resp.read(), b'Hello\r\n') |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 491 | resp.close() |
| 492 | |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 493 | def test_incomplete_read(self): |
| 494 | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n') |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 495 | resp = client.HTTPResponse(sock, method="GET") |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 496 | resp.begin() |
| 497 | try: |
| 498 | resp.read() |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 499 | except client.IncompleteRead as i: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 500 | self.assertEqual(i.partial, b'Hello\r\n') |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 501 | self.assertEqual(repr(i), |
| 502 | "IncompleteRead(7 bytes read, 3 more expected)") |
| 503 | self.assertEqual(str(i), |
| 504 | "IncompleteRead(7 bytes read, 3 more expected)") |
| 505 | else: |
| 506 | self.fail('IncompleteRead expected') |
| 507 | finally: |
| 508 | resp.close() |
| 509 | |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 510 | def test_epipe(self): |
| 511 | sock = EPipeSocket( |
| 512 | "HTTP/1.0 401 Authorization Required\r\n" |
| 513 | "Content-type: text/html\r\n" |
| 514 | "WWW-Authenticate: Basic realm=\"example\"\r\n", |
| 515 | b"Content-Length") |
| 516 | conn = client.HTTPConnection("example.com") |
| 517 | conn.sock = sock |
| 518 | self.assertRaises(socket.error, |
| 519 | lambda: conn.request("PUT", "/url", "body")) |
| 520 | resp = conn.getresponse() |
| 521 | self.assertEqual(401, resp.status) |
| 522 | self.assertEqual("Basic realm=\"example\"", |
| 523 | resp.getheader("www-authenticate")) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 524 | |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 525 | # Test lines overflowing the max line size (_MAXLINE in http.client) |
| 526 | |
| 527 | def test_overflowing_status_line(self): |
| 528 | body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n" |
| 529 | resp = client.HTTPResponse(FakeSocket(body)) |
| 530 | self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin) |
| 531 | |
| 532 | def test_overflowing_header_line(self): |
| 533 | body = ( |
| 534 | 'HTTP/1.1 200 OK\r\n' |
| 535 | 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n' |
| 536 | ) |
| 537 | resp = client.HTTPResponse(FakeSocket(body)) |
| 538 | self.assertRaises(client.LineTooLong, resp.begin) |
| 539 | |
| 540 | def test_overflowing_chunked_line(self): |
| 541 | body = ( |
| 542 | 'HTTP/1.1 200 OK\r\n' |
| 543 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 544 | + '0' * 65536 + 'a\r\n' |
| 545 | 'hello world\r\n' |
| 546 | '0\r\n' |
| 547 | ) |
| 548 | resp = client.HTTPResponse(FakeSocket(body)) |
| 549 | resp.begin() |
| 550 | self.assertRaises(client.LineTooLong, resp.read) |
| 551 | |
Senthil Kumaran | 9c29f86 | 2012-04-29 10:20:46 +0800 | [diff] [blame] | 552 | def test_early_eof(self): |
| 553 | # Test httpresponse with no \r\n termination, |
| 554 | body = "HTTP/1.1 200 Ok" |
| 555 | sock = FakeSocket(body) |
| 556 | resp = client.HTTPResponse(sock) |
| 557 | resp.begin() |
| 558 | self.assertEqual(resp.read(), b'') |
| 559 | self.assertTrue(resp.isclosed()) |
| 560 | |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 561 | class OfflineTest(TestCase): |
| 562 | def test_responses(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 563 | self.assertEqual(client.responses[client.NOT_FOUND], "Not Found") |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 564 | |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 565 | |
| 566 | class SourceAddressTest(TestCase): |
| 567 | def setUp(self): |
| 568 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 569 | self.port = support.bind_port(self.serv) |
| 570 | self.source_port = support.find_unused_port() |
| 571 | self.serv.listen(5) |
| 572 | self.conn = None |
| 573 | |
| 574 | def tearDown(self): |
| 575 | if self.conn: |
| 576 | self.conn.close() |
| 577 | self.conn = None |
| 578 | self.serv.close() |
| 579 | self.serv = None |
| 580 | |
| 581 | def testHTTPConnectionSourceAddress(self): |
| 582 | self.conn = client.HTTPConnection(HOST, self.port, |
| 583 | source_address=('', self.source_port)) |
| 584 | self.conn.connect() |
| 585 | self.assertEqual(self.conn.sock.getsockname()[1], self.source_port) |
| 586 | |
| 587 | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
| 588 | 'http.client.HTTPSConnection not defined') |
| 589 | def testHTTPSConnectionSourceAddress(self): |
| 590 | self.conn = client.HTTPSConnection(HOST, self.port, |
| 591 | source_address=('', self.source_port)) |
| 592 | # We don't test anything here other the constructor not barfing as |
| 593 | # this code doesn't deal with setting up an active running SSL server |
| 594 | # for an ssl_wrapped connect() to actually return from. |
| 595 | |
| 596 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 597 | class TimeoutTest(TestCase): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 598 | PORT = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 599 | |
| 600 | def setUp(self): |
| 601 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 602 | TimeoutTest.PORT = support.bind_port(self.serv) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 603 | self.serv.listen(5) |
| 604 | |
| 605 | def tearDown(self): |
| 606 | self.serv.close() |
| 607 | self.serv = None |
| 608 | |
| 609 | def testTimeoutAttribute(self): |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 610 | # This will prove that the timeout gets through HTTPConnection |
| 611 | # and into the socket. |
| 612 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 613 | # default -- use global socket timeout |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 614 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 615 | socket.setdefaulttimeout(30) |
| 616 | try: |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 617 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 618 | httpConn.connect() |
| 619 | finally: |
| 620 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 621 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 622 | httpConn.close() |
| 623 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 624 | # no timeout -- do not use global socket default |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 625 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 626 | socket.setdefaulttimeout(30) |
| 627 | try: |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 628 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 629 | timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 630 | httpConn.connect() |
| 631 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 632 | socket.setdefaulttimeout(None) |
| 633 | self.assertEqual(httpConn.sock.gettimeout(), None) |
| 634 | httpConn.close() |
| 635 | |
| 636 | # a value |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 637 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 638 | httpConn.connect() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 639 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 640 | httpConn.close() |
| 641 | |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 642 | |
| 643 | class HTTPSTest(TestCase): |
| 644 | |
| 645 | def setUp(self): |
| 646 | if not hasattr(client, 'HTTPSConnection'): |
| 647 | self.skipTest('ssl support required') |
| 648 | |
| 649 | def make_server(self, certfile): |
| 650 | from test.ssl_servers import make_https_server |
| 651 | return make_https_server(self, certfile) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 652 | |
| 653 | def test_attributes(self): |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 654 | # simple test to check it's storing the timeout |
| 655 | h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
| 656 | self.assertEqual(h.timeout, 30) |
| 657 | |
| 658 | def _check_svn_python_org(self, resp): |
| 659 | # Just a simple check that everything went fine |
| 660 | server_string = resp.getheader('server') |
| 661 | self.assertIn('Apache', server_string) |
| 662 | |
| 663 | def test_networked(self): |
| 664 | # Default settings: no cert verification is done |
| 665 | support.requires('network') |
| 666 | with support.transient_internet('svn.python.org'): |
| 667 | h = client.HTTPSConnection('svn.python.org', 443) |
| 668 | h.request('GET', '/') |
| 669 | resp = h.getresponse() |
| 670 | self._check_svn_python_org(resp) |
| 671 | |
| 672 | def test_networked_good_cert(self): |
| 673 | # We feed a CA cert that validates the server's cert |
| 674 | import ssl |
| 675 | support.requires('network') |
| 676 | with support.transient_internet('svn.python.org'): |
| 677 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 678 | context.verify_mode = ssl.CERT_REQUIRED |
| 679 | context.load_verify_locations(CACERT_svn_python_org) |
| 680 | h = client.HTTPSConnection('svn.python.org', 443, context=context) |
| 681 | h.request('GET', '/') |
| 682 | resp = h.getresponse() |
| 683 | self._check_svn_python_org(resp) |
| 684 | |
| 685 | def test_networked_bad_cert(self): |
| 686 | # We feed a "CA" cert that is unrelated to the server's cert |
| 687 | import ssl |
| 688 | support.requires('network') |
| 689 | with support.transient_internet('svn.python.org'): |
| 690 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 691 | context.verify_mode = ssl.CERT_REQUIRED |
| 692 | context.load_verify_locations(CERT_localhost) |
| 693 | h = client.HTTPSConnection('svn.python.org', 443, context=context) |
| 694 | with self.assertRaises(ssl.SSLError): |
| 695 | h.request('GET', '/') |
| 696 | |
| 697 | def test_local_good_hostname(self): |
| 698 | # The (valid) cert validates the HTTP hostname |
| 699 | import ssl |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 700 | server = self.make_server(CERT_localhost) |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 701 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 702 | context.verify_mode = ssl.CERT_REQUIRED |
| 703 | context.load_verify_locations(CERT_localhost) |
| 704 | h = client.HTTPSConnection('localhost', server.port, context=context) |
| 705 | h.request('GET', '/nonexistent') |
| 706 | resp = h.getresponse() |
| 707 | self.assertEqual(resp.status, 404) |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 708 | del server |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 709 | |
| 710 | def test_local_bad_hostname(self): |
| 711 | # The (valid) cert doesn't validate the HTTP hostname |
| 712 | import ssl |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 713 | server = self.make_server(CERT_fakehostname) |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 714 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 715 | context.verify_mode = ssl.CERT_REQUIRED |
| 716 | context.load_verify_locations(CERT_fakehostname) |
| 717 | h = client.HTTPSConnection('localhost', server.port, context=context) |
| 718 | with self.assertRaises(ssl.CertificateError): |
| 719 | h.request('GET', '/') |
| 720 | # Same with explicit check_hostname=True |
| 721 | h = client.HTTPSConnection('localhost', server.port, context=context, |
| 722 | check_hostname=True) |
| 723 | with self.assertRaises(ssl.CertificateError): |
| 724 | h.request('GET', '/') |
| 725 | # With check_hostname=False, the mismatching is ignored |
| 726 | h = client.HTTPSConnection('localhost', server.port, context=context, |
| 727 | check_hostname=False) |
| 728 | h.request('GET', '/nonexistent') |
| 729 | resp = h.getresponse() |
| 730 | self.assertEqual(resp.status, 404) |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 731 | del server |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 732 | |
Petri Lehtinen | e119c40 | 2011-10-26 21:29:15 +0300 | [diff] [blame] | 733 | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
| 734 | 'http.client.HTTPSConnection not available') |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 735 | def test_host_port(self): |
| 736 | # Check invalid host_port |
| 737 | |
| 738 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
| 739 | self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp) |
| 740 | |
| 741 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
| 742 | "fe80::207:e9ff:fe9b", 8000), |
| 743 | ("www.python.org:443", "www.python.org", 443), |
| 744 | ("www.python.org:", "www.python.org", 443), |
| 745 | ("www.python.org", "www.python.org", 443), |
| 746 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443), |
| 747 | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", |
| 748 | 443)): |
| 749 | c = client.HTTPSConnection(hp) |
| 750 | self.assertEqual(h, c.host) |
| 751 | self.assertEqual(p, c.port) |
| 752 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 753 | |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 754 | class RequestBodyTest(TestCase): |
| 755 | """Test cases where a request includes a message body.""" |
| 756 | |
| 757 | def setUp(self): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 758 | self.conn = client.HTTPConnection('example.com') |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 759 | self.conn.sock = self.sock = FakeSocket("") |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 760 | self.conn.sock = self.sock |
| 761 | |
| 762 | def get_headers_and_fp(self): |
| 763 | f = io.BytesIO(self.sock.data) |
| 764 | f.readline() # read the request line |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 765 | message = client.parse_headers(f) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 766 | return message, f |
| 767 | |
| 768 | def test_manual_content_length(self): |
| 769 | # Set an incorrect content-length so that we can verify that |
| 770 | # it will not be over-ridden by the library. |
| 771 | self.conn.request("PUT", "/url", "body", |
| 772 | {"Content-Length": "42"}) |
| 773 | message, f = self.get_headers_and_fp() |
| 774 | self.assertEqual("42", message.get("content-length")) |
| 775 | self.assertEqual(4, len(f.read())) |
| 776 | |
| 777 | def test_ascii_body(self): |
| 778 | self.conn.request("PUT", "/url", "body") |
| 779 | message, f = self.get_headers_and_fp() |
| 780 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 781 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 782 | self.assertEqual("4", message.get("content-length")) |
| 783 | self.assertEqual(b'body', f.read()) |
| 784 | |
| 785 | def test_latin1_body(self): |
| 786 | self.conn.request("PUT", "/url", "body\xc1") |
| 787 | message, f = self.get_headers_and_fp() |
| 788 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 789 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 790 | self.assertEqual("5", message.get("content-length")) |
| 791 | self.assertEqual(b'body\xc1', f.read()) |
| 792 | |
| 793 | def test_bytes_body(self): |
| 794 | self.conn.request("PUT", "/url", b"body\xc1") |
| 795 | message, f = self.get_headers_and_fp() |
| 796 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 797 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 798 | self.assertEqual("5", message.get("content-length")) |
| 799 | self.assertEqual(b'body\xc1', f.read()) |
| 800 | |
| 801 | def test_file_body(self): |
Victor Stinner | 18d15cb | 2011-09-21 01:09:04 +0200 | [diff] [blame] | 802 | self.addCleanup(support.unlink, support.TESTFN) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 803 | with open(support.TESTFN, "w") as f: |
| 804 | f.write("body") |
| 805 | with open(support.TESTFN) as f: |
| 806 | self.conn.request("PUT", "/url", f) |
| 807 | message, f = self.get_headers_and_fp() |
| 808 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 809 | self.assertIsNone(message.get_charset()) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 810 | self.assertEqual("4", message.get("content-length")) |
| 811 | self.assertEqual(b'body', f.read()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 812 | |
| 813 | def test_binary_file_body(self): |
Victor Stinner | 18d15cb | 2011-09-21 01:09:04 +0200 | [diff] [blame] | 814 | self.addCleanup(support.unlink, support.TESTFN) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 815 | with open(support.TESTFN, "wb") as f: |
| 816 | f.write(b"body\xc1") |
| 817 | with open(support.TESTFN, "rb") as f: |
| 818 | self.conn.request("PUT", "/url", f) |
| 819 | message, f = self.get_headers_and_fp() |
| 820 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 821 | self.assertIsNone(message.get_charset()) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 822 | self.assertEqual("5", message.get("content-length")) |
| 823 | self.assertEqual(b'body\xc1', f.read()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 824 | |
Senthil Kumaran | 9f8dc44 | 2010-08-02 11:04:58 +0000 | [diff] [blame] | 825 | |
| 826 | class HTTPResponseTest(TestCase): |
| 827 | |
| 828 | def setUp(self): |
| 829 | body = "HTTP/1.1 200 Ok\r\nMy-Header: first-value\r\nMy-Header: \ |
| 830 | second-value\r\n\r\nText" |
| 831 | sock = FakeSocket(body) |
| 832 | self.resp = client.HTTPResponse(sock) |
| 833 | self.resp.begin() |
| 834 | |
| 835 | def test_getting_header(self): |
| 836 | header = self.resp.getheader('My-Header') |
| 837 | self.assertEqual(header, 'first-value, second-value') |
| 838 | |
| 839 | header = self.resp.getheader('My-Header', 'some default') |
| 840 | self.assertEqual(header, 'first-value, second-value') |
| 841 | |
| 842 | def test_getting_nonexistent_header_with_string_default(self): |
| 843 | header = self.resp.getheader('No-Such-Header', 'default-value') |
| 844 | self.assertEqual(header, 'default-value') |
| 845 | |
| 846 | def test_getting_nonexistent_header_with_iterable_default(self): |
| 847 | header = self.resp.getheader('No-Such-Header', ['default', 'values']) |
| 848 | self.assertEqual(header, 'default, values') |
| 849 | |
| 850 | header = self.resp.getheader('No-Such-Header', ('default', 'values')) |
| 851 | self.assertEqual(header, 'default, values') |
| 852 | |
| 853 | def test_getting_nonexistent_header_without_default(self): |
| 854 | header = self.resp.getheader('No-Such-Header') |
| 855 | self.assertEqual(header, None) |
| 856 | |
| 857 | def test_getting_header_defaultint(self): |
| 858 | header = self.resp.getheader('No-Such-Header',default=42) |
| 859 | self.assertEqual(header, 42) |
| 860 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 861 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 862 | support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 863 | HTTPSTest, RequestBodyTest, SourceAddressTest, |
Senthil Kumaran | 9f8dc44 | 2010-08-02 11:04:58 +0000 | [diff] [blame] | 864 | HTTPResponseTest) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 865 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 866 | if __name__ == '__main__': |
| 867 | test_main() |