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