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