This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
HTTP20Adapter.close tests (supersedes #307) #344
Merged
Lukasa
merged 4 commits into
python-hyper:development
from
KostyaEsmukov:pr/requests_close
Jul 31, 2017
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d292ab
Implement HTTP20Adapter.close to close connections
allanlei 92bac67
Make requests adapter close() tests integrational
KostyaEsmukov 8587eb3
Ensure that connections are actually closed on closing requests adapter
KostyaEsmukov 9a49106
Split an assertion in requests adapter close() tests
KostyaEsmukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.