Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

HTTP20Adapter.close tests (supersedes #307) #344

Merged
merged 4 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hyper/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ def getheaders(self, name):
orig.msg = FakeOriginalResponse(resp.headers.iter_raw())

return response

def close(self):
for connection in self.connections.values():
connection.close()
self.connections.clear()
95 changes: 95 additions & 0 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1717,3 +1717,98 @@ def socket_handler(listener):
timeout=(10, 0.5))

self.tear_down()

def test_adapter_close(self):
self.set_up(secure=False)

def socket_handler(listener):
sock = listener.accept()[0]

# We should get the initial request.
data = b''
while not data.endswith(b'\r\n\r\n'):
data += sock.recv(65535)

# We need to send back a response.
resp = (
b'HTTP/1.1 201 No Content\r\n'
b'Server: socket-level-server\r\n'
b'Content-Length: 0\r\n'
b'Connection: close\r\n'
b'\r\n'
)
sock.send(resp)
sock.close()

self._start_server(socket_handler)

a = HTTP20Adapter()
s = requests.Session()
s.mount('http://', a)
r = s.get('http://%s:%s' % (self.host, self.port))
connections_before_close = list(a.connections.values())
s.close()

# check that connections cache is empty
assert not a.connections

# check that all connections are actually closed
assert (connections_before_close and
all(conn._sock is None for conn in connections_before_close))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split the first assertion out and call it when we get the connections initially.


assert r.status_code == 201
assert len(r.headers) == 3
assert r.headers['server'] == 'socket-level-server'
assert r.headers['content-length'] == '0'
assert r.headers['connection'] == 'close'

assert r.content == b''

self.tear_down()

def test_adapter_close_context_manager(self):
self.set_up(secure=False)

def socket_handler(listener):
sock = listener.accept()[0]

# We should get the initial request.
data = b''
while not data.endswith(b'\r\n\r\n'):
data += sock.recv(65535)

# We need to send back a response.
resp = (
b'HTTP/1.1 201 No Content\r\n'
b'Server: socket-level-server\r\n'
b'Content-Length: 0\r\n'
b'Connection: close\r\n'
b'\r\n'
)
sock.send(resp)
sock.close()

self._start_server(socket_handler)

with requests.Session() as s:
a = HTTP20Adapter()
s.mount('http://', a)
r = s.get('http://%s:%s' % (self.host, self.port))
connections_before_close = list(a.connections.values())

# check that connections cache is empty
assert not a.connections

# check that all connections are actually closed
assert (connections_before_close and
all(conn._sock is None for conn in connections_before_close))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note here.


assert r.status_code == 201
assert len(r.headers) == 3
assert r.headers['server'] == 'socket-level-server'
assert r.headers['content-length'] == '0'
assert r.headers['connection'] == 'close'

assert r.content == b''

self.tear_down()