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