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