Senthil Kumaran | aa5f49e | 2010-10-03 18:26:07 +0000 | [diff] [blame] | 1 | import httplib |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 2 | import array |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 3 | import httplib |
| 4 | import StringIO |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 5 | import socket |
Victor Stinner | 2c6aee9 | 2010-07-24 02:46:16 +0000 | [diff] [blame] | 6 | import errno |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 7 | |
Gregory P. Smith | 9d32521 | 2010-01-03 02:06:07 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | TestCase = unittest.TestCase |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 10 | |
| 11 | from test import test_support |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 12 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 13 | HOST = test_support.HOST |
| 14 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 15 | class FakeSocket: |
Senthil Kumaran | 36f28f7 | 2014-05-16 18:51:46 -0700 | [diff] [blame] | 16 | def __init__(self, text, fileclass=StringIO.StringIO, host=None, port=None): |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 17 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 18 | self.fileclass = fileclass |
Martin v. Löwis | 040a927 | 2006-11-12 10:32:47 +0000 | [diff] [blame] | 19 | self.data = '' |
Senthil Kumaran | 36f28f7 | 2014-05-16 18:51:46 -0700 | [diff] [blame] | 20 | self.host = host |
| 21 | self.port = port |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 22 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 23 | def sendall(self, data): |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 24 | self.data += ''.join(data) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 25 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 26 | def makefile(self, mode, bufsize=None): |
| 27 | if mode != 'r' and mode != 'rb': |
Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 28 | raise httplib.UnimplementedFileMode() |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 29 | return self.fileclass(self.text) |
| 30 | |
Senthil Kumaran | 36f28f7 | 2014-05-16 18:51:46 -0700 | [diff] [blame] | 31 | def close(self): |
| 32 | pass |
| 33 | |
Victor Stinner | 2c6aee9 | 2010-07-24 02:46:16 +0000 | [diff] [blame] | 34 | class EPipeSocket(FakeSocket): |
| 35 | |
| 36 | def __init__(self, text, pipe_trigger): |
| 37 | # When sendall() is called with pipe_trigger, raise EPIPE. |
| 38 | FakeSocket.__init__(self, text) |
| 39 | self.pipe_trigger = pipe_trigger |
| 40 | |
| 41 | def sendall(self, data): |
| 42 | if self.pipe_trigger in data: |
| 43 | raise socket.error(errno.EPIPE, "gotcha") |
| 44 | self.data += data |
| 45 | |
| 46 | def close(self): |
| 47 | pass |
| 48 | |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 49 | class NoEOFStringIO(StringIO.StringIO): |
| 50 | """Like StringIO, but raises AssertionError on EOF. |
| 51 | |
| 52 | This is used below to test that httplib doesn't try to read |
| 53 | more from the underlying file than it should. |
| 54 | """ |
| 55 | def read(self, n=-1): |
| 56 | data = StringIO.StringIO.read(self, n) |
| 57 | if data == '': |
| 58 | raise AssertionError('caller tried to read past EOF') |
| 59 | return data |
| 60 | |
| 61 | def readline(self, length=None): |
| 62 | data = StringIO.StringIO.readline(self, length) |
| 63 | if data == '': |
| 64 | raise AssertionError('caller tried to read past EOF') |
| 65 | return data |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 66 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 67 | |
| 68 | class HeaderTests(TestCase): |
| 69 | def test_auto_headers(self): |
| 70 | # Some headers are added automatically, but should not be added by |
| 71 | # .request() if they are explicitly set. |
| 72 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 73 | class HeaderCountingBuffer(list): |
| 74 | def __init__(self): |
| 75 | self.count = {} |
| 76 | def append(self, item): |
| 77 | kv = item.split(':') |
| 78 | if len(kv) > 1: |
| 79 | # item is a 'Key: Value' header string |
| 80 | lcKey = kv[0].lower() |
| 81 | self.count.setdefault(lcKey, 0) |
| 82 | self.count[lcKey] += 1 |
| 83 | list.append(self, item) |
| 84 | |
| 85 | for explicit_header in True, False: |
| 86 | for header in 'Content-length', 'Host', 'Accept-encoding': |
| 87 | conn = httplib.HTTPConnection('example.com') |
| 88 | conn.sock = FakeSocket('blahblahblah') |
| 89 | conn._buffer = HeaderCountingBuffer() |
| 90 | |
| 91 | body = 'spamspamspam' |
| 92 | headers = {} |
| 93 | if explicit_header: |
| 94 | headers[header] = str(len(body)) |
| 95 | conn.request('POST', '/', body, headers) |
| 96 | self.assertEqual(conn._buffer.count[header.lower()], 1) |
| 97 | |
Senthil Kumaran | 618802d | 2012-05-19 16:52:21 +0800 | [diff] [blame] | 98 | def test_content_length_0(self): |
| 99 | |
| 100 | class ContentLengthChecker(list): |
| 101 | def __init__(self): |
| 102 | list.__init__(self) |
| 103 | self.content_length = None |
| 104 | def append(self, item): |
| 105 | kv = item.split(':', 1) |
| 106 | if len(kv) > 1 and kv[0].lower() == 'content-length': |
| 107 | self.content_length = kv[1].strip() |
| 108 | list.append(self, item) |
| 109 | |
| 110 | # POST with empty body |
| 111 | conn = httplib.HTTPConnection('example.com') |
| 112 | conn.sock = FakeSocket(None) |
| 113 | conn._buffer = ContentLengthChecker() |
| 114 | conn.request('POST', '/', '') |
| 115 | self.assertEqual(conn._buffer.content_length, '0', |
| 116 | 'Header Content-Length not set') |
| 117 | |
| 118 | # PUT request with empty body |
| 119 | conn = httplib.HTTPConnection('example.com') |
| 120 | conn.sock = FakeSocket(None) |
| 121 | conn._buffer = ContentLengthChecker() |
| 122 | conn.request('PUT', '/', '') |
| 123 | self.assertEqual(conn._buffer.content_length, '0', |
| 124 | 'Header Content-Length not set') |
| 125 | |
Senthil Kumaran | aa5f49e | 2010-10-03 18:26:07 +0000 | [diff] [blame] | 126 | def test_putheader(self): |
| 127 | conn = httplib.HTTPConnection('example.com') |
| 128 | conn.sock = FakeSocket(None) |
| 129 | conn.putrequest('GET','/') |
| 130 | conn.putheader('Content-length',42) |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 131 | self.assertIn('Content-length: 42', conn._buffer) |
Senthil Kumaran | aa5f49e | 2010-10-03 18:26:07 +0000 | [diff] [blame] | 132 | |
Senthil Kumaran | 501bfd8 | 2010-11-14 03:31:52 +0000 | [diff] [blame] | 133 | def test_ipv6host_header(self): |
| 134 | # Default host header on IPv6 transaction should wrapped by [] if |
| 135 | # its actual IPv6 address |
| 136 | expected = 'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \ |
| 137 | 'Accept-Encoding: identity\r\n\r\n' |
| 138 | conn = httplib.HTTPConnection('[2001::]:81') |
| 139 | sock = FakeSocket('') |
| 140 | conn.sock = sock |
| 141 | conn.request('GET', '/foo') |
| 142 | self.assertTrue(sock.data.startswith(expected)) |
| 143 | |
| 144 | expected = 'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \ |
| 145 | 'Accept-Encoding: identity\r\n\r\n' |
| 146 | conn = httplib.HTTPConnection('[2001:102A::]') |
| 147 | sock = FakeSocket('') |
| 148 | conn.sock = sock |
| 149 | conn.request('GET', '/foo') |
| 150 | self.assertTrue(sock.data.startswith(expected)) |
| 151 | |
| 152 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 153 | class BasicTest(TestCase): |
| 154 | def test_status_lines(self): |
| 155 | # Test HTTP status lines |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 156 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 157 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 158 | sock = FakeSocket(body) |
| 159 | resp = httplib.HTTPResponse(sock) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 160 | resp.begin() |
Serhiy Storchaka | c97f5ed | 2013-12-17 21:49:48 +0200 | [diff] [blame] | 161 | self.assertEqual(resp.read(0), '') # Issue #20007 |
| 162 | self.assertFalse(resp.isclosed()) |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 163 | self.assertEqual(resp.read(), 'Text') |
Facundo Batista | 7066590 | 2007-10-18 03:16:03 +0000 | [diff] [blame] | 164 | self.assertTrue(resp.isclosed()) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 165 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 166 | body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
| 167 | sock = FakeSocket(body) |
| 168 | resp = httplib.HTTPResponse(sock) |
| 169 | self.assertRaises(httplib.BadStatusLine, resp.begin) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 170 | |
Dirkjan Ochtman | ebc73dc | 2010-02-24 04:49:00 +0000 | [diff] [blame] | 171 | def test_bad_status_repr(self): |
| 172 | exc = httplib.BadStatusLine('') |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 173 | self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') |
Dirkjan Ochtman | ebc73dc | 2010-02-24 04:49:00 +0000 | [diff] [blame] | 174 | |
Facundo Batista | 7066590 | 2007-10-18 03:16:03 +0000 | [diff] [blame] | 175 | def test_partial_reads(self): |
Antoine Pitrou | 4113d2b | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 176 | # if we have a length, the system knows when to close itself |
Facundo Batista | 7066590 | 2007-10-18 03:16:03 +0000 | [diff] [blame] | 177 | # same behaviour than when we read the whole thing with read() |
| 178 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
| 179 | sock = FakeSocket(body) |
| 180 | resp = httplib.HTTPResponse(sock) |
| 181 | resp.begin() |
| 182 | self.assertEqual(resp.read(2), 'Te') |
| 183 | self.assertFalse(resp.isclosed()) |
| 184 | self.assertEqual(resp.read(2), 'xt') |
| 185 | self.assertTrue(resp.isclosed()) |
| 186 | |
Antoine Pitrou | 4113d2b | 2012-12-15 19:11:54 +0100 | [diff] [blame] | 187 | def test_partial_reads_no_content_length(self): |
| 188 | # when no length is present, the socket should be gracefully closed when |
| 189 | # all data was read |
| 190 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 191 | sock = FakeSocket(body) |
| 192 | resp = httplib.HTTPResponse(sock) |
| 193 | resp.begin() |
| 194 | self.assertEqual(resp.read(2), 'Te') |
| 195 | self.assertFalse(resp.isclosed()) |
| 196 | self.assertEqual(resp.read(2), 'xt') |
| 197 | self.assertEqual(resp.read(1), '') |
| 198 | self.assertTrue(resp.isclosed()) |
| 199 | |
Antoine Pitrou | d66c0ee | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 200 | def test_partial_reads_incomplete_body(self): |
| 201 | # if the server shuts down the connection before the whole |
| 202 | # content-length is delivered, the socket is gracefully closed |
| 203 | body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText" |
| 204 | sock = FakeSocket(body) |
| 205 | resp = httplib.HTTPResponse(sock) |
| 206 | resp.begin() |
| 207 | self.assertEqual(resp.read(2), 'Te') |
| 208 | self.assertFalse(resp.isclosed()) |
| 209 | self.assertEqual(resp.read(2), 'xt') |
| 210 | self.assertEqual(resp.read(1), '') |
| 211 | self.assertTrue(resp.isclosed()) |
| 212 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 213 | def test_host_port(self): |
| 214 | # Check invalid host_port |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 215 | |
Łukasz Langa | 7a15390 | 2011-10-18 17:16:00 +0200 | [diff] [blame] | 216 | # Note that httplib does not accept user:password@ in the host-port. |
| 217 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 218 | self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp) |
| 219 | |
Jeremy Hylton | 21d2e59 | 2008-11-29 00:09:16 +0000 | [diff] [blame] | 220 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", |
| 221 | 8000), |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 222 | ("www.python.org:80", "www.python.org", 80), |
| 223 | ("www.python.org", "www.python.org", 80), |
Łukasz Langa | 7a15390 | 2011-10-18 17:16:00 +0200 | [diff] [blame] | 224 | ("www.python.org:", "www.python.org", 80), |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 225 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)): |
Martin v. Löwis | 74a249e | 2004-09-14 21:45:36 +0000 | [diff] [blame] | 226 | http = httplib.HTTP(hp) |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 227 | c = http._conn |
Jeremy Hylton | 21d2e59 | 2008-11-29 00:09:16 +0000 | [diff] [blame] | 228 | if h != c.host: |
| 229 | self.fail("Host incorrectly parsed: %s != %s" % (h, c.host)) |
| 230 | if p != c.port: |
| 231 | self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) |
Skip Montanaro | 10e6e0e | 2004-09-14 16:32:02 +0000 | [diff] [blame] | 232 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 233 | def test_response_headers(self): |
| 234 | # test response with multiple message headers with the same field name. |
| 235 | text = ('HTTP/1.1 200 OK\r\n' |
Jeremy Hylton | 21d2e59 | 2008-11-29 00:09:16 +0000 | [diff] [blame] | 236 | 'Set-Cookie: Customer="WILE_E_COYOTE";' |
| 237 | ' Version="1"; Path="/acme"\r\n' |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 238 | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
| 239 | ' Path="/acme"\r\n' |
| 240 | '\r\n' |
| 241 | 'No body\r\n') |
| 242 | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
| 243 | ', ' |
| 244 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
| 245 | s = FakeSocket(text) |
| 246 | r = httplib.HTTPResponse(s) |
| 247 | r.begin() |
| 248 | cookies = r.getheader("Set-Cookie") |
| 249 | if cookies != hdr: |
| 250 | self.fail("multiple headers not combined properly") |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 251 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 252 | def test_read_head(self): |
| 253 | # Test that the library doesn't attempt to read any data |
| 254 | # from a HEAD request. (Tickles SF bug #622042.) |
| 255 | sock = FakeSocket( |
| 256 | 'HTTP/1.1 200 OK\r\n' |
| 257 | 'Content-Length: 14432\r\n' |
| 258 | '\r\n', |
| 259 | NoEOFStringIO) |
| 260 | resp = httplib.HTTPResponse(sock, method="HEAD") |
| 261 | resp.begin() |
| 262 | if resp.read() != "": |
| 263 | self.fail("Did not expect response from HEAD request") |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 264 | |
Berker Peksag | b7414e0 | 2014-08-05 07:15:57 +0300 | [diff] [blame^] | 265 | def test_too_many_headers(self): |
| 266 | headers = '\r\n'.join('Header%d: foo' % i for i in xrange(200)) + '\r\n' |
| 267 | text = ('HTTP/1.1 200 OK\r\n' + headers) |
| 268 | s = FakeSocket(text) |
| 269 | r = httplib.HTTPResponse(s) |
| 270 | self.assertRaises(httplib.HTTPException, r.begin) |
| 271 | |
Martin v. Löwis | 040a927 | 2006-11-12 10:32:47 +0000 | [diff] [blame] | 272 | def test_send_file(self): |
| 273 | expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
| 274 | 'Accept-Encoding: identity\r\nContent-Length:' |
| 275 | |
| 276 | body = open(__file__, 'rb') |
| 277 | conn = httplib.HTTPConnection('example.com') |
| 278 | sock = FakeSocket(body) |
| 279 | conn.sock = sock |
| 280 | conn.request('GET', '/foo', body) |
| 281 | self.assertTrue(sock.data.startswith(expected)) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 283 | def test_send(self): |
| 284 | expected = 'this is a test this is only a test' |
| 285 | conn = httplib.HTTPConnection('example.com') |
| 286 | sock = FakeSocket(None) |
| 287 | conn.sock = sock |
| 288 | conn.send(expected) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 289 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 290 | sock.data = '' |
| 291 | conn.send(array.array('c', expected)) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 292 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 293 | sock.data = '' |
| 294 | conn.send(StringIO.StringIO(expected)) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 295 | self.assertEqual(expected, sock.data) |
Antoine Pitrou | 7248178 | 2009-09-29 17:48:18 +0000 | [diff] [blame] | 296 | |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 297 | def test_chunked(self): |
| 298 | chunked_start = ( |
| 299 | 'HTTP/1.1 200 OK\r\n' |
| 300 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 301 | 'a\r\n' |
| 302 | 'hello worl\r\n' |
| 303 | '1\r\n' |
| 304 | 'd\r\n' |
| 305 | ) |
| 306 | sock = FakeSocket(chunked_start + '0\r\n') |
| 307 | resp = httplib.HTTPResponse(sock, method="GET") |
| 308 | resp.begin() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 309 | self.assertEqual(resp.read(), 'hello world') |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 310 | resp.close() |
| 311 | |
| 312 | for x in ('', 'foo\r\n'): |
| 313 | sock = FakeSocket(chunked_start + x) |
| 314 | resp = httplib.HTTPResponse(sock, method="GET") |
| 315 | resp.begin() |
| 316 | try: |
| 317 | resp.read() |
| 318 | except httplib.IncompleteRead, i: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 319 | self.assertEqual(i.partial, 'hello world') |
Benjamin Peterson | 7d49bba | 2009-03-02 22:41:42 +0000 | [diff] [blame] | 320 | self.assertEqual(repr(i),'IncompleteRead(11 bytes read)') |
| 321 | self.assertEqual(str(i),'IncompleteRead(11 bytes read)') |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 322 | else: |
| 323 | self.fail('IncompleteRead expected') |
| 324 | finally: |
| 325 | resp.close() |
| 326 | |
Senthil Kumaran | ed920434 | 2010-04-28 17:20:43 +0000 | [diff] [blame] | 327 | def test_chunked_head(self): |
| 328 | chunked_start = ( |
| 329 | 'HTTP/1.1 200 OK\r\n' |
| 330 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 331 | 'a\r\n' |
| 332 | 'hello world\r\n' |
| 333 | '1\r\n' |
| 334 | 'd\r\n' |
| 335 | ) |
| 336 | sock = FakeSocket(chunked_start + '0\r\n') |
| 337 | resp = httplib.HTTPResponse(sock, method="HEAD") |
| 338 | resp.begin() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 339 | self.assertEqual(resp.read(), '') |
| 340 | self.assertEqual(resp.status, 200) |
| 341 | self.assertEqual(resp.reason, 'OK') |
Senthil Kumaran | fb69501 | 2010-06-04 17:17:09 +0000 | [diff] [blame] | 342 | self.assertTrue(resp.isclosed()) |
Senthil Kumaran | ed920434 | 2010-04-28 17:20:43 +0000 | [diff] [blame] | 343 | |
Georg Brandl | 8c460d5 | 2008-02-24 00:14:24 +0000 | [diff] [blame] | 344 | def test_negative_content_length(self): |
Jeremy Hylton | 21d2e59 | 2008-11-29 00:09:16 +0000 | [diff] [blame] | 345 | sock = FakeSocket('HTTP/1.1 200 OK\r\n' |
| 346 | 'Content-Length: -1\r\n\r\nHello\r\n') |
Georg Brandl | 8c460d5 | 2008-02-24 00:14:24 +0000 | [diff] [blame] | 347 | resp = httplib.HTTPResponse(sock, method="GET") |
| 348 | resp.begin() |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 349 | self.assertEqual(resp.read(), 'Hello\r\n') |
Antoine Pitrou | d66c0ee | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 350 | self.assertTrue(resp.isclosed()) |
Georg Brandl | 8c460d5 | 2008-02-24 00:14:24 +0000 | [diff] [blame] | 351 | |
Benjamin Peterson | 7d49bba | 2009-03-02 22:41:42 +0000 | [diff] [blame] | 352 | def test_incomplete_read(self): |
| 353 | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n') |
| 354 | resp = httplib.HTTPResponse(sock, method="GET") |
| 355 | resp.begin() |
| 356 | try: |
| 357 | resp.read() |
| 358 | except httplib.IncompleteRead as i: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 359 | self.assertEqual(i.partial, 'Hello\r\n') |
Benjamin Peterson | 7d49bba | 2009-03-02 22:41:42 +0000 | [diff] [blame] | 360 | self.assertEqual(repr(i), |
| 361 | "IncompleteRead(7 bytes read, 3 more expected)") |
| 362 | self.assertEqual(str(i), |
| 363 | "IncompleteRead(7 bytes read, 3 more expected)") |
Antoine Pitrou | d66c0ee | 2013-02-02 22:49:34 +0100 | [diff] [blame] | 364 | self.assertTrue(resp.isclosed()) |
Benjamin Peterson | 7d49bba | 2009-03-02 22:41:42 +0000 | [diff] [blame] | 365 | else: |
| 366 | self.fail('IncompleteRead expected') |
Benjamin Peterson | 7d49bba | 2009-03-02 22:41:42 +0000 | [diff] [blame] | 367 | |
Victor Stinner | 2c6aee9 | 2010-07-24 02:46:16 +0000 | [diff] [blame] | 368 | def test_epipe(self): |
| 369 | sock = EPipeSocket( |
| 370 | "HTTP/1.0 401 Authorization Required\r\n" |
| 371 | "Content-type: text/html\r\n" |
| 372 | "WWW-Authenticate: Basic realm=\"example\"\r\n", |
| 373 | b"Content-Length") |
| 374 | conn = httplib.HTTPConnection("example.com") |
| 375 | conn.sock = sock |
| 376 | self.assertRaises(socket.error, |
| 377 | lambda: conn.request("PUT", "/url", "body")) |
| 378 | resp = conn.getresponse() |
| 379 | self.assertEqual(401, resp.status) |
| 380 | self.assertEqual("Basic realm=\"example\"", |
| 381 | resp.getheader("www-authenticate")) |
| 382 | |
Senthil Kumaran | d389cb5 | 2010-09-21 01:38:15 +0000 | [diff] [blame] | 383 | def test_filenoattr(self): |
| 384 | # Just test the fileno attribute in the HTTPResponse Object. |
| 385 | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
| 386 | sock = FakeSocket(body) |
| 387 | resp = httplib.HTTPResponse(sock) |
| 388 | self.assertTrue(hasattr(resp,'fileno'), |
| 389 | 'HTTPResponse should expose a fileno attribute') |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | d7b6ac6 | 2010-12-18 18:18:21 +0000 | [diff] [blame] | 391 | # Test lines overflowing the max line size (_MAXLINE in http.client) |
| 392 | |
| 393 | def test_overflowing_status_line(self): |
| 394 | self.skipTest("disabled for HTTP 0.9 support") |
| 395 | body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n" |
| 396 | resp = httplib.HTTPResponse(FakeSocket(body)) |
| 397 | self.assertRaises((httplib.LineTooLong, httplib.BadStatusLine), resp.begin) |
| 398 | |
| 399 | def test_overflowing_header_line(self): |
| 400 | body = ( |
| 401 | 'HTTP/1.1 200 OK\r\n' |
| 402 | 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n' |
| 403 | ) |
| 404 | resp = httplib.HTTPResponse(FakeSocket(body)) |
| 405 | self.assertRaises(httplib.LineTooLong, resp.begin) |
| 406 | |
| 407 | def test_overflowing_chunked_line(self): |
| 408 | body = ( |
| 409 | 'HTTP/1.1 200 OK\r\n' |
| 410 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 411 | + '0' * 65536 + 'a\r\n' |
| 412 | 'hello world\r\n' |
| 413 | '0\r\n' |
| 414 | ) |
| 415 | resp = httplib.HTTPResponse(FakeSocket(body)) |
| 416 | resp.begin() |
| 417 | self.assertRaises(httplib.LineTooLong, resp.read) |
| 418 | |
Senthil Kumaran | f5aaf6f | 2012-04-29 10:15:31 +0800 | [diff] [blame] | 419 | def test_early_eof(self): |
| 420 | # Test httpresponse with no \r\n termination, |
| 421 | body = "HTTP/1.1 200 Ok" |
| 422 | sock = FakeSocket(body) |
| 423 | resp = httplib.HTTPResponse(sock) |
| 424 | resp.begin() |
| 425 | self.assertEqual(resp.read(), '') |
| 426 | self.assertTrue(resp.isclosed()) |
Antoine Pitrou | d7b6ac6 | 2010-12-18 18:18:21 +0000 | [diff] [blame] | 427 | |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 428 | class OfflineTest(TestCase): |
| 429 | def test_responses(self): |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 430 | self.assertEqual(httplib.responses[httplib.NOT_FOUND], "Not Found") |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 431 | |
Gregory P. Smith | 9d32521 | 2010-01-03 02:06:07 +0000 | [diff] [blame] | 432 | |
| 433 | class SourceAddressTest(TestCase): |
| 434 | def setUp(self): |
| 435 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 436 | self.port = test_support.bind_port(self.serv) |
| 437 | self.source_port = test_support.find_unused_port() |
| 438 | self.serv.listen(5) |
| 439 | self.conn = None |
| 440 | |
| 441 | def tearDown(self): |
| 442 | if self.conn: |
| 443 | self.conn.close() |
| 444 | self.conn = None |
| 445 | self.serv.close() |
| 446 | self.serv = None |
| 447 | |
| 448 | def testHTTPConnectionSourceAddress(self): |
| 449 | self.conn = httplib.HTTPConnection(HOST, self.port, |
| 450 | source_address=('', self.source_port)) |
| 451 | self.conn.connect() |
| 452 | self.assertEqual(self.conn.sock.getsockname()[1], self.source_port) |
| 453 | |
| 454 | @unittest.skipIf(not hasattr(httplib, 'HTTPSConnection'), |
| 455 | 'httplib.HTTPSConnection not defined') |
| 456 | def testHTTPSConnectionSourceAddress(self): |
| 457 | self.conn = httplib.HTTPSConnection(HOST, self.port, |
| 458 | source_address=('', self.source_port)) |
| 459 | # We don't test anything here other the constructor not barfing as |
| 460 | # this code doesn't deal with setting up an active running SSL server |
| 461 | # for an ssl_wrapped connect() to actually return from. |
| 462 | |
| 463 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 464 | class TimeoutTest(TestCase): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 465 | PORT = None |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 466 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 467 | def setUp(self): |
| 468 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Trent Nelson | 6c4a7c6 | 2008-04-09 00:34:53 +0000 | [diff] [blame] | 469 | TimeoutTest.PORT = test_support.bind_port(self.serv) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 470 | self.serv.listen(5) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 471 | |
| 472 | def tearDown(self): |
| 473 | self.serv.close() |
| 474 | self.serv = None |
| 475 | |
| 476 | def testTimeoutAttribute(self): |
| 477 | '''This will prove that the timeout gets through |
| 478 | HTTPConnection and into the socket. |
| 479 | ''' |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 480 | # default -- use global socket timeout |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 481 | self.assertIsNone(socket.getdefaulttimeout()) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 482 | socket.setdefaulttimeout(30) |
| 483 | try: |
| 484 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT) |
| 485 | httpConn.connect() |
| 486 | finally: |
| 487 | socket.setdefaulttimeout(None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 488 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 489 | httpConn.close() |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 490 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 491 | # no timeout -- do not use global socket default |
Serhiy Storchaka | 528bed8 | 2014-02-08 14:49:55 +0200 | [diff] [blame] | 492 | self.assertIsNone(socket.getdefaulttimeout()) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 493 | socket.setdefaulttimeout(30) |
| 494 | try: |
Trent Nelson | 6c4a7c6 | 2008-04-09 00:34:53 +0000 | [diff] [blame] | 495 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, |
| 496 | timeout=None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 497 | httpConn.connect() |
| 498 | finally: |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 499 | socket.setdefaulttimeout(None) |
| 500 | self.assertEqual(httpConn.sock.gettimeout(), None) |
| 501 | httpConn.close() |
| 502 | |
| 503 | # a value |
| 504 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
| 505 | httpConn.connect() |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 506 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 507 | httpConn.close() |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 508 | |
| 509 | |
Facundo Batista | 70f996b | 2007-05-21 17:32:32 +0000 | [diff] [blame] | 510 | class HTTPSTimeoutTest(TestCase): |
| 511 | # XXX Here should be tests for HTTPS, there isn't any right now! |
| 512 | |
| 513 | def test_attributes(self): |
| 514 | # simple test to check it's storing it |
Thomas Wouters | 628e3bb | 2007-08-30 22:35:31 +0000 | [diff] [blame] | 515 | if hasattr(httplib, 'HTTPSConnection'): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 516 | h = httplib.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
Thomas Wouters | 628e3bb | 2007-08-30 22:35:31 +0000 | [diff] [blame] | 517 | self.assertEqual(h.timeout, 30) |
Facundo Batista | 70f996b | 2007-05-21 17:32:32 +0000 | [diff] [blame] | 518 | |
Petri Lehtinen | 6d089df | 2011-10-26 21:25:56 +0300 | [diff] [blame] | 519 | @unittest.skipIf(not hasattr(httplib, 'HTTPS'), 'httplib.HTTPS not available') |
Łukasz Langa | 7a15390 | 2011-10-18 17:16:00 +0200 | [diff] [blame] | 520 | def test_host_port(self): |
| 521 | # Check invalid host_port |
| 522 | |
| 523 | # Note that httplib does not accept user:password@ in the host-port. |
| 524 | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
| 525 | self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp) |
| 526 | |
| 527 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", |
| 528 | 8000), |
| 529 | ("pypi.python.org:443", "pypi.python.org", 443), |
| 530 | ("pypi.python.org", "pypi.python.org", 443), |
| 531 | ("pypi.python.org:", "pypi.python.org", 443), |
| 532 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443)): |
| 533 | http = httplib.HTTPS(hp) |
| 534 | c = http._conn |
| 535 | if h != c.host: |
| 536 | self.fail("Host incorrectly parsed: %s != %s" % (h, c.host)) |
| 537 | if p != c.port: |
| 538 | self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) |
| 539 | |
| 540 | |
Senthil Kumaran | 36f28f7 | 2014-05-16 18:51:46 -0700 | [diff] [blame] | 541 | class TunnelTests(TestCase): |
| 542 | def test_connect(self): |
| 543 | response_text = ( |
| 544 | 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT |
| 545 | 'HTTP/1.1 200 OK\r\n' # Reply to HEAD |
| 546 | 'Content-Length: 42\r\n\r\n' |
| 547 | ) |
| 548 | |
| 549 | def create_connection(address, timeout=None, source_address=None): |
| 550 | return FakeSocket(response_text, host=address[0], port=address[1]) |
| 551 | |
| 552 | conn = httplib.HTTPConnection('proxy.com') |
| 553 | conn._create_connection = create_connection |
| 554 | |
| 555 | # Once connected, we should not be able to tunnel anymore |
| 556 | conn.connect() |
| 557 | self.assertRaises(RuntimeError, conn.set_tunnel, 'destination.com') |
| 558 | |
| 559 | # But if close the connection, we are good. |
| 560 | conn.close() |
| 561 | conn.set_tunnel('destination.com') |
| 562 | conn.request('HEAD', '/', '') |
| 563 | |
| 564 | self.assertEqual(conn.sock.host, 'proxy.com') |
| 565 | self.assertEqual(conn.sock.port, 80) |
| 566 | self.assertTrue('CONNECT destination.com' in conn.sock.data) |
| 567 | self.assertTrue('Host: destination.com' in conn.sock.data) |
| 568 | |
| 569 | self.assertTrue('Host: proxy.com' not in conn.sock.data) |
| 570 | |
| 571 | conn.close() |
| 572 | |
| 573 | conn.request('PUT', '/', '') |
| 574 | self.assertEqual(conn.sock.host, 'proxy.com') |
| 575 | self.assertEqual(conn.sock.port, 80) |
| 576 | self.assertTrue('CONNECT destination.com' in conn.sock.data) |
| 577 | self.assertTrue('Host: destination.com' in conn.sock.data) |
| 578 | |
| 579 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 580 | def test_main(verbose=None): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 581 | test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
Senthil Kumaran | 36f28f7 | 2014-05-16 18:51:46 -0700 | [diff] [blame] | 582 | HTTPSTimeoutTest, SourceAddressTest, TunnelTests) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 583 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 584 | if __name__ == '__main__': |
| 585 | test_main() |