Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_total_timeout(self):
block_event = Event()
ready_event = self.start_basic_handler(block_send=block_event, num=2)
# This will get the socket to raise an EAGAIN on the read
timeout = Timeout(connect=3, read=SHORT_TIMEOUT)
pool = HTTPConnectionPool(self.host, self.port, timeout=timeout, retries=False)
self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
block_event.set()
ready_event.wait()
block_event.clear()
# The connect should succeed and this should hit the read timeout
timeout = Timeout(connect=3, read=5, total=SHORT_TIMEOUT)
pool = HTTPConnectionPool(self.host, self.port, timeout=timeout, retries=False)
self.assertRaises(ReadTimeoutError, pool.request, 'GET', '/')
def _test_body(self, data):
self.start_chunked_handler()
with HTTPConnectionPool(self.host, self.port, retries=False) as pool:
pool.urlopen("GET", "/", data, chunked=True)
header, body = self.buffer.split(b"\r\n\r\n", 1)
assert b"Transfer-Encoding: chunked" in header.split(b"\r\n")
if data:
bdata = data if isinstance(data, bytes) else data.encode("utf-8")
assert b"\r\n" + bdata + b"\r\n" in body
assert body.endswith(b"\r\n0\r\n\r\n")
len_str = body.split(b"\r\n", 1)[0]
stated_len = int(len_str, 16)
assert stated_len == len(bdata)
else:
assert body == b"0\r\n\r\n"
def test_dns_error(self):
pool = HTTPConnectionPool('thishostdoesnotexist.invalid', self.port, timeout=0.001)
self.assertRaises(MaxRetryError, pool.request, 'GET', '/test', retries=2)
def test_retries_put_filehandle(self):
"""HTTP PUT retry with a file-like object should not timeout"""
with HTTPConnectionPool(self.host, self.port, timeout=0.1) as pool:
retry = Retry(total=3, status_forcelist=[418])
# httplib reads in 8k chunks; use a larger content length
content_length = 65535
data = b"A" * content_length
uploaded_file = io.BytesIO(data)
headers = {
"test-name": "test_retries_put_filehandle",
"Content-Length": str(content_length),
}
resp = pool.urlopen(
"PUT",
"/successful_retry",
headers=headers,
retries=retry,
body=uploaded_file,
assert_same_host=False,
def test_for_double_release(self):
MAXSIZE=5
# Check default state
pool = HTTPConnectionPool(self.host, self.port, maxsize=MAXSIZE)
self.assertEqual(pool.num_connections, 0)
self.assertEqual(pool.pool.qsize(), MAXSIZE)
# Make an empty slot for testing
pool.pool.get()
self.assertEqual(pool.pool.qsize(), MAXSIZE-1)
# Check state after simple request
pool.urlopen('GET', '/')
self.assertEqual(pool.pool.qsize(), MAXSIZE-1)
# Check state without release
pool.urlopen('GET', '/', preload_content=False)
self.assertEqual(pool.pool.qsize(), MAXSIZE-2)
pool.urlopen('GET', '/')
def test_partial_response(self):
pool = HTTPConnectionPool(self.host, self.port, maxsize=1)
req_data = {'lol': 'cat'}
resp_data = urlencode(req_data).encode('utf-8')
r = pool.request('GET', '/echo', fields=req_data, preload_content=False)
self.assertEqual(r.read(5), resp_data[:5])
self.assertEqual(r.read(), resp_data[5:])
def test_doesnt_use_https_if_not_specified(self):
con = Urllib3HttpConnection()
self.assertIsInstance(con.pool, urllib3.HTTPConnectionPool)
def _test_broken_header_parsing(self, headers):
handler = create_response_handler((
b'HTTP/1.1 200 OK\r\n'
b'Content-Length: 0\r\n'
b'Content-type: text/plain\r\n'
) + b'\r\n'.join(headers) + b'\r\n'
)
self._start_server(handler)
pool = HTTPConnectionPool(self.host, self.port, retries=False)
with LogRecorder() as logs:
pool.request('GET', '/')
for record in logs:
if 'Failed to parse headers' in record.msg and \
pool._absolute_url('/') == record.args[0]:
return
self.fail('Missing log about unparsed headers')
def __init__(self, host, port, useragent=USERAGENT,
connection_class=urllib3.HTTPConnectionPool, **kwargs):
self.useragent = useragent
self._connection = connection_class(host, port, **kwargs)
def __init__(self, config):
url = config.get("geonames", "url")
username = config.get("geonames", "user")
fuzzy = config.get("geonames", "fuzzy")
self.post_data = {
'q': '',
'username': username,
'type': 'json',
'fuzzy': fuzzy,
'orderBy': 'relevance'
}
self.pool = urllib3.HTTPConnectionPool(host=url, maxsize=25, headers={'accept': 'application/json'})