From ed08296445e1bccba51f3c4bad098ba8e98da216 Mon Sep 17 00:00:00 2001 From: Allan Lei Date: Sat, 11 Feb 2017 14:31:24 +0800 Subject: [PATCH] Implement HTTP20Adapter.close to close connections --- hyper/contrib.py | 4 ++++ test/test_hyper.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/hyper/contrib.py b/hyper/contrib.py index dccec518..958addb9 100644 --- a/hyper/contrib.py +++ b/hyper/contrib.py @@ -157,3 +157,7 @@ def getheaders(self, name): orig.msg = FakeOriginalResponse(resp.headers.iter_raw()) return response + + def close(self): + for connection in self.connections.values(): + connection._conn.close() diff --git a/test/test_hyper.py b/test/test_hyper.py index 6a18d592..0e7cd60b 100644 --- a/test/test_hyper.py +++ b/test/test_hyper.py @@ -26,6 +26,7 @@ import socket import zlib from io import BytesIO +import requests TEST_DIR = os.path.abspath(os.path.dirname(__file__)) TEST_CERTS_DIR = os.path.join(TEST_DIR, 'certs') @@ -1128,6 +1129,25 @@ def test_adapter_accept_client_certificate(self): cert=CLIENT_PEM_FILE) assert conn1 is conn2 + def test_adapter_close(self): + """ + This tests HTTP20Adapter properly closes connections + """ + s = requests.Session() + a = HTTP20Adapter() + s.mount('https://http2bin.org', a) + s.get('https://http2bin.org/') + s.close() + + def test_adapter_close_context_manager(self): + """ + This tests HTTP20Adapter properly closes connections via context manager + """ + with requests.Session() as s: + a = HTTP20Adapter() + s.mount('https://http2bin.org', a) + s.get('https://http2bin.org/') + class TestUtilities(object): def test_combining_repeated_headers(self):