Skip to content

Conversation

@urkle
Copy link

@urkle urkle commented Aug 27, 2025

Description

This PR adds support for passing a function for the response_body/response_xml/response_json.

This allows producing dynamic responses for mock.

Fixes #164

PR Checklist

  • I've added tests for any code changes
  • I've documented any new features

urkle added 2 commits August 27, 2025 11:26
```python
def dyn_resp(req, resp):
   # Can make the response dynamic based on the request
   return {'key': 'value'}

pook.get('https://example.org/', persist=True, response_json=dyn_resp)
```
@urkle
Copy link
Author

urkle commented Sep 17, 2025

@h2non have you had a chance to look at this PR?

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! And apologies for the late review, I've been unplugged for a while.

With the tests, because this feature is implemented in each of the interceptors, it would be good to add it to the StandardTests base class. That'll make sure it's tested for all the interceptors. You can definitely keep the tests in mock_test if you like and you feel they aren't redundant to any added in StandardTests 🙂

I'll see about pushing a change later this weekend or next week unless you get around to it first.

Cheers 🙂

self: ``pook.Response`` current instance.
"""
if hasattr(body, "encode"):
if isfunction(body):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to prefer isfunction over callable here? isfunction returns false for objects with __call__.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, no. it was more so I wasn't aware/forgot about callable. (been a while since I did heavy python coding ).

but yeah callable makes better sense to allow for __call__ support.

return self

def fetch_body(self, request):
if isfunction(self._body) or hasattr(self._body, 'can_fetch_body'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this would be equivalent (if can_fetch_body could be excluded with this approach?)

Suggested change
if isfunction(self._body) or hasattr(self._body, 'can_fetch_body'):
if callable(self._body):

Copy link
Author

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.

return b"hello from pook"

mock = Mock(
url='https://example.com/fetch',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top here that we tend to use the url_404 fixture for URLs. That way, if for some reason the test configuration causes the interceptor not to work, then the status == 200 assertion will fail and we'll get a hint 🙂 Theoretically https://example.com/fetch shouldn't ever give us back a body of hello from pook, but still a good assurance 😅

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, meant to request changes!

@urkle
Copy link
Author

urkle commented Oct 10, 2025

I added a fixup commit with a bunch of changes and more encompassing tests (in the StandardTests). Which was good as it exposed some issues with the urllib implementation. And I also added a persist() test which exposed another bug where the mock was only usable one time (thus defeating the purpose of this while PR).

And I reworked the tests in the mock_tests to actually test that the correct _body was set / wrapped (For JSON).

Please look thorugh my commit and see if there is anything else you want tweaked. If it all looks good let me know and I can squash the commits down before you merge anything.

@urkle urkle requested a review from sarayourfriend October 13, 2025 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamnic response support

2 participants