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