Skip to content
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

Expose expired mocks that would otherwise match the request #129

Merged
merged 6 commits into from
Mar 29, 2024

Conversation

sarayourfriend
Copy link
Collaborator

@sarayourfriend sarayourfriend commented Mar 29, 2024

Closes #127 by @silverwind

Thanks for the idea! If you have time to check out this PR and see if it matches what you had in mind, please do. Here's an example from a test case to demonstrate how this looks in practice:

__________________________________________________________________________________________ test_times_integrated ___________________________________________________________________________________________

httpbin = <pytest_httpbin.serve.Server object at 0x73b353b25640>

    @pytest.mark.pook
    def test_times_integrated(httpbin):
        url = f"{httpbin.url}/status/404"
        pook.get(url).times(2).reply(200).body("hello from pook")
    
        res = urlopen(url)
        assert res.read() == "hello from pook"
    
        res = urlopen(url)
        assert res.read() == "hello from pook"
    
        # with pytest.raises(PookNoMatches, match="Mock matches request but is expired."):
>       urlopen(url)

tests/unit/mock_test.py:153: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:215: in urlopen
    return opener.open(url, data, timeout)
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:515: in open
    response = self._open(req, data)
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:532: in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:492: in _call_chain
    result = func(*args)
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:1373: in http_open
    return self.do_open(http.client.HTTPConnection, req)
../../.local/share/hatch/env/virtual/.pythons/3.12/python/lib/python3.12/urllib/request.py:1344: in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
src/pook/interceptors/http.py:112: in handler
    return self._on_request(
src/pook/interceptors/http.py:61: in _on_request
    mock = self.engine.match(req)
src/pook/engine.py:441: in match
    self.no_matches(msg)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <pook.engine.Engine object at 0x73b35303d7f0>
msg = 'pook error!\n\n=> Cannot match any mock for the following request:\n=================================================...04)\n  ]),\n  response=Response(\n    headers=HTTPHeaderDict({}),\n    status=200,\n    body=hello from pook\n  )\n)\n'

    def no_matches(self, msg):
>       raise PookNoMatches(msg)
E       pook.exceptions.PookNoMatches: pook error!
E       
E       => Cannot match any mock for the following request:
E       ==================================================
E       Method: GET
E       URL: http://127.0.0.1:40529/status/404
E       Headers: HTTPHeaderDict({'Host': '127.0.0.1:40529', 'User-Agent': 'Python-urllib/3.12', 'Connection': 'close'})
E       ==================================================
E       
E       => Detailed matching errors:
E       Mock matches request but is expired.
E       Mock(
E         matches=2,
E         times=0,
E         persist=False,
E         matchers=MatcherEngine([
E           MethodMatcher(GET),
E           URLMatcher(http://127.0.0.1:40529/status/404)
E         ]),
E         response=Response(
E           headers=HTTPHeaderDict({}),
E           status=200,
E           body=hello from pook
E         )
E       )

@sarayourfriend sarayourfriend merged commit a790994 into master Mar 29, 2024
8 checks passed
@sarayourfriend sarayourfriend deleted the add/try-find-matching-expended-mock branch March 29, 2024 09:23
@silverwind
Copy link

silverwind commented Mar 29, 2024

Thanks, this looks nice. I only wish that the error message would change as well but it's still better then before.

@sarayourfriend
Copy link
Collaborator Author

sarayourfriend commented Mar 29, 2024

I only wish that the error message would change as well but it's still better then before.

If you have a concrete suggestion for the specific change and are willing to open a PR, please do, it would be appreciated. Here's the code that writes the error message:

https://github.com/h2non/pook/blob/HEAD/src/pook/engine.py#L428-L441

You could, for example, inspect the list of errors and see if an expiration error is there, and write a different wrapper for the no-match message. If you have an idea for what the error should be instead, we can discuss here or in a PR you've opened, if you wish to do so.

@silverwind
Copy link

Let's keep it, I guess "Mock matches request but is expired." at the end it enough to notice and people might be parsing the error message so it could be breaking.

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.

Provide a more helpful error when times is exceeded on a mock
2 participants