From 34ac39eda10ee3ca45630a9addc5c45f67ed927d Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Mon, 7 Oct 2024 23:33:36 -0600 Subject: [PATCH] Bump to 2.1.0 (#142) * Bump to 2.1.0 * Test all interceptors json request and response handling --- src/pook/__init__.py | 2 +- tests/unit/interceptors/aiohttp_test.py | 19 ++------- tests/unit/interceptors/base.py | 54 +++++++++++++++++++++++-- tests/unit/interceptors/httpx_test.py | 8 ++-- tests/unit/interceptors/urllib3_test.py | 4 +- tests/unit/interceptors/urllib_test.py | 3 +- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/pook/__init__.py b/src/pook/__init__.py index 4c35803..4e0a417 100644 --- a/src/pook/__init__.py +++ b/src/pook/__init__.py @@ -9,4 +9,4 @@ __license__ = "MIT" # Current version -__version__ = "2.0.1" +__version__ = "2.1.0" diff --git a/tests/unit/interceptors/aiohttp_test.py b/tests/unit/interceptors/aiohttp_test.py index 5746ac7..eee547a 100644 --- a/tests/unit/interceptors/aiohttp_test.py +++ b/tests/unit/interceptors/aiohttp_test.py @@ -1,5 +1,3 @@ -import json - import aiohttp import pytest @@ -13,11 +11,11 @@ class TestStandardAiohttp(StandardTests): is_async = True - async def amake_request(self, method, url): + async def amake_request(self, method, url, content=None): async with aiohttp.ClientSession(loop=self.loop) as session: - req = await session.request(method=method, url=url) - content = await req.read() - return req.status, content + req = await session.request(method=method, url=url, data=content) + response_content = await req.read() + return req.status, response_content def _pook_url(URL): @@ -57,15 +55,6 @@ async def test_binary_body(URL): assert await req.read() == BINARY_FILE -@pytest.mark.asyncio -async def test_json_matcher_data_payload(URL): - payload = {"foo": "bar"} - pook.post(URL).json(payload).reply(200).body(BINARY_FILE) - async with aiohttp.ClientSession() as session: - req = await session.post(URL, data=json.dumps(payload)) - assert await req.read() == BINARY_FILE - - @pytest.mark.asyncio async def test_json_matcher_json_payload(URL): payload = {"foo": "bar"} diff --git a/tests/unit/interceptors/base.py b/tests/unit/interceptors/base.py index 3d0cfb4..2a6040b 100644 --- a/tests/unit/interceptors/base.py +++ b/tests/unit/interceptors/base.py @@ -1,5 +1,6 @@ import asyncio -from typing import Optional, Tuple +import json +from typing import Optional, Tuple, Union import pytest @@ -8,15 +9,22 @@ class StandardTests: is_async: bool = False + loop: asyncio.AbstractEventLoop - async def amake_request(self, method: str, url: str) -> Tuple[int, Optional[bytes]]: + async def amake_request( + self, method: str, url: str, content: Union[bytes, None] = None + ) -> Tuple[int, Optional[bytes]]: raise NotImplementedError( "Sub-classes for async transports must implement `amake_request`" ) - def make_request(self, method: str, url: str) -> Tuple[int, Optional[bytes]]: + def make_request( + self, method: str, url: str, content: Union[bytes, None] = None + ) -> Tuple[int, Optional[bytes]]: if self.is_async: - return self.loop.run_until_complete(self.amake_request(method, url)) + return self.loop.run_until_complete( + self.amake_request(method, url, content) + ) raise NotImplementedError("Sub-classes must implement `make_request`") @@ -56,3 +64,41 @@ def test_network_mode(self, httpbin): status, body = self.make_request("POST", upstream_url) assert status == 500 + + @pytest.mark.pook + def test_json_request(self, httpbin): + url = f"{httpbin.url}/status/404" + json_request = {"hello": "json-request"} + pook.get(url).json(json_request).reply(200).body("hello from pook") + + status, body = self.make_request("GET", url, json.dumps(json_request).encode()) + + assert status == 200 + assert body == b"hello from pook" + + @pytest.mark.pook + def test_json_response(self, httpbin): + url = f"{httpbin.url}/status/404" + json_response = {"hello": "json-request"} + pook.get(url).reply(200).json(json_response) + + status, body = self.make_request("GET", url) + + assert status == 200 + assert body + assert json.loads(body) == json_response + + @pytest.mark.pook + def test_json_request_and_response(self, httpbin): + url = f"{httpbin.url}/status/404" + json_request = {"id": "123abc"} + json_response = {"title": "123abc title"} + pook.get(url).json(json_request).reply(200).json(json_response) + + status, body = self.make_request( + "GET", url, content=json.dumps(json_request).encode() + ) + + assert status == 200 + assert body + assert json.loads(body) == json_response diff --git a/tests/unit/interceptors/httpx_test.py b/tests/unit/interceptors/httpx_test.py index 2df6c0f..2223ef4 100644 --- a/tests/unit/interceptors/httpx_test.py +++ b/tests/unit/interceptors/httpx_test.py @@ -12,16 +12,16 @@ class TestStandardAsyncHttpx(StandardTests): is_async = True - async def amake_request(self, method, url): + async def amake_request(self, method, url, content=None): async with httpx.AsyncClient() as client: - response = await client.request(method=method, url=url) + response = await client.request(method=method, url=url, content=content) content = await response.aread() return response.status_code, content class TestStandardSyncHttpx(StandardTests): - def make_request(self, method, url): - response = httpx.request(method=method, url=url) + def make_request(self, method, url, content=None): + response = httpx.request(method=method, url=url, content=content) content = response.read() return response.status_code, content diff --git a/tests/unit/interceptors/urllib3_test.py b/tests/unit/interceptors/urllib3_test.py index 4e0abbd..d267748 100644 --- a/tests/unit/interceptors/urllib3_test.py +++ b/tests/unit/interceptors/urllib3_test.py @@ -7,9 +7,9 @@ class TestStandardUrllib3(StandardTests): - def make_request(self, method, url): + def make_request(self, method, url, content=None): http = urllib3.PoolManager() - response = http.request(method, url) + response = http.request(method, url, content) return response.status, response.read() diff --git a/tests/unit/interceptors/urllib_test.py b/tests/unit/interceptors/urllib_test.py index 5e86383..c110ecc 100644 --- a/tests/unit/interceptors/urllib_test.py +++ b/tests/unit/interceptors/urllib_test.py @@ -8,10 +8,11 @@ class TestUrllib(StandardTests): - def make_request(self, method, url): + def make_request(self, method, url, content=None): request = Request( url=url, method=method, + data=content, ) try: response = urlopen(request)