Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame^] | 1 | import http.client as httplib |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 2 | import io |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3 | import socket |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 4 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 5 | from unittest import TestCase |
| 6 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | HOST = support.HOST |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 10 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 11 | class FakeSocket: |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 12 | def __init__(self, text, fileclass=io.BytesIO): |
| 13 | if isinstance(text, str): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 14 | text = text.encode("ascii") |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 15 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 16 | self.fileclass = fileclass |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 17 | self.data = b'' |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 18 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 19 | def sendall(self, data): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | self.data += data |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 21 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 22 | def makefile(self, mode, bufsize=None): |
| 23 | if mode != 'r' and mode != 'rb': |
Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 24 | raise httplib.UnimplementedFileMode() |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 25 | return self.fileclass(self.text) |
| 26 | |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 27 | class NoEOFStringIO(io.BytesIO): |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 28 | """Like StringIO, but raises AssertionError on EOF. |
| 29 | |
| 30 | This is used below to test that httplib doesn't try to read |
| 31 | more from the underlying file than it should. |
| 32 | """ |
| 33 | def read(self, n=-1): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 34 | data = io.BytesIO.read(self, n) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 35 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 36 | raise AssertionError('caller tried to read past EOF') |
| 37 | return data |
| 38 | |
| 39 | def readline(self, length=None): |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 40 | data = io.BytesIO.readline(self, length) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 41 | if data == b'': |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 42 | raise AssertionError('caller tried to read past EOF') |
| 43 | return data |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 44 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 45 | |
| 46 | class HeaderTests(TestCase): |
| 47 | def test_auto_headers(self): |
| 48 | # Some headers are added automatically, but should not be added by |
| 49 | # .request() if they are explicitly set. |
| 50 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 51 | class HeaderCountingBuffer(list): |
| 52 | def __init__(self): |
| 53 | self.count = {} |
| 54 | def append(self, item): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 55 | kv = item.split(b':') |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 56 | if len(kv) > 1: |
| 57 | # item is a 'Key: Value' header string |
Martin v. Löwis | dd5a860 | 2007-06-30 09:22:09 +0000 | [diff] [blame] | 58 | lcKey = kv[0].decode('ascii').lower() |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 59 | self.count.setdefault(lcKey, 0) |
| 60 | self.count[lcKey] += 1 |
| 61 | list.append(self, item) |
| 62 | |
| 63 | for explicit_header in True, False: |
| 64 | for header in 'Content-length', 'Host', 'Accept-encoding': |
| 65 | conn = httplib.HTTPConnection('example.com') |
| 66 | conn.sock = FakeSocket('blahblahblah') |
| 67 | conn._buffer = HeaderCountingBuffer() |
| 68 | |
| 69 | body = 'spamspamspam' |
| 70 | headers = {} |
| 71 | if explicit_header: |
| 72 | headers[header] = str(len(body)) |
| 73 | conn.request('POST', '/', body, headers) |
| 74 | self.assertEqual(conn._buffer.count[header.lower()], 1) |
| 75 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | class BasicTest(TestCase): |
| 77 | def test_status_lines(self): |
| 78 | # Test HTTP status lines |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 81 | sock = FakeSocket(body) |
| 82 | resp = httplib.HTTPResponse(sock) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 83 | resp.begin() |
Jeremy Hylton | 8fff792 | 2007-08-03 20:56:14 +0000 | [diff] [blame] | 84 | self.assertEqual(resp.read(), b"Text") |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 85 | self.assertTrue(resp.isclosed()) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 87 | body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
| 88 | sock = FakeSocket(body) |
| 89 | resp = httplib.HTTPResponse(sock) |
| 90 | self.assertRaises(httplib.BadStatusLine, resp.begin) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 92 | def test_partial_reads(self): |
| 93 | # if we have a lenght, the system knows when to close itself |
| 94 | # same behaviour than when we read the whole thing with read() |
| 95 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 96 | sock = FakeSocket(body) |
| 97 | resp = httplib.HTTPResponse(sock) |
| 98 | resp.begin() |
| 99 | self.assertEqual(resp.read(2), b'Te') |
| 100 | self.assertFalse(resp.isclosed()) |
| 101 | self.assertEqual(resp.read(2), b'xt') |
| 102 | self.assertTrue(resp.isclosed()) |
| 103 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 104 | def test_host_port(self): |
| 105 | # Check invalid host_port |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | for hp in ("www.python.org:abc", "www.python.org:"): |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 108 | self.assertRaises(httplib.InvalidURL, httplib.HTTPConnection, hp) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 110 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
| 111 | "fe80::207:e9ff:fe9b", 8000), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | ("www.python.org:80", "www.python.org", 80), |
| 113 | ("www.python.org", "www.python.org", 80), |
| 114 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)): |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 115 | c = httplib.HTTPConnection(hp) |
| 116 | self.assertEqual(h, c.host) |
| 117 | self.assertEqual(p, c.port) |
Skip Montanaro | 10e6e0e | 2004-09-14 16:32:02 +0000 | [diff] [blame] | 118 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | def test_response_headers(self): |
| 120 | # test response with multiple message headers with the same field name. |
| 121 | text = ('HTTP/1.1 200 OK\r\n' |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 122 | 'Set-Cookie: Customer="WILE_E_COYOTE"; ' |
| 123 | 'Version="1"; Path="/acme"\r\n' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
| 125 | ' Path="/acme"\r\n' |
| 126 | '\r\n' |
| 127 | 'No body\r\n') |
| 128 | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
| 129 | ', ' |
| 130 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
| 131 | s = FakeSocket(text) |
| 132 | r = httplib.HTTPResponse(s) |
| 133 | r.begin() |
| 134 | cookies = r.getheader("Set-Cookie") |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 135 | self.assertEqual(cookies, hdr) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 136 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | def test_read_head(self): |
| 138 | # Test that the library doesn't attempt to read any data |
| 139 | # from a HEAD request. (Tickles SF bug #622042.) |
| 140 | sock = FakeSocket( |
| 141 | 'HTTP/1.1 200 OK\r\n' |
| 142 | 'Content-Length: 14432\r\n' |
| 143 | '\r\n', |
| 144 | NoEOFStringIO) |
| 145 | resp = httplib.HTTPResponse(sock, method="HEAD") |
| 146 | resp.begin() |
Guido van Rossum | a00f123 | 2007-09-12 19:43:09 +0000 | [diff] [blame] | 147 | if resp.read(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 148 | self.fail("Did not expect response from HEAD request") |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 149 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 150 | def test_send_file(self): |
Guido van Rossum | 022c474 | 2007-08-29 02:00:20 +0000 | [diff] [blame] | 151 | expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' |
| 152 | b'Accept-Encoding: identity\r\nContent-Length:') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 153 | |
| 154 | body = open(__file__, 'rb') |
| 155 | conn = httplib.HTTPConnection('example.com') |
| 156 | sock = FakeSocket(body) |
| 157 | conn.sock = sock |
| 158 | conn.request('GET', '/foo', body) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 159 | self.assertTrue(sock.data.startswith(expected), '%r != %r' % |
| 160 | (sock.data[:len(expected)], expected)) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 161 | |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 162 | def test_chunked(self): |
| 163 | chunked_start = ( |
| 164 | 'HTTP/1.1 200 OK\r\n' |
| 165 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 166 | 'a\r\n' |
| 167 | 'hello worl\r\n' |
| 168 | '1\r\n' |
| 169 | 'd\r\n' |
| 170 | ) |
| 171 | sock = FakeSocket(chunked_start + '0\r\n') |
| 172 | resp = httplib.HTTPResponse(sock, method="GET") |
| 173 | resp.begin() |
| 174 | self.assertEquals(resp.read(), b'hello world') |
| 175 | resp.close() |
| 176 | |
| 177 | for x in ('', 'foo\r\n'): |
| 178 | sock = FakeSocket(chunked_start + x) |
| 179 | resp = httplib.HTTPResponse(sock, method="GET") |
| 180 | resp.begin() |
| 181 | try: |
| 182 | resp.read() |
| 183 | except httplib.IncompleteRead as i: |
| 184 | self.assertEquals(i.partial, b'hello world') |
| 185 | else: |
| 186 | self.fail('IncompleteRead expected') |
| 187 | finally: |
| 188 | resp.close() |
| 189 | |
| 190 | def test_negative_content_length(self): |
| 191 | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n') |
| 192 | resp = httplib.HTTPResponse(sock, method="GET") |
| 193 | resp.begin() |
| 194 | self.assertEquals(resp.read(), b'Hello\r\n') |
| 195 | resp.close() |
| 196 | |
| 197 | |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 198 | class OfflineTest(TestCase): |
| 199 | def test_responses(self): |
| 200 | self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found") |
| 201 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 202 | class TimeoutTest(TestCase): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 203 | PORT = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 204 | |
| 205 | def setUp(self): |
| 206 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 207 | TimeoutTest.PORT = support.bind_port(self.serv) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 208 | self.serv.listen(5) |
| 209 | |
| 210 | def tearDown(self): |
| 211 | self.serv.close() |
| 212 | self.serv = None |
| 213 | |
| 214 | def testTimeoutAttribute(self): |
Jeremy Hylton | 3a38c91 | 2007-08-14 17:08:07 +0000 | [diff] [blame] | 215 | # This will prove that the timeout gets through HTTPConnection |
| 216 | # and into the socket. |
| 217 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 218 | # default |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 219 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 220 | httpConn.connect() |
| 221 | self.assertTrue(httpConn.sock.gettimeout() is None) |
| 222 | httpConn.close() |
| 223 | |
| 224 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 225 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 226 | httpConn.connect() |
| 227 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 228 | httpConn.close() |
| 229 | |
| 230 | # None, having other default |
| 231 | previous = socket.getdefaulttimeout() |
| 232 | socket.setdefaulttimeout(30) |
| 233 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 234 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, |
| 235 | timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 236 | httpConn.connect() |
| 237 | finally: |
| 238 | socket.setdefaulttimeout(previous) |
| 239 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
| 240 | httpConn.close() |
| 241 | |
| 242 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 243 | class HTTPSTimeoutTest(TestCase): |
| 244 | # XXX Here should be tests for HTTPS, there isn't any right now! |
| 245 | |
| 246 | def test_attributes(self): |
| 247 | # simple test to check it's storing it |
Thomas Wouters | 582b586 | 2007-08-30 22:39:17 +0000 | [diff] [blame] | 248 | if hasattr(httplib, 'HTTPSConnection'): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 249 | h = httplib.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
Thomas Wouters | 582b586 | 2007-08-30 22:39:17 +0000 | [diff] [blame] | 250 | self.assertEqual(h.timeout, 30) |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 251 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 252 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 253 | support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 254 | HTTPSTimeoutTest) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 255 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | if __name__ == '__main__': |
| 257 | test_main() |