Skip to content

Commit

Permalink
test that http.client _tunnel closes the response on exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Apr 25, 2023
1 parent 5500734 commit 86c29f5
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,12 +2383,33 @@ def test_tunnel_debuglog(self):

self.conn.set_debuglevel(1)
self.conn._create_connection = self._create_connection(response_text)
self.conn.set_tunnel("destination.com")
self.conn.request('HEAD', '/', '')


def test_tunnel_leak(self):
sock = None

def _create_connection(address, timeout=None, source_address=None):
nonlocal sock
sock = FakeSocket(
'HTTP/1.1 404 NOT FOUND\r\n\r\n',
host=address[0],
port=address[1],
)
return sock

self.conn._create_connection = _create_connection
self.conn.set_tunnel('destination.com')
exc = None
try:
self.conn.request('HEAD', '/', '')
except OSError as e:
# keeping a reference to exc keeps response alive in the traceback
exc = e
self.assertIsNotNone(exc)
self.assertTrue(sock.file_closed)

with support.captured_stdout() as output:
self.conn.request('PUT', '/', '')
lines = output.getvalue().splitlines()
self.assertIn('header: {}'.format(expected_header), lines)


if __name__ == '__main__':
Expand Down

0 comments on commit 86c29f5

Please sign in to comment.