3
3
from urllib3.connectionpool import connection_from_url, HTTPConnectionPool
4
from urllib3.util import get_host, make_headers
5
from urllib3.exceptions import EmptyPoolError, LocationParseError
4
from urllib3.packages.ssl_match_hostname import CertificateError
5
from urllib3.exceptions import (
14
from socket import timeout as SocketTimeout
15
from ssl import SSLError as BaseSSLError
18
from queue import Empty
19
from http.client import HTTPException
21
from Queue import Empty
22
from httplib import HTTPException
8
25
class TestConnectionPool(unittest.TestCase):
9
def test_get_host(self):
11
'http://google.com/mail': ('http', 'google.com', None),
12
'http://google.com/mail/': ('http', 'google.com', None),
13
'google.com/mail': ('http', 'google.com', None),
14
'http://google.com/': ('http', 'google.com', None),
15
'http://google.com': ('http', 'google.com', None),
16
'http://www.google.com': ('http', 'www.google.com', None),
17
'http://mail.google.com': ('http', 'mail.google.com', None),
18
'http://google.com:8000/mail/': ('http', 'google.com', 8000),
19
'http://google.com:8000': ('http', 'google.com', 8000),
20
'https://google.com': ('https', 'google.com', None),
21
'https://google.com:8000': ('https', 'google.com', 8000),
22
'http://user:password@127.0.0.1:1234': ('http', '127.0.0.1', 1234),
24
for url, expected_host in url_host_map.items():
25
returned_host = get_host(url)
26
self.assertEquals(returned_host, expected_host)
28
26
def test_same_host(self):
30
28
('http://google.com/', '/'),
50
48
c = connection_from_url(a)
51
49
self.assertFalse(c.is_same_host(b), "%s =? %s" % (a, b))
53
def test_invalid_host(self):
54
# TODO: Add more tests
56
'http://google.com:foo',
59
for location in invalid_host:
60
self.assertRaises(LocationParseError, get_host, location)
63
def test_make_headers(self):
65
make_headers(accept_encoding=True),
66
{'accept-encoding': 'gzip,deflate'})
69
make_headers(accept_encoding='foo,bar'),
70
{'accept-encoding': 'foo,bar'})
73
make_headers(accept_encoding=['foo', 'bar']),
74
{'accept-encoding': 'foo,bar'})
77
make_headers(accept_encoding=True, user_agent='banana'),
78
{'accept-encoding': 'gzip,deflate', 'user-agent': 'banana'})
81
make_headers(user_agent='banana'),
82
{'user-agent': 'banana'})
85
make_headers(keep_alive=True),
86
{'connection': 'keep-alive'})
89
make_headers(basic_auth='foo:bar'),
90
{'authorization': 'Basic Zm9vOmJhcg=='})
92
51
def test_max_connections(self):
93
52
pool = HTTPConnectionPool(host='localhost', maxsize=1, block=True)
127
86
str(EmptyPoolError(HTTPConnectionPool(host='localhost'), "Test.")),
128
87
"HTTPConnectionPool(host='localhost', port=None): Test.")
89
def test_pool_size(self):
91
pool = HTTPConnectionPool(host='localhost', maxsize=POOL_SIZE, block=True)
96
def _test(exception, expect):
97
pool._make_request = lambda *args, **kwargs: _raise(exception)
98
with self.assertRaises(expect):
99
pool.request('GET', '/')
101
self.assertEqual(pool.pool.qsize(), POOL_SIZE)
103
#make sure that all of the exceptions return the connection to the pool
104
_test(Empty, TimeoutError)
105
_test(SocketTimeout, TimeoutError)
106
_test(BaseSSLError, SSLError)
107
_test(CertificateError, SSLError)
109
# The pool should never be empty, and with these two exceptions being raised,
110
# a retry will be triggered, but that retry will fail, eventually raising
111
# MaxRetryError, not EmptyPoolError
112
# See: https://github.com/shazow/urllib3/issues/76
113
pool._make_request = lambda *args, **kwargs: _raise(HTTPException)
114
with self.assertRaises(MaxRetryError):
115
pool.request('GET', '/', retries=1, pool_timeout=0.01)
116
self.assertEqual(pool.pool.qsize(), POOL_SIZE)
118
def test_assert_same_host(self):
119
c = connection_from_url('http://google.com:80')
121
with self.assertRaises(HostChangedError):
122
c.request('GET', 'http://yahoo.com:80', assert_same_host=True)
124
def test_pool_close(self):
125
pool = connection_from_url('http://google.com:80')
127
# Populate with some connections
128
conn1 = pool._get_conn()
129
conn2 = pool._get_conn()
130
conn3 = pool._get_conn()
131
pool._put_conn(conn1)
132
pool._put_conn(conn2)
134
old_pool_queue = pool.pool
137
self.assertEqual(pool.pool, None)
139
with self.assertRaises(ClosedPoolError):
142
pool._put_conn(conn3)
144
with self.assertRaises(ClosedPoolError):
147
with self.assertRaises(Empty):
148
old_pool_queue.get(block=False)
131
151
if __name__ == '__main__':