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

Allow getting mocked requests #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

etandel
Copy link

@etandel etandel commented Sep 23, 2019

Sometimes it is useful to get the requests that were made in order to check for certain arguments such as header and payload. Currently, aioresponses lets us do this by indexing into aioresponses.requests with a (method, url). However, I find this a bit clunky to use, especially when the mocked url is a regex.

This PR includes a new way of getting the requests by using the RequestMatch object that is created when defining the mock. This is done by making the mocking methods return the created RequestMatch and by adding a new .matched_requests() that takes this match objects and uses it to search for matching requests on aioresponses.requests.

Example:

        loop = asyncio.get_event_loop()
        session = aiohttp.ClientSession()

        matcher = m.post('http://example.com', status=500)
        m.post('http://example.com', status=200)

        loop.run_until_complete(
            session.post('http://example.com', json={"a": 1})
        )
        loop.run_until_complete(
            session.post('http://example.com', json={"a": 2})
        )

        requests = aioresponses.matched_requests(matcher)
        assert requests[0].kwargs["json"] == {"a": 1}
        assert requests[1].kwargs["json"] == {"a": 2}

This is my first PR to this project, please let me know if there is any way it can be improved.

@etandel
Copy link
Author

etandel commented Oct 1, 2019

(will fix tests)

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.

1 participant