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