getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.14k stars 354 forks source link

Feature Request - Flag to allow unmatched HTTP calls to pass through & record new responses #634

Closed MorAlterya closed 1 year ago

MorAlterya commented 1 year ago

Why do I want it:

We want to use Responses' as a caching layer and have it be a best-effort cache.
Our code makes hundreds of different requests to dozens of domains, and the requests per use-case change pretty frequently as we develop our product.

Responses works great, aside from the fact that we cannot maintain the request-response registry manually and must record the responses instead.
I want the developers to be able to use the caching as smooth as possible.

Our current usage is:

Bottom line:

Is there way to do this already and I'm missing something? If not - Is this something feasible? Should I open a PR for this?

Thanks in advance!

beliaev-maksim commented 1 year ago

@markstory what are your thoughts on this ?

for me that sounds dangerous and might cause some false positives. What is the purpose of the test if it will regenerate in case of not found matches. I think testing itself should be "a bit hard", meaning that users should go and have a check of tests if they modified code base.

markstory commented 1 year ago

for me that sounds dangerous and might cause some false positives. What is the purpose of the test if it will regenerate in case of not found matches.

I share this concern. Having tests automatically pass through unregistered requests and update a fixture file feels like a workflow that could be built on top of the primitives we already provide.

MorAlterya commented 1 year ago

The purpose of this feature request is for the Responses module to be able to act as a cache, If desired, and not just for strict testing. It is extremely powerful as-is, and I think of properly labeled and behind a feature flag this is very useful.

@markstory I'd love to be able to implement this myself if needed and you think it is unfit for the official package, could you give me a few pointers as to how to implement such a feature?

beliaev-maksim commented 1 year ago

@MorAlterya you can create your own wrapper and catch the exceptions. If exception is raised, you send a real request and cache the value in registry file

markstory commented 1 year ago

Building a pass-through cache is outside of the scope of what responses is trying to solve. It expands scope to include managing cache data at the key level and for all entries. The focus of responses has and will continue to be as a aid for testing against external APIs and webservices.