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