Commit 14b9e0ad authored by Jérome Perrin's avatar Jérome Perrin

software/caddy-frontend/test: use resource.Session as context manager

this prevents ResourceWarning because this way the sockets are closed
parent 03c55402
...@@ -499,31 +499,32 @@ def fakeHTTPSResult(domain, path, port=HTTPS_PORT, ...@@ -499,31 +499,32 @@ def fakeHTTPSResult(domain, path, port=HTTPS_PORT,
headers.setdefault('Via', 'http/1.1 clientvia') headers.setdefault('Via', 'http/1.1 clientvia')
session = requests.Session() session = requests.Session()
if source_ip is not None: with session:
new_source = source.SourceAddressAdapter(source_ip) if source_ip is not None:
session.mount('http://', new_source) new_source = source.SourceAddressAdapter(source_ip)
session.mount('https://', new_source) session.mount('http://', new_source)
socket_getaddrinfo = socket.getaddrinfo session.mount('https://', new_source)
try: socket_getaddrinfo = socket.getaddrinfo
add_custom_dns(domain, port, TEST_IP) try:
socket.getaddrinfo = new_getaddrinfo add_custom_dns(domain, port, TEST_IP)
# Use a prepared request, to disable path normalization. socket.getaddrinfo = new_getaddrinfo
# We need this because some test checks requests with paths like # Use a prepared request, to disable path normalization.
# /test-path/deep/.././deeper but we don't want the client to send # We need this because some test checks requests with paths like
# /test-path/deeper # /test-path/deep/.././deeper but we don't want the client to send
# See also https://github.com/psf/requests/issues/5289 # /test-path/deeper
url = 'https://%s:%s/%s' % (domain, port, path) # See also https://github.com/psf/requests/issues/5289
req = requests.Request( url = 'https://%s:%s/%s' % (domain, port, path)
method='GET', req = requests.Request(
url=url, method='GET',
headers=headers, url=url,
cookies=cookies, headers=headers,
) cookies=cookies,
prepped = req.prepare() )
prepped.url = url prepped = req.prepare()
return session.send(prepped, verify=False, allow_redirects=False) prepped.url = url
finally: return session.send(prepped, verify=False, allow_redirects=False)
socket.getaddrinfo = socket_getaddrinfo finally:
socket.getaddrinfo = socket_getaddrinfo
def fakeHTTPResult(domain, path, port=HTTP_PORT, def fakeHTTPResult(domain, path, port=HTTP_PORT,
...@@ -541,17 +542,18 @@ def fakeHTTPResult(domain, path, port=HTTP_PORT, ...@@ -541,17 +542,18 @@ def fakeHTTPResult(domain, path, port=HTTP_PORT,
headers.setdefault('Via', 'http/1.1 clientvia') headers.setdefault('Via', 'http/1.1 clientvia')
headers['Host'] = '%s:%s' % (domain, port) headers['Host'] = '%s:%s' % (domain, port)
session = requests.Session() session = requests.Session()
if source_ip is not None: with session:
new_source = source.SourceAddressAdapter(source_ip) if source_ip is not None:
session.mount('http://', new_source) new_source = source.SourceAddressAdapter(source_ip)
session.mount('https://', new_source) session.mount('http://', new_source)
session.mount('https://', new_source)
# Use a prepared request, to disable path normalization.
url = 'http://%s:%s/%s' % (TEST_IP, port, path) # Use a prepared request, to disable path normalization.
req = requests.Request(method='GET', url=url, headers=headers) url = 'http://%s:%s/%s' % (TEST_IP, port, path)
prepped = req.prepare() req = requests.Request(method='GET', url=url, headers=headers)
prepped.url = url prepped = req.prepare()
return session.send(prepped, allow_redirects=False) prepped.url = url
return session.send(prepped, allow_redirects=False)
class TestHandler(BaseHTTPRequestHandler): class TestHandler(BaseHTTPRequestHandler):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment