Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 1 | import httplib |
| 2 | import StringIO |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +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 | |
| 7 | from test import test_support |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 8 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 9 | HOST = test_support.HOST |
| 10 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 11 | class FakeSocket: |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 12 | def __init__(self, text, fileclass=StringIO.StringIO): |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 13 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 14 | self.fileclass = fileclass |
Martin v. Löwis | 040a927 | 2006-11-12 10:32:47 +0000 | [diff] [blame] | 15 | self.data = '' |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 17 | def sendall(self, data): |
Martin v. Löwis | 040a927 | 2006-11-12 10:32:47 +0000 | [diff] [blame] | 18 | self.data += data |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 19 | |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 20 | def makefile(self, mode, bufsize=None): |
| 21 | if mode != 'r' and mode != 'rb': |
Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 22 | raise httplib.UnimplementedFileMode() |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 23 | return self.fileclass(self.text) |
| 24 | |
| 25 | class NoEOFStringIO(StringIO.StringIO): |
| 26 | """Like StringIO, but raises AssertionError on EOF. |
| 27 | |
| 28 | This is used below to test that httplib doesn't try to read |
| 29 | more from the underlying file than it should. |
| 30 | """ |
| 31 | def read(self, n=-1): |
| 32 | data = StringIO.StringIO.read(self, n) |
| 33 | if data == '': |
| 34 | raise AssertionError('caller tried to read past EOF') |
| 35 | return data |
| 36 | |
| 37 | def readline(self, length=None): |
| 38 | data = StringIO.StringIO.readline(self, length) |
| 39 | if data == '': |
| 40 | raise AssertionError('caller tried to read past EOF') |
| 41 | return data |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 43 | |
| 44 | class HeaderTests(TestCase): |
| 45 | def test_auto_headers(self): |
| 46 | # Some headers are added automatically, but should not be added by |
| 47 | # .request() if they are explicitly set. |
| 48 | |
| 49 | import httplib |
| 50 | |
| 51 | class HeaderCountingBuffer(list): |
| 52 | def __init__(self): |
| 53 | self.count = {} |
| 54 | def append(self, item): |
| 55 | kv = item.split(':') |
| 56 | if len(kv) > 1: |
| 57 | # item is a 'Key: Value' header string |
| 58 | lcKey = kv[0].lower() |
| 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 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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() |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 84 | self.assertEqual(resp.read(), 'Text') |
Facundo Batista | 7066590 | 2007-10-18 03:16:03 +0000 | [diff] [blame] | 85 | self.assertTrue(resp.isclosed()) |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 86 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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 | |
Facundo Batista | 7066590 | 2007-10-18 03:16:03 +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), 'Te') |
| 100 | self.assertFalse(resp.isclosed()) |
| 101 | self.assertEqual(resp.read(2), 'xt') |
| 102 | self.assertTrue(resp.isclosed()) |
| 103 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 107 | for hp in ("www.python.org:abc", "www.python.org:"): |
| 108 | self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp) |
| 109 | |
| 110 | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", 8000), |
| 111 | ("www.python.org:80", "www.python.org", 80), |
| 112 | ("www.python.org", "www.python.org", 80), |
| 113 | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)): |
Martin v. Löwis | 74a249e | 2004-09-14 21:45:36 +0000 | [diff] [blame] | 114 | http = httplib.HTTP(hp) |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 115 | c = http._conn |
| 116 | if h != c.host: self.fail("Host incorrectly parsed: %s != %s" % (h, c.host)) |
| 117 | if p != c.port: self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) |
Skip Montanaro | 10e6e0e | 2004-09-14 16:32:02 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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' |
| 122 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n' |
| 123 | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
| 124 | ' Path="/acme"\r\n' |
| 125 | '\r\n' |
| 126 | 'No body\r\n') |
| 127 | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
| 128 | ', ' |
| 129 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
| 130 | s = FakeSocket(text) |
| 131 | r = httplib.HTTPResponse(s) |
| 132 | r.begin() |
| 133 | cookies = r.getheader("Set-Cookie") |
| 134 | if cookies != hdr: |
| 135 | self.fail("multiple headers not combined properly") |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 136 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +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() |
| 147 | if resp.read() != "": |
| 148 | self.fail("Did not expect response from HEAD request") |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 149 | |
Martin v. Löwis | 040a927 | 2006-11-12 10:32:47 +0000 | [diff] [blame] | 150 | def test_send_file(self): |
| 151 | expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
| 152 | 'Accept-Encoding: identity\r\nContent-Length:' |
| 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) |
| 159 | self.assertTrue(sock.data.startswith(expected)) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 161 | def test_chunked(self): |
| 162 | chunked_start = ( |
| 163 | 'HTTP/1.1 200 OK\r\n' |
| 164 | 'Transfer-Encoding: chunked\r\n\r\n' |
| 165 | 'a\r\n' |
| 166 | 'hello worl\r\n' |
| 167 | '1\r\n' |
| 168 | 'd\r\n' |
| 169 | ) |
| 170 | sock = FakeSocket(chunked_start + '0\r\n') |
| 171 | resp = httplib.HTTPResponse(sock, method="GET") |
| 172 | resp.begin() |
| 173 | self.assertEquals(resp.read(), 'hello world') |
| 174 | resp.close() |
| 175 | |
| 176 | for x in ('', 'foo\r\n'): |
| 177 | sock = FakeSocket(chunked_start + x) |
| 178 | resp = httplib.HTTPResponse(sock, method="GET") |
| 179 | resp.begin() |
| 180 | try: |
| 181 | resp.read() |
| 182 | except httplib.IncompleteRead, i: |
| 183 | self.assertEquals(i.partial, 'hello world') |
| 184 | else: |
| 185 | self.fail('IncompleteRead expected') |
| 186 | finally: |
| 187 | resp.close() |
| 188 | |
Georg Brandl | 8c460d5 | 2008-02-24 00:14:24 +0000 | [diff] [blame] | 189 | def test_negative_content_length(self): |
| 190 | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n') |
| 191 | resp = httplib.HTTPResponse(sock, method="GET") |
| 192 | resp.begin() |
| 193 | self.assertEquals(resp.read(), 'Hello\r\n') |
| 194 | resp.close() |
| 195 | |
Georg Brandl | 2363503 | 2008-02-24 00:03:22 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 4cbd1e3 | 2006-02-17 22:01:08 +0000 | [diff] [blame] | 197 | class OfflineTest(TestCase): |
| 198 | def test_responses(self): |
| 199 | self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found") |
| 200 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 201 | class TimeoutTest(TestCase): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 202 | PORT = None |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 203 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 204 | def setUp(self): |
| 205 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Trent Nelson | 6c4a7c6 | 2008-04-09 00:34:53 +0000 | [diff] [blame] | 206 | TimeoutTest.PORT = test_support.bind_port(self.serv) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 207 | self.serv.listen(5) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 208 | |
| 209 | def tearDown(self): |
| 210 | self.serv.close() |
| 211 | self.serv = None |
| 212 | |
| 213 | def testTimeoutAttribute(self): |
| 214 | '''This will prove that the timeout gets through |
| 215 | HTTPConnection and into the socket. |
| 216 | ''' |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 217 | # default -- use global socket timeout |
| 218 | self.assert_(socket.getdefaulttimeout() is None) |
| 219 | socket.setdefaulttimeout(30) |
| 220 | try: |
| 221 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT) |
| 222 | httpConn.connect() |
| 223 | finally: |
| 224 | socket.setdefaulttimeout(None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 225 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 226 | httpConn.close() |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 227 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 228 | # no timeout -- do not use global socket default |
| 229 | self.assert_(socket.getdefaulttimeout() is None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 230 | socket.setdefaulttimeout(30) |
| 231 | try: |
Trent Nelson | 6c4a7c6 | 2008-04-09 00:34:53 +0000 | [diff] [blame] | 232 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, |
| 233 | timeout=None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 234 | httpConn.connect() |
| 235 | finally: |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 236 | socket.setdefaulttimeout(None) |
| 237 | self.assertEqual(httpConn.sock.gettimeout(), None) |
| 238 | httpConn.close() |
| 239 | |
| 240 | # a value |
| 241 | httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
| 242 | httpConn.connect() |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 243 | self.assertEqual(httpConn.sock.gettimeout(), 30) |
Facundo Batista | f196629 | 2007-03-25 03:20:05 +0000 | [diff] [blame] | 244 | httpConn.close() |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 245 | |
| 246 | |
Facundo Batista | 70f996b | 2007-05-21 17:32:32 +0000 | [diff] [blame] | 247 | class HTTPSTimeoutTest(TestCase): |
| 248 | # XXX Here should be tests for HTTPS, there isn't any right now! |
| 249 | |
| 250 | def test_attributes(self): |
| 251 | # simple test to check it's storing it |
Thomas Wouters | 628e3bb | 2007-08-30 22:35:31 +0000 | [diff] [blame] | 252 | if hasattr(httplib, 'HTTPSConnection'): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 253 | h = httplib.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
Thomas Wouters | 628e3bb | 2007-08-30 22:35:31 +0000 | [diff] [blame] | 254 | self.assertEqual(h.timeout, 30) |
Facundo Batista | 70f996b | 2007-05-21 17:32:32 +0000 | [diff] [blame] | 255 | |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 256 | def test_main(verbose=None): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 257 | test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
| 258 | HTTPSTimeoutTest) |
Jeremy Hylton | 2c17825 | 2004-08-07 16:28:14 +0000 | [diff] [blame] | 259 | |
Georg Brandl | 71a2089 | 2006-10-29 20:24:01 +0000 | [diff] [blame] | 260 | if __name__ == '__main__': |
| 261 | test_main() |