-
Notifications
You must be signed in to change notification settings - Fork 43
Feature/dynamic response #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,9 +1,26 @@ | ||||||
| import json | ||||||
| from inspect import isfunction | ||||||
|
|
||||||
| from .constants import TYPES | ||||||
| from .headers import HTTPHeaderDict | ||||||
| from .helpers import trigger_methods | ||||||
|
|
||||||
| class WrapJSON: | ||||||
| def __init__(self, data): | ||||||
| self.data = data | ||||||
| # Marker for easier detection in fetch_body | ||||||
| self.can_fetch_body = True | ||||||
|
|
||||||
| def __call__(self, request, response): | ||||||
| data = self.data(request, response) | ||||||
| return self.encode_json(data) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def encode_json(data): | ||||||
| if not isinstance(data, str) and not isinstance(data, bytes): | ||||||
| data = json.dumps(data, indent=4) | ||||||
| return data | ||||||
|
|
||||||
|
|
||||||
| class Response: | ||||||
| """ | ||||||
|
|
@@ -14,9 +31,9 @@ class Response: | |||||
| Arguments: | ||||||
| status (int): HTTP response status code. Defaults to ``200``. | ||||||
| headers (dict): HTTP response headers. | ||||||
| body (str|bytes): HTTP response body. | ||||||
| json (str|dict|list): HTTP response JSON body. | ||||||
| xml (str): HTTP response XML body. | ||||||
| body (str|bytes|function): HTTP response body. | ||||||
| json (str|bytes|dict|list|function): HTTP response JSON body. | ||||||
| xml (str|function): HTTP response XML body. | ||||||
| type (str): HTTP response content MIME type. | ||||||
| file (str): file path to HTTP body response. | ||||||
| """ | ||||||
|
|
@@ -165,7 +182,9 @@ def body(self, body, *, chunked=False): | |||||
| Returns: | ||||||
| self: ``pook.Response`` current instance. | ||||||
| """ | ||||||
| if hasattr(body, "encode"): | ||||||
| if isfunction(body): | ||||||
|
||||||
| pass | ||||||
urkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| elif hasattr(body, "encode"): | ||||||
| body = body.encode("utf-8", "backslashreplace") | ||||||
| elif isinstance(body, list): | ||||||
| for i, chunk in enumerate(body): | ||||||
|
|
@@ -178,6 +197,11 @@ def body(self, body, *, chunked=False): | |||||
| self.header("Transfer-Encoding", "chunked") | ||||||
| return self | ||||||
|
|
||||||
| def fetch_body(self, request): | ||||||
urkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if isfunction(self._body) or hasattr(self._body, 'can_fetch_body'): | ||||||
|
||||||
| if isfunction(self._body) or hasattr(self._body, 'can_fetch_body'): | |
| if callable(self._body): |
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.
I'll do some tests. The main issue I was having was that I couldn't easily "detect" if the object was the right type, hence the can_fetch_body marker.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,37 @@ def test_mock_constructor(param_kwargs, query_string, url_404): | |
| assert res.status == 200 | ||
| assert json.loads(res.read()) == {"hello": "from pook"} | ||
|
|
||
| def test_dynamic_mock_response_body(): | ||
| def resp_builder(req, resp): | ||
| return b"hello from pook" | ||
|
|
||
| mock = Mock( | ||
| url='https://example.com/fetch', | ||
|
||
| reply_status=200, | ||
| response_body=resp_builder, | ||
| ) | ||
|
|
||
| with pook.use(): | ||
| pook.engine().add_mock(mock) | ||
| res = urlopen('https://example.com/fetch') | ||
| assert res.status == 200 | ||
| assert res.read() == b"hello from pook" | ||
|
|
||
| def test_dynamic_mock_response_json(): | ||
| def resp_builder(req, resp): | ||
| return {"hello": "from pook"} | ||
urkle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| mock = Mock( | ||
| url='https://example.com/fetch', | ||
| reply_status=200, | ||
| response_json=resp_builder, | ||
| ) | ||
|
|
||
| with pook.use(): | ||
| pook.engine().add_mock(mock) | ||
| res = urlopen('https://example.com/fetch') | ||
| assert res.status == 200 | ||
| assert json.loads(res.read()) == {"hello": "from pook"} | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "params, req_params, expected", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.