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 | |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 21 | # constants for testing chunked encoding |
| 22 | chunked_start = ( |
| 23 | 'HTTP/1.1 200 OK\r\n' |
| 24 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 25 | 'a\r\n' |
| 26 | 'hello worl\r\n' |
| 27 | '3\r\n' |
| 28 | 'd! \r\n' |
| 29 | '8\r\n' |
| 30 | 'and now \r\n' |
| 31 | '22\r\n' |
| 32 | 'for something completely different\r\n' |
| 33 | ) |
| 34 | chunked_expected = b'hello world! and now for something completely different' |
| 35 | chunk_extension = ";foo=bar" |
| 36 | last_chunk = "0\r\n" |
| 37 | last_chunk_extended = "0" + chunk_extension + "\r\n" |
| 38 | trailers = "X-Dummy: foo\r\nX-Dumm2: bar\r\n" |
| 39 | chunked_end = "\r\n" |
| 40 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 41 | HOST = support.HOST |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 43 | class FakeSocket: |
Senthil Kumaran | 9da047b | 2014-04-14 13:07:56 -0400 | [diff] [blame] | 44 | def __init__(self, text, fileclass=io.BytesIO, host=None, port=None): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 45 | if isinstance(text, str): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 46 | text = text.encode("ascii") |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 47 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 48 | self.fileclass = fileclass |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 49 | self.data = b'' |
Antoine Pitrou | 90e4774 | 2013-01-02 22:10:47 +0100 | [diff] [blame] | 50 | self.sendall_calls = 0 |
Senthil Kumaran | 9da047b | 2014-04-14 13:07:56 -0400 | [diff] [blame] | 51 | self.host = host |
| 52 | self.port = port |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 53 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 54 | def sendall(self, data): |
Antoine Pitrou | 90e4774 | 2013-01-02 22:10:47 +0100 | [diff] [blame] | 55 | self.sendall_calls += 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | self.data += data |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 57 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 58 | def makefile(self, mode, bufsize=None): |
| 59 | if mode != 'r' and mode != 'rb': |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 60 | raise client.UnimplementedFileMode() |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 61 | # keep the file around so we can check how much was read from it |
| 62 | self.file = self.fileclass(self.text) |
| 63 | self.file.close = lambda:None #nerf close () |
| 64 | return self.file |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 65 | |
Senthil Kumaran | 9da047b | 2014-04-14 13:07:56 -0400 | [diff] [blame] | 66 | def close(self): |
| 67 | pass |
| 68 | |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 69 | class EPipeSocket(FakeSocket): |
| 70 | |
| 71 | def __init__(self, text, pipe_trigger): |
| 72 | # When sendall() is called with pipe_trigger, raise EPIPE. |
| 73 | FakeSocket.__init__(self, text) |
| 74 | self.pipe_trigger = pipe_trigger |
| 75 | |
| 76 | def sendall(self, data): |
| 77 | if self.pipe_trigger in data: |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 78 | raise OSError(errno.EPIPE, "gotcha") |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 79 | self.data += data |
| 80 | |
| 81 | def close(self): |
| 82 | pass |
| 83 | |
Serhiy Storchaka | 50254c5 | 2013-08-29 11:35:43 +0300 | [diff] [blame] | 84 | class NoEOFBytesIO(io.BytesIO): |
| 85 | """Like BytesIO, but raises AssertionError on EOF. |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 86 | |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 87 | 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] | 88 | more from the underlying file than it should. |
| 89 | """ |
| 90 | def read(self, n=-1): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 91 | data = io.BytesIO.read(self, n) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 92 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 93 | raise AssertionError('caller tried to read past EOF') |
| 94 | return data |
| 95 | |
| 96 | def readline(self, length=None): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 97 | data = io.BytesIO.readline(self, length) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 98 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 99 | raise AssertionError('caller tried to read past EOF') |
| 100 | return data |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 101 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 102 | class HeaderTests(TestCase): |
| 103 | def test_auto_headers(self): |
| 104 | # Some headers are added automatically, but should not be added by |
| 105 | # .request() if they are explicitly set. |
| 106 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 107 | class HeaderCountingBuffer(list): |
| 108 | def __init__(self): |
| 109 | self.count = {} |
| 110 | def append(self, item): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 111 | kv = item.split(b':') |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 112 | if len(kv) > 1: |
| 113 | # item is a 'Key: Value' header string |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 114 | lcKey = kv[0].decode('ascii').lower() |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 115 | self.count.setdefault(lcKey, 0) |
| 116 | self.count[lcKey] += 1 |
| 117 | list.append(self, item) |
| 118 | |
| 119 | for explicit_header in True, False: |
| 120 | for header in 'Content-length', 'Host', 'Accept-encoding': |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 121 | conn = client.HTTPConnection('example.com') |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 122 | conn.sock = FakeSocket('blahblahblah') |
| 123 | conn._buffer = HeaderCountingBuffer() |
| 124 | |
| 125 | body = 'spamspamspam' |
| 126 | headers = {} |
| 127 | if explicit_header: |
| 128 | headers[header] = str(len(body)) |
| 129 | conn.request('POST', '/', body, headers) |
| 130 | self.assertEqual(conn._buffer.count[header.lower()], 1) |
| 131 | |
Senthil Kumaran | 5fa4a89 | 2012-05-19 16:58:09 +0800 | [diff] [blame] | 132 | def test_content_length_0(self): |
| 133 | |
| 134 | class ContentLengthChecker(list): |
| 135 | def __init__(self): |
| 136 | list.__init__(self) |
| 137 | self.content_length = None |
| 138 | def append(self, item): |
| 139 | kv = item.split(b':', 1) |
| 140 | if len(kv) > 1 and kv[0].lower() == b'content-length': |
| 141 | self.content_length = kv[1].strip() |
| 142 | list.append(self, item) |
| 143 | |
| 144 | # POST with empty body |
| 145 | conn = client.HTTPConnection('example.com') |
| 146 | conn.sock = FakeSocket(None) |
| 147 | conn._buffer = ContentLengthChecker() |
| 148 | conn.request('POST', '/', '') |
| 149 | self.assertEqual(conn._buffer.content_length, b'0', |
| 150 | 'Header Content-Length not set') |
| 151 | |
| 152 | # PUT request with empty body |
| 153 | conn = client.HTTPConnection('example.com') |
| 154 | conn.sock = FakeSocket(None) |
| 155 | conn._buffer = ContentLengthChecker() |
| 156 | conn.request('PUT', '/', '') |
| 157 | self.assertEqual(conn._buffer.content_length, b'0', |
| 158 | 'Header Content-Length not set') |
| 159 | |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 160 | def test_putheader(self): |
| 161 | conn = client.HTTPConnection('example.com') |
| 162 | conn.sock = FakeSocket(None) |
| 163 | conn.putrequest('GET','/') |
| 164 | conn.putheader('Content-length', 42) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 165 | self.assertIn(b'Content-length: 42', conn._buffer) |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 166 | |
Senthil Kumaran | 74ebd9e | 2010-11-13 12:27:49 +0000 | [diff] [blame] | 167 | def test_ipv6host_header(self): |
| 168 | # Default host header on IPv6 transaction should wrapped by [] if |
| 169 | # its actual IPv6 address |
| 170 | expected = b'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \ |
| 171 | b'Accept-Encoding: identity\r\n\r\n' |
| 172 | conn = client.HTTPConnection('[2001::]:81') |
| 173 | sock = FakeSocket('') |
| 174 | conn.sock = sock |
| 175 | conn.request('GET', '/foo') |
| 176 | self.assertTrue(sock.data.startswith(expected)) |
| 177 | |
| 178 | expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \ |
| 179 | b'Accept-Encoding: identity\r\n\r\n' |
| 180 | conn = client.HTTPConnection('[2001:102A::]') |
| 181 | sock = FakeSocket('') |
| 182 | conn.sock = sock |
| 183 | conn.request('GET', '/foo') |
| 184 | self.assertTrue(sock.data.startswith(expected)) |
| 185 | |
Senthil Kumaran | 58d5dbf | 2010-10-03 18:22:42 +0000 | [diff] [blame] | 186 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 187 | class BasicTest(TestCase): |
| 188 | def test_status_lines(self): |
| 189 | # Test HTTP status lines |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 190 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 191 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 192 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 193 | resp = client.HTTPResponse(sock) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 194 | resp.begin() |
Serhiy Storchaka | 1c84ac1 | 2013-12-17 21:50:02 +0200 | [diff] [blame] | 195 | self.assertEqual(resp.read(0), b'') # Issue #20007 |
| 196 | self.assertFalse(resp.isclosed()) |
| 197 | self.assertFalse(resp.closed) |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 198 | self.assertEqual(resp.read(), b"Text") |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 199 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 200 | self.assertFalse(resp.closed) |
| 201 | resp.close() |
| 202 | self.assertTrue(resp.closed) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 204 | body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
| 205 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 206 | resp = client.HTTPResponse(sock) |
| 207 | self.assertRaises(client.BadStatusLine, resp.begin) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 11dbfd4 | 2010-03-21 22:50:04 +0000 | [diff] [blame] | 209 | def test_bad_status_repr(self): |
| 210 | exc = client.BadStatusLine('') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 211 | self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') |
Benjamin Peterson | 11dbfd4 | 2010-03-21 22:50:04 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 213 | def test_partial_reads(self): |
Antoine Pitrou | 084daa2 | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 214 | # 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] | 215 | # same behaviour than when we read the whole thing with read() |
| 216 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 217 | sock = FakeSocket(body) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 218 | resp = client.HTTPResponse(sock) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 219 | resp.begin() |
| 220 | self.assertEqual(resp.read(2), b'Te') |
| 221 | self.assertFalse(resp.isclosed()) |
| 222 | self.assertEqual(resp.read(2), b'xt') |
| 223 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 224 | self.assertFalse(resp.closed) |
| 225 | resp.close() |
| 226 | self.assertTrue(resp.closed) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 228 | def test_partial_readintos(self): |
Antoine Pitrou | d20e774 | 2012-12-15 19:22:30 +0100 | [diff] [blame] | 229 | # if we have a length, the system knows when to close itself |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 230 | # same behaviour than when we read the whole thing with read() |
| 231 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 232 | sock = FakeSocket(body) |
| 233 | resp = client.HTTPResponse(sock) |
| 234 | resp.begin() |
| 235 | b = bytearray(2) |
| 236 | n = resp.readinto(b) |
| 237 | self.assertEqual(n, 2) |
| 238 | self.assertEqual(bytes(b), b'Te') |
| 239 | self.assertFalse(resp.isclosed()) |
| 240 | n = resp.readinto(b) |
| 241 | self.assertEqual(n, 2) |
| 242 | self.assertEqual(bytes(b), b'xt') |
| 243 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b6c86fd | 2013-02-06 10:35:40 +0200 | [diff] [blame] | 244 | self.assertFalse(resp.closed) |
| 245 | resp.close() |
| 246 | self.assertTrue(resp.closed) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 247 | |
Antoine Pitrou | 084daa2 | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 248 | def test_partial_reads_no_content_length(self): |
| 249 | # when no length is present, the socket should be gracefully closed when |
| 250 | # all data was read |
| 251 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 252 | sock = FakeSocket(body) |
| 253 | resp = client.HTTPResponse(sock) |
| 254 | resp.begin() |
| 255 | self.assertEqual(resp.read(2), b'Te') |
| 256 | self.assertFalse(resp.isclosed()) |
| 257 | self.assertEqual(resp.read(2), b'xt') |
| 258 | self.assertEqual(resp.read(1), b'') |
| 259 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 260 | self.assertFalse(resp.closed) |
| 261 | resp.close() |
| 262 | self.assertTrue(resp.closed) |
Antoine Pitrou | 084daa2 | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 263 | |
Antoine Pitrou | d20e774 | 2012-12-15 19:22:30 +0100 | [diff] [blame] | 264 | def test_partial_readintos_no_content_length(self): |
| 265 | # when no length is present, the socket should be gracefully closed when |
| 266 | # all data was read |
| 267 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 268 | sock = FakeSocket(body) |
| 269 | resp = client.HTTPResponse(sock) |
| 270 | resp.begin() |
| 271 | b = bytearray(2) |
| 272 | n = resp.readinto(b) |
| 273 | self.assertEqual(n, 2) |
| 274 | self.assertEqual(bytes(b), b'Te') |
| 275 | self.assertFalse(resp.isclosed()) |
| 276 | n = resp.readinto(b) |
| 277 | self.assertEqual(n, 2) |
| 278 | self.assertEqual(bytes(b), b'xt') |
| 279 | n = resp.readinto(b) |
| 280 | self.assertEqual(n, 0) |
| 281 | self.assertTrue(resp.isclosed()) |
| 282 | |
Antoine Pitrou | beec61a | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 283 | def test_partial_reads_incomplete_body(self): |
| 284 | # if the server shuts down the connection before the whole |
| 285 | # content-length is delivered, the socket is gracefully closed |
| 286 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText" |
| 287 | sock = FakeSocket(body) |
| 288 | resp = client.HTTPResponse(sock) |
| 289 | resp.begin() |
| 290 | self.assertEqual(resp.read(2), b'Te') |
| 291 | self.assertFalse(resp.isclosed()) |
| 292 | self.assertEqual(resp.read(2), b'xt') |
| 293 | self.assertEqual(resp.read(1), b'') |
| 294 | self.assertTrue(resp.isclosed()) |
| 295 | |
Antoine Pitrou | 6a35e18 | 2013-02-02 23:04:56 +0100 | [diff] [blame] | 296 | def test_partial_readintos_incomplete_body(self): |
| 297 | # if the server shuts down the connection before the whole |
| 298 | # content-length is delivered, the socket is gracefully closed |
| 299 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText" |
| 300 | sock = FakeSocket(body) |
| 301 | resp = client.HTTPResponse(sock) |
| 302 | resp.begin() |
| 303 | b = bytearray(2) |
| 304 | n = resp.readinto(b) |
| 305 | self.assertEqual(n, 2) |
| 306 | self.assertEqual(bytes(b), b'Te') |
| 307 | self.assertFalse(resp.isclosed()) |
| 308 | n = resp.readinto(b) |
| 309 | self.assertEqual(n, 2) |
| 310 | self.assertEqual(bytes(b), b'xt') |
| 311 | n = resp.readinto(b) |
| 312 | self.assertEqual(n, 0) |
| 313 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 314 | self.assertFalse(resp.closed) |
| 315 | resp.close() |
| 316 | self.assertTrue(resp.closed) |
Antoine Pitrou | 6a35e18 | 2013-02-02 23:04:56 +0100 | [diff] [blame] | 317 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 318 | def test_host_port(self): |
| 319 | # Check invalid host_port |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 320 | |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 321 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 322 | self.assertRaises(client.InvalidURL, client.HTTPConnection, hp) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 323 | |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 324 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
| 325 | "fe80::207:e9ff:fe9b", 8000), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 326 | ("www.python.org:80", "www.python.org", 80), |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 327 | ("www.python.org:", "www.python.org", 80), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 328 | ("www.python.org", "www.python.org", 80), |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 329 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80), |
| 330 | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 331 | c = client.HTTPConnection(hp) |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 332 | self.assertEqual(h, c.host) |
| 333 | self.assertEqual(p, c.port) |
Skip Montanaro | 10e6e0e | 2004-09-14 16:32:02 +0000 | [diff] [blame] | 334 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 335 | def test_response_headers(self): |
| 336 | # test response with multiple message headers with the same field name. |
| 337 | text = ('HTTP/1.1 200 OK\r\n' |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 338 | 'Set-Cookie: Customer="WILE_E_COYOTE"; ' |
| 339 | 'Version="1"; Path="/acme"\r\n' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 340 | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
| 341 | ' Path="/acme"\r\n' |
| 342 | '\r\n' |
| 343 | 'No body\r\n') |
| 344 | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
| 345 | ', ' |
| 346 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
| 347 | s = FakeSocket(text) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 348 | r = client.HTTPResponse(s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 349 | r.begin() |
| 350 | cookies = r.getheader("Set-Cookie") |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 351 | self.assertEqual(cookies, hdr) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 352 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 353 | def test_read_head(self): |
| 354 | # Test that the library doesn't attempt to read any data |
| 355 | # from a HEAD request. (Tickles SF bug #622042.) |
| 356 | sock = FakeSocket( |
| 357 | 'HTTP/1.1 200 OK\r\n' |
| 358 | 'Content-Length: 14432\r\n' |
| 359 | '\r\n', |
Serhiy Storchaka | 50254c5 | 2013-08-29 11:35:43 +0300 | [diff] [blame] | 360 | NoEOFBytesIO) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 361 | resp = client.HTTPResponse(sock, method="HEAD") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 362 | resp.begin() |
Guido van Rossum | a00f123 | 2007-09-12 19:43:09 +0000 | [diff] [blame] | 363 | if resp.read(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 364 | self.fail("Did not expect response from HEAD request") |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 366 | def test_readinto_head(self): |
| 367 | # Test that the library doesn't attempt to read any data |
| 368 | # from a HEAD request. (Tickles SF bug #622042.) |
| 369 | sock = FakeSocket( |
| 370 | 'HTTP/1.1 200 OK\r\n' |
| 371 | 'Content-Length: 14432\r\n' |
| 372 | '\r\n', |
Serhiy Storchaka | 50254c5 | 2013-08-29 11:35:43 +0300 | [diff] [blame] | 373 | NoEOFBytesIO) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 374 | resp = client.HTTPResponse(sock, method="HEAD") |
| 375 | resp.begin() |
| 376 | b = bytearray(5) |
| 377 | if resp.readinto(b) != 0: |
| 378 | self.fail("Did not expect response from HEAD request") |
| 379 | self.assertEqual(bytes(b), b'\x00'*5) |
| 380 | |
Georg Brandl | bf3f8eb | 2013-10-27 07:34:48 +0100 | [diff] [blame] | 381 | def test_too_many_headers(self): |
| 382 | headers = '\r\n'.join('Header%d: foo' % i |
| 383 | for i in range(client._MAXHEADERS + 1)) + '\r\n' |
| 384 | text = ('HTTP/1.1 200 OK\r\n' + headers) |
| 385 | s = FakeSocket(text) |
| 386 | r = client.HTTPResponse(s) |
| 387 | self.assertRaisesRegex(client.HTTPException, |
| 388 | r"got more than \d+ headers", r.begin) |
| 389 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 390 | def test_send_file(self): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 391 | expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' |
| 392 | b'Accept-Encoding: identity\r\nContent-Length:') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 393 | |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 394 | with open(__file__, 'rb') as body: |
| 395 | conn = client.HTTPConnection('example.com') |
| 396 | sock = FakeSocket(body) |
| 397 | conn.sock = sock |
| 398 | conn.request('GET', '/foo', body) |
| 399 | self.assertTrue(sock.data.startswith(expected), '%r != %r' % |
| 400 | (sock.data[:len(expected)], expected)) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 402 | def test_send(self): |
| 403 | expected = b'this is a test this is only a test' |
| 404 | conn = client.HTTPConnection('example.com') |
| 405 | sock = FakeSocket(None) |
| 406 | conn.sock = sock |
| 407 | conn.send(expected) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 408 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 409 | sock.data = b'' |
| 410 | conn.send(array.array('b', expected)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 411 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 412 | sock.data = b'' |
| 413 | conn.send(io.BytesIO(expected)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 414 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | ead1d62 | 2009-09-29 18:44:53 +0000 | [diff] [blame] | 415 | |
Andrew Svetlov | 7b2c8bb | 2013-04-12 22:49:19 +0300 | [diff] [blame] | 416 | def test_send_updating_file(self): |
| 417 | def data(): |
| 418 | yield 'data' |
| 419 | yield None |
| 420 | yield 'data_two' |
| 421 | |
| 422 | class UpdatingFile(): |
| 423 | mode = 'r' |
| 424 | d = data() |
| 425 | def read(self, blocksize=-1): |
| 426 | return self.d.__next__() |
| 427 | |
| 428 | expected = b'data' |
| 429 | |
| 430 | conn = client.HTTPConnection('example.com') |
| 431 | sock = FakeSocket("") |
| 432 | conn.sock = sock |
| 433 | conn.send(UpdatingFile()) |
| 434 | self.assertEqual(sock.data, expected) |
| 435 | |
| 436 | |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 437 | def test_send_iter(self): |
| 438 | expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
| 439 | b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \ |
| 440 | b'\r\nonetwothree' |
| 441 | |
| 442 | def body(): |
| 443 | yield b"one" |
| 444 | yield b"two" |
| 445 | yield b"three" |
| 446 | |
| 447 | conn = client.HTTPConnection('example.com') |
| 448 | sock = FakeSocket("") |
| 449 | conn.sock = sock |
| 450 | conn.request('GET', '/foo', body(), {'Content-Length': '11'}) |
Victor Stinner | 04ba966 | 2011-01-04 00:04:46 +0000 | [diff] [blame] | 451 | self.assertEqual(sock.data, expected) |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 452 | |
Senthil Kumaran | eb71ad4 | 2011-08-02 18:33:41 +0800 | [diff] [blame] | 453 | def test_send_type_error(self): |
| 454 | # See: Issue #12676 |
| 455 | conn = client.HTTPConnection('example.com') |
| 456 | conn.sock = FakeSocket('') |
| 457 | with self.assertRaises(TypeError): |
| 458 | conn.request('POST', 'test', conn) |
| 459 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 460 | def test_chunked(self): |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 461 | expected = chunked_expected |
| 462 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 463 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 464 | resp.begin() |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 465 | self.assertEqual(resp.read(), expected) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 466 | resp.close() |
| 467 | |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 468 | # Various read sizes |
| 469 | for n in range(1, 12): |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 470 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 471 | resp = client.HTTPResponse(sock, method="GET") |
| 472 | resp.begin() |
| 473 | self.assertEqual(resp.read(n) + resp.read(n) + resp.read(), expected) |
| 474 | resp.close() |
| 475 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 476 | for x in ('', 'foo\r\n'): |
| 477 | sock = FakeSocket(chunked_start + x) |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 478 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 479 | resp.begin() |
| 480 | try: |
| 481 | resp.read() |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 482 | except client.IncompleteRead as i: |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 483 | self.assertEqual(i.partial, expected) |
| 484 | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
| 485 | self.assertEqual(repr(i), expected_message) |
| 486 | self.assertEqual(str(i), expected_message) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 487 | else: |
| 488 | self.fail('IncompleteRead expected') |
| 489 | finally: |
| 490 | resp.close() |
| 491 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 492 | def test_readinto_chunked(self): |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 493 | |
| 494 | expected = chunked_expected |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 495 | nexpected = len(expected) |
| 496 | b = bytearray(128) |
| 497 | |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 498 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 499 | resp = client.HTTPResponse(sock, method="GET") |
| 500 | resp.begin() |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 501 | n = resp.readinto(b) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 502 | self.assertEqual(b[:nexpected], expected) |
| 503 | self.assertEqual(n, nexpected) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 504 | resp.close() |
| 505 | |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 506 | # Various read sizes |
| 507 | for n in range(1, 12): |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 508 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 509 | resp = client.HTTPResponse(sock, method="GET") |
| 510 | resp.begin() |
| 511 | m = memoryview(b) |
| 512 | i = resp.readinto(m[0:n]) |
| 513 | i += resp.readinto(m[i:n + i]) |
| 514 | i += resp.readinto(m[i:]) |
| 515 | self.assertEqual(b[:nexpected], expected) |
| 516 | self.assertEqual(i, nexpected) |
| 517 | resp.close() |
| 518 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 519 | for x in ('', 'foo\r\n'): |
| 520 | sock = FakeSocket(chunked_start + x) |
| 521 | resp = client.HTTPResponse(sock, method="GET") |
| 522 | resp.begin() |
| 523 | try: |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 524 | n = resp.readinto(b) |
| 525 | except client.IncompleteRead as i: |
Antoine Pitrou | f7e7818 | 2012-01-04 18:57:22 +0100 | [diff] [blame] | 526 | self.assertEqual(i.partial, expected) |
| 527 | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
| 528 | self.assertEqual(repr(i), expected_message) |
| 529 | self.assertEqual(str(i), expected_message) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 530 | else: |
| 531 | self.fail('IncompleteRead expected') |
| 532 | finally: |
| 533 | resp.close() |
| 534 | |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 535 | def test_chunked_head(self): |
| 536 | chunked_start = ( |
| 537 | 'HTTP/1.1 200 OK\r\n' |
| 538 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 539 | 'a\r\n' |
| 540 | 'hello world\r\n' |
| 541 | '1\r\n' |
| 542 | 'd\r\n' |
| 543 | ) |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 544 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 545 | resp = client.HTTPResponse(sock, method="HEAD") |
| 546 | resp.begin() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 547 | self.assertEqual(resp.read(), b'') |
| 548 | self.assertEqual(resp.status, 200) |
| 549 | self.assertEqual(resp.reason, 'OK') |
Senthil Kumaran | 0b99883 | 2010-06-04 17:27:11 +0000 | [diff] [blame] | 550 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 551 | self.assertFalse(resp.closed) |
| 552 | resp.close() |
| 553 | self.assertTrue(resp.closed) |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 555 | def test_readinto_chunked_head(self): |
| 556 | chunked_start = ( |
| 557 | 'HTTP/1.1 200 OK\r\n' |
| 558 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 559 | 'a\r\n' |
| 560 | 'hello world\r\n' |
| 561 | '1\r\n' |
| 562 | 'd\r\n' |
| 563 | ) |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 564 | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 565 | resp = client.HTTPResponse(sock, method="HEAD") |
| 566 | resp.begin() |
| 567 | b = bytearray(5) |
| 568 | n = resp.readinto(b) |
| 569 | self.assertEqual(n, 0) |
| 570 | self.assertEqual(bytes(b), b'\x00'*5) |
| 571 | self.assertEqual(resp.status, 200) |
| 572 | self.assertEqual(resp.reason, 'OK') |
| 573 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b6c86fd | 2013-02-06 10:35:40 +0200 | [diff] [blame] | 574 | self.assertFalse(resp.closed) |
| 575 | resp.close() |
| 576 | self.assertTrue(resp.closed) |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 577 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 578 | def test_negative_content_length(self): |
Jeremy Hylton | 8206695 | 2008-12-15 03:08:30 +0000 | [diff] [blame] | 579 | sock = FakeSocket( |
| 580 | '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] | 581 | resp = client.HTTPResponse(sock, method="GET") |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 582 | resp.begin() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 583 | self.assertEqual(resp.read(), b'Hello\r\n') |
Antoine Pitrou | beec61a | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 584 | self.assertTrue(resp.isclosed()) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 585 | |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 586 | def test_incomplete_read(self): |
| 587 | 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] | 588 | resp = client.HTTPResponse(sock, method="GET") |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 589 | resp.begin() |
| 590 | try: |
| 591 | resp.read() |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 592 | except client.IncompleteRead as i: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 593 | self.assertEqual(i.partial, b'Hello\r\n') |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 594 | self.assertEqual(repr(i), |
| 595 | "IncompleteRead(7 bytes read, 3 more expected)") |
| 596 | self.assertEqual(str(i), |
| 597 | "IncompleteRead(7 bytes read, 3 more expected)") |
Antoine Pitrou | beec61a | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 598 | self.assertTrue(resp.isclosed()) |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 599 | else: |
| 600 | self.fail('IncompleteRead expected') |
Benjamin Peterson | 6accb98 | 2009-03-02 22:50:25 +0000 | [diff] [blame] | 601 | |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 602 | def test_epipe(self): |
| 603 | sock = EPipeSocket( |
| 604 | "HTTP/1.0 401 Authorization Required\r\n" |
| 605 | "Content-type: text/html\r\n" |
| 606 | "WWW-Authenticate: Basic realm=\"example\"\r\n", |
| 607 | b"Content-Length") |
| 608 | conn = client.HTTPConnection("example.com") |
| 609 | conn.sock = sock |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 610 | self.assertRaises(OSError, |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 611 | lambda: conn.request("PUT", "/url", "body")) |
| 612 | resp = conn.getresponse() |
| 613 | self.assertEqual(401, resp.status) |
| 614 | self.assertEqual("Basic realm=\"example\"", |
| 615 | resp.getheader("www-authenticate")) |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 616 | |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 617 | # Test lines overflowing the max line size (_MAXLINE in http.client) |
| 618 | |
| 619 | def test_overflowing_status_line(self): |
| 620 | body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n" |
| 621 | resp = client.HTTPResponse(FakeSocket(body)) |
| 622 | self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin) |
| 623 | |
| 624 | def test_overflowing_header_line(self): |
| 625 | body = ( |
| 626 | 'HTTP/1.1 200 OK\r\n' |
| 627 | 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n' |
| 628 | ) |
| 629 | resp = client.HTTPResponse(FakeSocket(body)) |
| 630 | self.assertRaises(client.LineTooLong, resp.begin) |
| 631 | |
| 632 | def test_overflowing_chunked_line(self): |
| 633 | body = ( |
| 634 | 'HTTP/1.1 200 OK\r\n' |
| 635 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 636 | + '0' * 65536 + 'a\r\n' |
| 637 | 'hello world\r\n' |
| 638 | '0\r\n' |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 639 | '\r\n' |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 640 | ) |
| 641 | resp = client.HTTPResponse(FakeSocket(body)) |
| 642 | resp.begin() |
| 643 | self.assertRaises(client.LineTooLong, resp.read) |
| 644 | |
Senthil Kumaran | 9c29f86 | 2012-04-29 10:20:46 +0800 | [diff] [blame] | 645 | def test_early_eof(self): |
| 646 | # Test httpresponse with no \r\n termination, |
| 647 | body = "HTTP/1.1 200 Ok" |
| 648 | sock = FakeSocket(body) |
| 649 | resp = client.HTTPResponse(sock) |
| 650 | resp.begin() |
| 651 | self.assertEqual(resp.read(), b'') |
| 652 | self.assertTrue(resp.isclosed()) |
Serhiy Storchaka | b5b9c8c | 2013-02-06 10:31:57 +0200 | [diff] [blame] | 653 | self.assertFalse(resp.closed) |
| 654 | resp.close() |
| 655 | self.assertTrue(resp.closed) |
Senthil Kumaran | 9c29f86 | 2012-04-29 10:20:46 +0800 | [diff] [blame] | 656 | |
Antoine Pitrou | 90e4774 | 2013-01-02 22:10:47 +0100 | [diff] [blame] | 657 | def test_delayed_ack_opt(self): |
| 658 | # Test that Nagle/delayed_ack optimistaion works correctly. |
| 659 | |
| 660 | # For small payloads, it should coalesce the body with |
| 661 | # headers, resulting in a single sendall() call |
| 662 | conn = client.HTTPConnection('example.com') |
| 663 | sock = FakeSocket(None) |
| 664 | conn.sock = sock |
| 665 | body = b'x' * (conn.mss - 1) |
| 666 | conn.request('POST', '/', body) |
| 667 | self.assertEqual(sock.sendall_calls, 1) |
| 668 | |
| 669 | # For large payloads, it should send the headers and |
| 670 | # then the body, resulting in more than one sendall() |
| 671 | # call |
| 672 | conn = client.HTTPConnection('example.com') |
| 673 | sock = FakeSocket(None) |
| 674 | conn.sock = sock |
| 675 | body = b'x' * conn.mss |
| 676 | conn.request('POST', '/', body) |
| 677 | self.assertGreater(sock.sendall_calls, 1) |
| 678 | |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 679 | def test_chunked_extension(self): |
| 680 | extra = '3;foo=bar\r\n' + 'abc\r\n' |
| 681 | expected = chunked_expected + b'abc' |
| 682 | |
| 683 | sock = FakeSocket(chunked_start + extra + last_chunk_extended + chunked_end) |
| 684 | resp = client.HTTPResponse(sock, method="GET") |
| 685 | resp.begin() |
| 686 | self.assertEqual(resp.read(), expected) |
| 687 | resp.close() |
| 688 | |
| 689 | def test_chunked_missing_end(self): |
| 690 | """some servers may serve up a short chunked encoding stream""" |
| 691 | expected = chunked_expected |
| 692 | sock = FakeSocket(chunked_start + last_chunk) #no terminating crlf |
| 693 | resp = client.HTTPResponse(sock, method="GET") |
| 694 | resp.begin() |
| 695 | self.assertEqual(resp.read(), expected) |
| 696 | resp.close() |
| 697 | |
| 698 | def test_chunked_trailers(self): |
| 699 | """See that trailers are read and ignored""" |
| 700 | expected = chunked_expected |
| 701 | sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end) |
| 702 | resp = client.HTTPResponse(sock, method="GET") |
| 703 | resp.begin() |
| 704 | self.assertEqual(resp.read(), expected) |
| 705 | # we should have reached the end of the file |
| 706 | self.assertEqual(sock.file.read(100), b"") #we read to the end |
| 707 | resp.close() |
| 708 | |
| 709 | def test_chunked_sync(self): |
| 710 | """Check that we don't read past the end of the chunked-encoding stream""" |
| 711 | expected = chunked_expected |
| 712 | extradata = "extradata" |
| 713 | sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end + extradata) |
| 714 | resp = client.HTTPResponse(sock, method="GET") |
| 715 | resp.begin() |
| 716 | self.assertEqual(resp.read(), expected) |
| 717 | # the file should now have our extradata ready to be read |
| 718 | self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end |
| 719 | resp.close() |
| 720 | |
| 721 | def test_content_length_sync(self): |
| 722 | """Check that we don't read past the end of the Content-Length stream""" |
| 723 | extradata = "extradata" |
| 724 | expected = b"Hello123\r\n" |
| 725 | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello123\r\n' + extradata) |
| 726 | resp = client.HTTPResponse(sock, method="GET") |
| 727 | resp.begin() |
| 728 | self.assertEqual(resp.read(), expected) |
| 729 | # the file should now have our extradata ready to be read |
| 730 | self.assertEqual(sock.file.read(100), extradata.encode("ascii")) #we read to the end |
| 731 | resp.close() |
| 732 | |
| 733 | class ExtendedReadTest(TestCase): |
| 734 | """ |
| 735 | Test peek(), read1(), readline() |
| 736 | """ |
| 737 | lines = ( |
| 738 | 'HTTP/1.1 200 OK\r\n' |
| 739 | '\r\n' |
| 740 | 'hello world!\n' |
| 741 | 'and now \n' |
| 742 | 'for something completely different\n' |
| 743 | 'foo' |
| 744 | ) |
| 745 | lines_expected = lines[lines.find('hello'):].encode("ascii") |
| 746 | lines_chunked = ( |
| 747 | 'HTTP/1.1 200 OK\r\n' |
| 748 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 749 | 'a\r\n' |
| 750 | 'hello worl\r\n' |
| 751 | '3\r\n' |
| 752 | 'd!\n\r\n' |
| 753 | '9\r\n' |
| 754 | 'and now \n\r\n' |
| 755 | '23\r\n' |
| 756 | 'for something completely different\n\r\n' |
| 757 | '3\r\n' |
| 758 | 'foo\r\n' |
| 759 | '0\r\n' # terminating chunk |
| 760 | '\r\n' # end of trailers |
| 761 | ) |
| 762 | |
| 763 | def setUp(self): |
| 764 | sock = FakeSocket(self.lines) |
| 765 | resp = client.HTTPResponse(sock, method="GET") |
| 766 | resp.begin() |
| 767 | resp.fp = io.BufferedReader(resp.fp) |
| 768 | self.resp = resp |
| 769 | |
| 770 | |
| 771 | |
| 772 | def test_peek(self): |
| 773 | resp = self.resp |
| 774 | # patch up the buffered peek so that it returns not too much stuff |
| 775 | oldpeek = resp.fp.peek |
| 776 | def mypeek(n=-1): |
| 777 | p = oldpeek(n) |
| 778 | if n >= 0: |
| 779 | return p[:n] |
| 780 | return p[:10] |
| 781 | resp.fp.peek = mypeek |
| 782 | |
| 783 | all = [] |
| 784 | while True: |
| 785 | # try a short peek |
| 786 | p = resp.peek(3) |
| 787 | if p: |
| 788 | self.assertGreater(len(p), 0) |
| 789 | # then unbounded peek |
| 790 | p2 = resp.peek() |
| 791 | self.assertGreaterEqual(len(p2), len(p)) |
| 792 | self.assertTrue(p2.startswith(p)) |
| 793 | next = resp.read(len(p2)) |
| 794 | self.assertEqual(next, p2) |
| 795 | else: |
| 796 | next = resp.read() |
| 797 | self.assertFalse(next) |
| 798 | all.append(next) |
| 799 | if not next: |
| 800 | break |
| 801 | self.assertEqual(b"".join(all), self.lines_expected) |
| 802 | |
| 803 | def test_readline(self): |
| 804 | resp = self.resp |
| 805 | self._verify_readline(self.resp.readline, self.lines_expected) |
| 806 | |
| 807 | def _verify_readline(self, readline, expected): |
| 808 | all = [] |
| 809 | while True: |
| 810 | # short readlines |
| 811 | line = readline(5) |
| 812 | if line and line != b"foo": |
| 813 | if len(line) < 5: |
| 814 | self.assertTrue(line.endswith(b"\n")) |
| 815 | all.append(line) |
| 816 | if not line: |
| 817 | break |
| 818 | self.assertEqual(b"".join(all), expected) |
| 819 | |
| 820 | def test_read1(self): |
| 821 | resp = self.resp |
| 822 | def r(): |
| 823 | res = resp.read1(4) |
| 824 | self.assertLessEqual(len(res), 4) |
| 825 | return res |
| 826 | readliner = Readliner(r) |
| 827 | self._verify_readline(readliner.readline, self.lines_expected) |
| 828 | |
| 829 | def test_read1_unbounded(self): |
| 830 | resp = self.resp |
| 831 | all = [] |
| 832 | while True: |
| 833 | data = resp.read1() |
| 834 | if not data: |
| 835 | break |
| 836 | all.append(data) |
| 837 | self.assertEqual(b"".join(all), self.lines_expected) |
| 838 | |
| 839 | def test_read1_bounded(self): |
| 840 | resp = self.resp |
| 841 | all = [] |
| 842 | while True: |
| 843 | data = resp.read1(10) |
| 844 | if not data: |
| 845 | break |
| 846 | self.assertLessEqual(len(data), 10) |
| 847 | all.append(data) |
| 848 | self.assertEqual(b"".join(all), self.lines_expected) |
| 849 | |
| 850 | def test_read1_0(self): |
| 851 | self.assertEqual(self.resp.read1(0), b"") |
| 852 | |
| 853 | def test_peek_0(self): |
| 854 | p = self.resp.peek(0) |
| 855 | self.assertLessEqual(0, len(p)) |
| 856 | |
| 857 | class ExtendedReadTestChunked(ExtendedReadTest): |
| 858 | """ |
| 859 | Test peek(), read1(), readline() in chunked mode |
| 860 | """ |
| 861 | lines = ( |
| 862 | 'HTTP/1.1 200 OK\r\n' |
| 863 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 864 | 'a\r\n' |
| 865 | 'hello worl\r\n' |
| 866 | '3\r\n' |
| 867 | 'd!\n\r\n' |
| 868 | '9\r\n' |
| 869 | 'and now \n\r\n' |
| 870 | '23\r\n' |
| 871 | 'for something completely different\n\r\n' |
| 872 | '3\r\n' |
| 873 | 'foo\r\n' |
| 874 | '0\r\n' # terminating chunk |
| 875 | '\r\n' # end of trailers |
| 876 | ) |
| 877 | |
| 878 | |
| 879 | class Readliner: |
| 880 | """ |
| 881 | a simple readline class that uses an arbitrary read function and buffering |
| 882 | """ |
| 883 | def __init__(self, readfunc): |
| 884 | self.readfunc = readfunc |
| 885 | self.remainder = b"" |
| 886 | |
| 887 | def readline(self, limit): |
| 888 | data = [] |
| 889 | datalen = 0 |
| 890 | read = self.remainder |
| 891 | try: |
| 892 | while True: |
| 893 | idx = read.find(b'\n') |
| 894 | if idx != -1: |
| 895 | break |
| 896 | if datalen + len(read) >= limit: |
| 897 | idx = limit - datalen - 1 |
| 898 | # read more data |
| 899 | data.append(read) |
| 900 | read = self.readfunc() |
| 901 | if not read: |
| 902 | idx = 0 #eof condition |
| 903 | break |
| 904 | idx += 1 |
| 905 | data.append(read[:idx]) |
| 906 | self.remainder = read[idx:] |
| 907 | return b"".join(data) |
| 908 | except: |
| 909 | self.remainder = b"".join(data) |
| 910 | raise |
| 911 | |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 912 | class OfflineTest(TestCase): |
| 913 | def test_responses(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 914 | self.assertEqual(client.responses[client.NOT_FOUND], "Not Found") |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 915 | |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 916 | |
| 917 | class SourceAddressTest(TestCase): |
| 918 | def setUp(self): |
| 919 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 920 | self.port = support.bind_port(self.serv) |
| 921 | self.source_port = support.find_unused_port() |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame^] | 922 | self.serv.listen() |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 923 | self.conn = None |
| 924 | |
| 925 | def tearDown(self): |
| 926 | if self.conn: |
| 927 | self.conn.close() |
| 928 | self.conn = None |
| 929 | self.serv.close() |
| 930 | self.serv = None |
| 931 | |
| 932 | def testHTTPConnectionSourceAddress(self): |
| 933 | self.conn = client.HTTPConnection(HOST, self.port, |
| 934 | source_address=('', self.source_port)) |
| 935 | self.conn.connect() |
| 936 | self.assertEqual(self.conn.sock.getsockname()[1], self.source_port) |
| 937 | |
| 938 | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
| 939 | 'http.client.HTTPSConnection not defined') |
| 940 | def testHTTPSConnectionSourceAddress(self): |
| 941 | self.conn = client.HTTPSConnection(HOST, self.port, |
| 942 | source_address=('', self.source_port)) |
| 943 | # We don't test anything here other the constructor not barfing as |
| 944 | # this code doesn't deal with setting up an active running SSL server |
| 945 | # for an ssl_wrapped connect() to actually return from. |
| 946 | |
| 947 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 948 | class TimeoutTest(TestCase): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 949 | PORT = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 950 | |
| 951 | def setUp(self): |
| 952 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 953 | TimeoutTest.PORT = support.bind_port(self.serv) |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame^] | 954 | self.serv.listen() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 955 | |
| 956 | def tearDown(self): |
| 957 | self.serv.close() |
| 958 | self.serv = None |
| 959 | |
| 960 | def testTimeoutAttribute(self): |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 961 | # This will prove that the timeout gets through HTTPConnection |
| 962 | # and into the socket. |
| 963 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 964 | # default -- use global socket timeout |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 965 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 966 | socket.setdefaulttimeout(30) |
| 967 | try: |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 968 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 969 | httpConn.connect() |
| 970 | finally: |
| 971 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 972 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 973 | httpConn.close() |
| 974 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 975 | # no timeout -- do not use global socket default |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 976 | self.assertIsNone(socket.getdefaulttimeout()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 977 | socket.setdefaulttimeout(30) |
| 978 | try: |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 979 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 980 | timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 981 | httpConn.connect() |
| 982 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 983 | socket.setdefaulttimeout(None) |
| 984 | self.assertEqual(httpConn.sock.gettimeout(), None) |
| 985 | httpConn.close() |
| 986 | |
| 987 | # a value |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 988 | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 989 | httpConn.connect() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 990 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 991 | httpConn.close() |
| 992 | |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 993 | |
| 994 | class HTTPSTest(TestCase): |
| 995 | |
| 996 | def setUp(self): |
| 997 | if not hasattr(client, 'HTTPSConnection'): |
| 998 | self.skipTest('ssl support required') |
| 999 | |
| 1000 | def make_server(self, certfile): |
| 1001 | from test.ssl_servers import make_https_server |
Antoine Pitrou | da23259 | 2013-02-05 21:20:51 +0100 | [diff] [blame] | 1002 | return make_https_server(self, certfile=certfile) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1003 | |
| 1004 | def test_attributes(self): |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1005 | # simple test to check it's storing the timeout |
| 1006 | h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
| 1007 | self.assertEqual(h.timeout, 30) |
| 1008 | |
| 1009 | def _check_svn_python_org(self, resp): |
| 1010 | # Just a simple check that everything went fine |
| 1011 | server_string = resp.getheader('server') |
| 1012 | self.assertIn('Apache', server_string) |
| 1013 | |
| 1014 | def test_networked(self): |
| 1015 | # Default settings: no cert verification is done |
| 1016 | support.requires('network') |
| 1017 | with support.transient_internet('svn.python.org'): |
| 1018 | h = client.HTTPSConnection('svn.python.org', 443) |
| 1019 | h.request('GET', '/') |
| 1020 | resp = h.getresponse() |
| 1021 | self._check_svn_python_org(resp) |
| 1022 | |
| 1023 | def test_networked_good_cert(self): |
| 1024 | # We feed a CA cert that validates the server's cert |
| 1025 | import ssl |
| 1026 | support.requires('network') |
| 1027 | with support.transient_internet('svn.python.org'): |
| 1028 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1029 | context.verify_mode = ssl.CERT_REQUIRED |
| 1030 | context.load_verify_locations(CACERT_svn_python_org) |
| 1031 | h = client.HTTPSConnection('svn.python.org', 443, context=context) |
| 1032 | h.request('GET', '/') |
| 1033 | resp = h.getresponse() |
| 1034 | self._check_svn_python_org(resp) |
| 1035 | |
| 1036 | def test_networked_bad_cert(self): |
| 1037 | # We feed a "CA" cert that is unrelated to the server's cert |
| 1038 | import ssl |
| 1039 | support.requires('network') |
| 1040 | with support.transient_internet('svn.python.org'): |
| 1041 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1042 | context.verify_mode = ssl.CERT_REQUIRED |
| 1043 | context.load_verify_locations(CERT_localhost) |
| 1044 | h = client.HTTPSConnection('svn.python.org', 443, context=context) |
| 1045 | with self.assertRaises(ssl.SSLError): |
| 1046 | h.request('GET', '/') |
| 1047 | |
| 1048 | def test_local_good_hostname(self): |
| 1049 | # The (valid) cert validates the HTTP hostname |
| 1050 | import ssl |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 1051 | server = self.make_server(CERT_localhost) |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1052 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1053 | context.verify_mode = ssl.CERT_REQUIRED |
| 1054 | context.load_verify_locations(CERT_localhost) |
| 1055 | h = client.HTTPSConnection('localhost', server.port, context=context) |
| 1056 | h.request('GET', '/nonexistent') |
| 1057 | resp = h.getresponse() |
| 1058 | self.assertEqual(resp.status, 404) |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 1059 | del server |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1060 | |
| 1061 | def test_local_bad_hostname(self): |
| 1062 | # The (valid) cert doesn't validate the HTTP hostname |
| 1063 | import ssl |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 1064 | server = self.make_server(CERT_fakehostname) |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1065 | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 1066 | context.verify_mode = ssl.CERT_REQUIRED |
| 1067 | context.load_verify_locations(CERT_fakehostname) |
| 1068 | h = client.HTTPSConnection('localhost', server.port, context=context) |
| 1069 | with self.assertRaises(ssl.CertificateError): |
| 1070 | h.request('GET', '/') |
| 1071 | # Same with explicit check_hostname=True |
| 1072 | h = client.HTTPSConnection('localhost', server.port, context=context, |
| 1073 | check_hostname=True) |
| 1074 | with self.assertRaises(ssl.CertificateError): |
| 1075 | h.request('GET', '/') |
| 1076 | # With check_hostname=False, the mismatching is ignored |
| 1077 | h = client.HTTPSConnection('localhost', server.port, context=context, |
| 1078 | check_hostname=False) |
| 1079 | h.request('GET', '/nonexistent') |
| 1080 | resp = h.getresponse() |
| 1081 | self.assertEqual(resp.status, 404) |
Brett Cannon | 252365b | 2011-08-04 22:43:11 -0700 | [diff] [blame] | 1082 | del server |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1083 | |
Petri Lehtinen | e119c40 | 2011-10-26 21:29:15 +0300 | [diff] [blame] | 1084 | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
| 1085 | 'http.client.HTTPSConnection not available') |
Łukasz Langa | a5a9a9c | 2011-10-18 21:17:39 +0200 | [diff] [blame] | 1086 | def test_host_port(self): |
| 1087 | # Check invalid host_port |
| 1088 | |
| 1089 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
| 1090 | self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp) |
| 1091 | |
| 1092 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
| 1093 | "fe80::207:e9ff:fe9b", 8000), |
| 1094 | ("www.python.org:443", "www.python.org", 443), |
| 1095 | ("www.python.org:", "www.python.org", 443), |
| 1096 | ("www.python.org", "www.python.org", 443), |
| 1097 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443), |
| 1098 | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", |
| 1099 | 443)): |
| 1100 | c = client.HTTPSConnection(hp) |
| 1101 | self.assertEqual(h, c.host) |
| 1102 | self.assertEqual(p, c.port) |
| 1103 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1104 | |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1105 | class RequestBodyTest(TestCase): |
| 1106 | """Test cases where a request includes a message body.""" |
| 1107 | |
| 1108 | def setUp(self): |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 1109 | self.conn = client.HTTPConnection('example.com') |
Jeremy Hylton | 636950f | 2009-03-28 04:34:21 +0000 | [diff] [blame] | 1110 | self.conn.sock = self.sock = FakeSocket("") |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1111 | self.conn.sock = self.sock |
| 1112 | |
| 1113 | def get_headers_and_fp(self): |
| 1114 | f = io.BytesIO(self.sock.data) |
| 1115 | f.readline() # read the request line |
Jeremy Hylton | 7c1692d | 2009-03-27 21:31:03 +0000 | [diff] [blame] | 1116 | message = client.parse_headers(f) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1117 | return message, f |
| 1118 | |
| 1119 | def test_manual_content_length(self): |
| 1120 | # Set an incorrect content-length so that we can verify that |
| 1121 | # it will not be over-ridden by the library. |
| 1122 | self.conn.request("PUT", "/url", "body", |
| 1123 | {"Content-Length": "42"}) |
| 1124 | message, f = self.get_headers_and_fp() |
| 1125 | self.assertEqual("42", message.get("content-length")) |
| 1126 | self.assertEqual(4, len(f.read())) |
| 1127 | |
| 1128 | def test_ascii_body(self): |
| 1129 | self.conn.request("PUT", "/url", "body") |
| 1130 | message, f = self.get_headers_and_fp() |
| 1131 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 1132 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1133 | self.assertEqual("4", message.get("content-length")) |
| 1134 | self.assertEqual(b'body', f.read()) |
| 1135 | |
| 1136 | def test_latin1_body(self): |
| 1137 | self.conn.request("PUT", "/url", "body\xc1") |
| 1138 | message, f = self.get_headers_and_fp() |
| 1139 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 1140 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1141 | self.assertEqual("5", message.get("content-length")) |
| 1142 | self.assertEqual(b'body\xc1', f.read()) |
| 1143 | |
| 1144 | def test_bytes_body(self): |
| 1145 | self.conn.request("PUT", "/url", b"body\xc1") |
| 1146 | message, f = self.get_headers_and_fp() |
| 1147 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 1148 | self.assertIsNone(message.get_charset()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1149 | self.assertEqual("5", message.get("content-length")) |
| 1150 | self.assertEqual(b'body\xc1', f.read()) |
| 1151 | |
| 1152 | def test_file_body(self): |
Victor Stinner | 18d15cb | 2011-09-21 01:09:04 +0200 | [diff] [blame] | 1153 | self.addCleanup(support.unlink, support.TESTFN) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 1154 | with open(support.TESTFN, "w") as f: |
| 1155 | f.write("body") |
| 1156 | with open(support.TESTFN) as f: |
| 1157 | self.conn.request("PUT", "/url", f) |
| 1158 | message, f = self.get_headers_and_fp() |
| 1159 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 1160 | self.assertIsNone(message.get_charset()) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 1161 | self.assertEqual("4", message.get("content-length")) |
| 1162 | self.assertEqual(b'body', f.read()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1163 | |
| 1164 | def test_binary_file_body(self): |
Victor Stinner | 18d15cb | 2011-09-21 01:09:04 +0200 | [diff] [blame] | 1165 | self.addCleanup(support.unlink, support.TESTFN) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 1166 | with open(support.TESTFN, "wb") as f: |
| 1167 | f.write(b"body\xc1") |
| 1168 | with open(support.TESTFN, "rb") as f: |
| 1169 | self.conn.request("PUT", "/url", f) |
| 1170 | message, f = self.get_headers_and_fp() |
| 1171 | self.assertEqual("text/plain", message.get_content_type()) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 1172 | self.assertIsNone(message.get_charset()) |
Brett Cannon | 77b7de6 | 2010-10-29 23:31:11 +0000 | [diff] [blame] | 1173 | self.assertEqual("5", message.get("content-length")) |
| 1174 | self.assertEqual(b'body\xc1', f.read()) |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 1175 | |
Senthil Kumaran | 9f8dc44 | 2010-08-02 11:04:58 +0000 | [diff] [blame] | 1176 | |
| 1177 | class HTTPResponseTest(TestCase): |
| 1178 | |
| 1179 | def setUp(self): |
| 1180 | body = "HTTP/1.1 200 Ok\r\nMy-Header: first-value\r\nMy-Header: \ |
| 1181 | second-value\r\n\r\nText" |
| 1182 | sock = FakeSocket(body) |
| 1183 | self.resp = client.HTTPResponse(sock) |
| 1184 | self.resp.begin() |
| 1185 | |
| 1186 | def test_getting_header(self): |
| 1187 | header = self.resp.getheader('My-Header') |
| 1188 | self.assertEqual(header, 'first-value, second-value') |
| 1189 | |
| 1190 | header = self.resp.getheader('My-Header', 'some default') |
| 1191 | self.assertEqual(header, 'first-value, second-value') |
| 1192 | |
| 1193 | def test_getting_nonexistent_header_with_string_default(self): |
| 1194 | header = self.resp.getheader('No-Such-Header', 'default-value') |
| 1195 | self.assertEqual(header, 'default-value') |
| 1196 | |
| 1197 | def test_getting_nonexistent_header_with_iterable_default(self): |
| 1198 | header = self.resp.getheader('No-Such-Header', ['default', 'values']) |
| 1199 | self.assertEqual(header, 'default, values') |
| 1200 | |
| 1201 | header = self.resp.getheader('No-Such-Header', ('default', 'values')) |
| 1202 | self.assertEqual(header, 'default, values') |
| 1203 | |
| 1204 | def test_getting_nonexistent_header_without_default(self): |
| 1205 | header = self.resp.getheader('No-Such-Header') |
| 1206 | self.assertEqual(header, None) |
| 1207 | |
| 1208 | def test_getting_header_defaultint(self): |
| 1209 | header = self.resp.getheader('No-Such-Header',default=42) |
| 1210 | self.assertEqual(header, 42) |
| 1211 | |
Senthil Kumaran | 9da047b | 2014-04-14 13:07:56 -0400 | [diff] [blame] | 1212 | class TunnelTests(TestCase): |
| 1213 | |
| 1214 | def test_connect(self): |
| 1215 | response_text = ( |
| 1216 | 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT |
| 1217 | 'HTTP/1.1 200 OK\r\n' # Reply to HEAD |
| 1218 | 'Content-Length: 42\r\n\r\n' |
| 1219 | ) |
| 1220 | |
| 1221 | def create_connection(address, timeout=None, source_address=None): |
| 1222 | return FakeSocket(response_text, host=address[0], |
| 1223 | port=address[1]) |
| 1224 | |
| 1225 | conn = client.HTTPConnection('proxy.com') |
| 1226 | conn._create_connection = create_connection |
| 1227 | |
| 1228 | # Once connected, we shouldn't be able to tunnel anymore |
| 1229 | conn.connect() |
| 1230 | self.assertRaises(RuntimeError, conn.set_tunnel, |
| 1231 | 'destination.com') |
| 1232 | |
| 1233 | # But if we close the connection, we're good |
| 1234 | conn.close() |
| 1235 | conn.set_tunnel('destination.com') |
| 1236 | conn.request('HEAD', '/', '') |
| 1237 | |
| 1238 | self.assertEqual(conn.sock.host, 'proxy.com') |
| 1239 | self.assertEqual(conn.sock.port, 80) |
| 1240 | self.assertTrue(b'CONNECT destination.com' in conn.sock.data) |
| 1241 | self.assertTrue(b'Host: destination.com' in conn.sock.data) |
| 1242 | |
| 1243 | # This test should be removed when CONNECT gets the HTTP/1.1 blessing |
| 1244 | self.assertTrue(b'Host: proxy.com' not in conn.sock.data) |
| 1245 | |
| 1246 | conn.close() |
| 1247 | conn.request('PUT', '/', '') |
| 1248 | self.assertEqual(conn.sock.host, 'proxy.com') |
| 1249 | self.assertEqual(conn.sock.port, 80) |
| 1250 | self.assertTrue(b'CONNECT destination.com' in conn.sock.data) |
| 1251 | self.assertTrue(b'Host: destination.com' in conn.sock.data) |
| 1252 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 1253 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1254 | support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 1255 | HTTPSTest, RequestBodyTest, SourceAddressTest, |
Kristján Valur Jónsson | 8e5d0ca | 2014-03-19 10:07:26 +0000 | [diff] [blame] | 1256 | HTTPResponseTest, ExtendedReadTest, |
Senthil Kumaran | 166214c | 2014-04-14 13:10:05 -0400 | [diff] [blame] | 1257 | ExtendedReadTestChunked, TunnelTests) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 1258 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1259 | if __name__ == '__main__': |
| 1260 | test_main() |