-
Notifications
You must be signed in to change notification settings - Fork 44
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
Automatic snapshot of Requests #159
Comments
This is a nice idea and feature for RESPX. I think it would be quite simple to support without too much effort. We already have the side effects to play with. I'm thinking a side effect that first looks for an existing file and returns a response if found, otherwise returns the One way could be to enhance the pass through behaviour for side effects, e.g. keep allowing the Rough example... def persist_response(response):
...
def snapshot(request, route):
snapshot_file = ... # find existing file by name based on request hash (url, etc details) or hashed route pattern?
if response_file:
# load response details from file, e.g. status, headers, body etc
response = httpx.Response(status_code, headers=..., content=..., ...)
# make route always return this response, i.e. no need to load file for subsequent requests
route.mock(return_value=response)
return response
# Snapshot file does not exist, make request pass through
# and register a response callback to persist it for subsequent requests
return PassThrough(response_hook=persist_response)
respx.route(host="example.org").mock(side_effect=snapshot) Note that the Once we support the response callback hook mechanism we can either document this, or probably include this snapshot function as a built in side effect for optional use. More ideas welcome 😉 . |
Ping @StephenBrown2 😉 ...your old #16 are kind-of being re-visited...any thoughts? |
Thoughts @sidharthv96 ? |
Wow, this is Exactly what I was looking for!! |
Yes, I think that we should break this issue down into separate pull request, where the first one is responsible for the pass through callback hook enhancement. One alternative solution to the |
If we go with I'll see if I can come up with a rough draft. |
A note/fyi....when looking at how we mock and how to implement a pass through response hook, it will have to callback using the transport result, i.e. raw url, stream, etc... not httpx response objects. |
What is the status on this? It looks like work has been done on this, opening a draft pull-request would make it easier for people to pick up from where this was left. |
Agree, a draft would be good 😉 |
See you in the future @StephenBrown2 |
Hi, I have a use-case where I'm querying over 80 different endpoints.
Currently the outputs are verified using pytest-snapshot.
But the tests won't run fast as the HTTP calls have to be made.
What I'm trying to achieve is a system where if I add a new module and run the test, it's HTTP calls will be run once and written to a file. For future runs, that response will be provided by the mock, so we can skip the HTTP calls.
Is such an approach feasible with enough modifications in respx, or are there any architectural limitations which would stop me from adding such a feature?
The text was updated successfully, but these errors were encountered: