Skip to content

Commit

Permalink
Bump to 2.1.0 (#142)
Browse files Browse the repository at this point in the history
* Bump to 2.1.0

* Test all interceptors json request and response handling
  • Loading branch information
sarayourfriend authored Oct 8, 2024
1 parent 15829a2 commit 34ac39e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/pook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
__license__ = "MIT"

# Current version
__version__ = "2.0.1"
__version__ = "2.1.0"
19 changes: 4 additions & 15 deletions tests/unit/interceptors/aiohttp_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import aiohttp
import pytest

Expand All @@ -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):
Expand Down Expand Up @@ -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"}
Expand Down
54 changes: 50 additions & 4 deletions tests/unit/interceptors/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
from typing import Optional, Tuple
import json
from typing import Optional, Tuple, Union

import pytest

Expand All @@ -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`")

Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions tests/unit/interceptors/httpx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/interceptors/urllib3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
3 changes: 2 additions & 1 deletion tests/unit/interceptors/urllib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 34ac39e

Please sign in to comment.