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

No access to request query string, body, etc. on mock object #680

Closed
ThiefMaster opened this issue Oct 12, 2023 · 4 comments
Closed

No access to request query string, body, etc. on mock object #680

ThiefMaster opened this issue Oct 12, 2023 · 4 comments
Assignees
Labels

Comments

@ThiefMaster
Copy link

In [1]: with responses.RequestsMock() as rsps:
    ...:     req = rsps.add(
    ...:         responses.POST,
    ...:         'http://example.com/foobar',
    ...:         body='{"foo": "bar"}',
    ...:         status=200,
    ...:         content_type='application/json',
    ...:     )
    ...:     resp = requests.post('http://example.com/foobar', json={'hello': 'world'}, params={'query': 'string'})
    ...:     print('response body', resp.json())
    ...:     print('request body', req.body)
    ...:     print('request url', req.url)
    ...:     pprint(req.__dict__)

response body {'foo': 'bar'}
request body {"foo": "bar"}
request url http://example.com/foobar
{'auto_calculate_content_length': False,
 'body': '{"foo": "bar"}',
 'call_count': 1,
 'content_type': 'application/json',
 'headers': None,
 'match': (),
 'method': 'POST',
 'passthrough': False,
 'status': 200,
 'stream': None,
 'url': 'http://example.com/foobar'}

I would have expected to be able to access the query string and body of the original request via the mock object. This would allow more complex assertions in a more imperative way than the (pretty declarative) matcher approach (which is great for simple cases though).

Since a single mocked response can be called multiple times, maybe it'd be nice to have something like req.calls[...] = with request objects (similar to the requests.Request class) that contain the original data from the request that was intercepted by this library.

@beliaev-maksim
Copy link
Collaborator

I think this is conceptual mistake. rsps.add returns Response object and not request
although, you can use resp.request which will return you requests.models.PreparedRequest

@ThiefMaster
Copy link
Author

ThiefMaster commented Oct 12, 2023

Sure, but I do not have access to resp when I call a function from my application that makes an HTTP request I'm mocking.

So basically I'm looking for a way to access the resp.request through without having access to resp. A bit like stdlib Mock objects give you access to all calls made on its methods.

@beliaev-maksim
Copy link
Collaborator

then #664 should address your issue

@ThiefMaster
Copy link
Author

indeed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

2 participants