Skip to content

Allow getting mocked requests#135

Open
etandel wants to merge 3 commits intopnuckowski:masterfrom
etandel:matched-requests
Open

Allow getting mocked requests#135
etandel wants to merge 3 commits intopnuckowski:masterfrom
etandel:matched-requests

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)

@nils-van-zuijlen
Copy link

nils-van-zuijlen commented Feb 18, 2026

Hello, I would also like to have something similar to help with checks on the request parameters, responses-style.

The first part of this PR adresses the use-case, but I would like to still be able to differenciate between matchers. The behaviour I would like to have is as follows:

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

matcher1 = m.post('http://example.com', status=500)
matcher2 = 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})
)

assert matcher1.requests[0].kwargs["json"] == {"a": 1}
assert len(matcher1.requests) == 1

assert matcher2.requests[1].kwargs["json"] == {"a": 2}
assert len(matcher2.requests) == 1

I believe this would be achievable by adding a requests field to the matcher, and appending to it at the same place that the aioresponses.requests dict is appended to.

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.

2 participants