From e7fca3093cdea8bfc40f6467a21505d12efad5a2 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Fri, 9 Mar 2018 13:15:39 +0100 Subject: [PATCH 1/4] Version upgrade [ci skip]. --- mocket/__init__.py | 2 +- mocket/mocket.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mocket/__init__.py b/mocket/__init__.py index e8e9bb5a..bca67931 100644 --- a/mocket/__init__.py +++ b/mocket/__init__.py @@ -7,4 +7,4 @@ __all__ = (mocketize, Mocket, MocketEntry, Mocketizer) -__version__ = '2.2.1' +__version__ = '2.2.2' diff --git a/mocket/mocket.py b/mocket/mocket.py index 708c5dee..a57febd7 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -409,6 +409,7 @@ def disable(): ssl.SSLSocket = ssl.__dict__['SSLSocket'] = true_ssl_socket ssl.SSLContext = ssl.__dict__['SSLContext'] = true_ssl_context socket.inet_pton = socket.__dict__['inet_pton'] = true_inet_pton + Mocket.reset() @classmethod def get_namespace(cls): @@ -477,7 +478,6 @@ def __exit__(self, type, value, tb): if self.instance: self.check_and_call('mocketize_teardown') Mocket.disable() - Mocket.reset() def check_and_call(self, method): method = getattr(self.instance, method, None) From 21e5a0747afb7aa9cd74983bb13ef4ecddd13893 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Tue, 13 Mar 2018 21:57:29 +0100 Subject: [PATCH 2/4] Mocket is removing the last_request only if needed. --- mocket/mockhttp.py | 3 ++- mocket/plugins/pook_mock_engine.py | 8 ++++++-- tests/tests35/test_http_aiohttp.py | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index dd4eb3bd..05fae930 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -134,7 +134,8 @@ def can_handle(self, data): except ValueError: try: same_entry = self == Mocket._last_entry - if same_entry: + if same_entry and decode_from_bytes(data).endswith(CRLF): + print(data) Mocket.remove_last_request() return same_entry except AttributeError: diff --git a/mocket/plugins/pook_mock_engine.py b/mocket/plugins/pook_mock_engine.py index f4e47d65..b1059089 100644 --- a/mocket/plugins/pook_mock_engine.py +++ b/mocket/plugins/pook_mock_engine.py @@ -17,8 +17,12 @@ def can_handle(self, data): return can_handle @classmethod - def single_register(cls, method, uri, body='', status=200, headers=None): - entry = cls(uri, method, Response(body=body, status=status, headers=headers)) + def single_register(cls, method, uri, body='', status=200, headers=None, match_querystring=True): + entry = cls( + uri, method, Response( + body=body, status=status, headers=headers + ), match_querystring=match_querystring + ) Mocket.register(entry) return entry diff --git a/tests/tests35/test_http_aiohttp.py b/tests/tests35/test_http_aiohttp.py index 0edc3965..2fe15577 100644 --- a/tests/tests35/test_http_aiohttp.py +++ b/tests/tests35/test_http_aiohttp.py @@ -29,6 +29,7 @@ async def main(l): async with session.post(url, data=body * 6) as post_response: assert post_response.status == 201 assert await post_response.text() == body * 2 + assert Mocket.last_request().method == 'POST' assert Mocket.last_request().body == body * 6 loop = asyncio.get_event_loop() From 39eee0fc033c86e42601c227bfab8f51a69d6213 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Tue, 13 Mar 2018 22:11:25 +0100 Subject: [PATCH 3/4] Improving tests. --- .coveragerc | 2 ++ mocket/mockhttp.py | 1 - tests/tests35/test_http_aiohttp.py | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.coveragerc b/.coveragerc index a83e456b..56f844c4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -8,6 +8,8 @@ omit = exclude_lines = pragma: no cover def __repr__ + def __str__ + def __unicode__ raise NotImplementedError except AttributeError except ImportError diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index 05fae930..d85d5146 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -135,7 +135,6 @@ def can_handle(self, data): try: same_entry = self == Mocket._last_entry if same_entry and decode_from_bytes(data).endswith(CRLF): - print(data) Mocket.remove_last_request() return same_entry except AttributeError: diff --git a/tests/tests35/test_http_aiohttp.py b/tests/tests35/test_http_aiohttp.py index 2fe15577..a5b85154 100644 --- a/tests/tests35/test_http_aiohttp.py +++ b/tests/tests35/test_http_aiohttp.py @@ -35,6 +35,7 @@ async def main(l): loop = asyncio.get_event_loop() loop.set_debug(True) loop.run_until_complete(main(loop)) + self.assertEqual(len(Mocket._requests), 2) @mocketize def test_https_session(self): @@ -58,6 +59,7 @@ async def main(l): loop = asyncio.get_event_loop() loop.set_debug(True) loop.run_until_complete(main(loop)) + self.assertEqual(len(Mocket._requests), 2) @httprettified def test_httprettish_session(self): From 4089495f319fa15a82ed730994673aa11371d791 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Fri, 16 Mar 2018 15:33:45 +0100 Subject: [PATCH 4/4] New attempt. --- mocket/mockhttp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index d85d5146..e04d6c82 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -117,6 +117,9 @@ def __init__(self, uri, method, responses, match_querystring=True): def collect(self, data): self._sent_data += data + decoded_data = decode_from_bytes(data) + if not decoded_data.startswith(Entry.METHODS) and decoded_data.endswith(CRLF): + Mocket.remove_last_request() super(Entry, self).collect(self._sent_data) def can_handle(self, data): @@ -133,10 +136,7 @@ def can_handle(self, data): method, path, version = self._parse_requestline(requestline) except ValueError: try: - same_entry = self == Mocket._last_entry - if same_entry and decode_from_bytes(data).endswith(CRLF): - Mocket.remove_last_request() - return same_entry + return self == Mocket._last_entry except AttributeError: return False uri = urlsplit(path)