Comments (12)
Wow, this is the first time I've seen httreplay. This is an interesting idea, I'm going to noodle on it a bit.
from vcrpy.
On the subject of capture/replay libs that you may or may not have seen, have you seen CaptureMock?
It's a little odd because it came out of this GUI testing tool called TextTest, but the really interesting thing is that it can mock things besides HTTP. You could conceivably mock database, memcache, etc.
from vcrpy.
@msabramo, that is really interesting, thanks for sending that.
@Aaron1011 Sorry it took so long but I think I like this idea. I'm still trying to think of a good implementation though. I noticed that httpreplay does this by letting you filter headers or query params. But what if there is sensitive data in the body as well?
The way Ruby VCR does this is by allowing you to define a block that returns a string that is filtered out wherever it appears, whether in headers, the body of the response, wherever. It's then replaced by a string that your supply.
This implementation seems more flexible but is a little more work to set up.
from vcrpy.
Also, in Ruby VCR:
When the interactions are replayed, the sensitive text will replace the substitution string so that the interaction will be identical to what was originally recorded.
from vcrpy.
Hmm... HTTP Basic auth base64 encodes the username and password, making simple string substitution fail in this case.
from vcrpy.
I wonder if the best solution would be to add callbacks that allows you to modify the request / response before they are saved or loaded.
from vcrpy.
Yeah callbacks are general and powerful and the you don't have to anticipate everyone's needs.
If it becomes clear that everyone is using the callbacks to do the same thing then you can specialize.
from vcrpy.
@kevin1024: If you want to see some examples of CaptureMock in action, check out https://github.com/msabramo/capturemock_examples
from vcrpy.
After thinking about this a bit, I think I would like to implement the 2 callbacks, but also provide a simpler method for the common case of stripping a header and stripping a query parameter. I think this is a good mix of flexibility and ease of use.
Example usage:
import vcr
my_vcr = vcr.VCR(
filter_headers = ['Authorization'],
filter_query_parameters=['api_key'],
)
with my_vcr.use_cassette('test.yml'):
# your http code here
Example usage of callback:
def before_record_cb(request, response, cassette):
if request.path != '/login':
return request, response
my_vcr = vcr.VCR(
before_record = before_record_cb,
)
with my_vcr.use_cassette('test.yml'):
# your http code here
The callback is called before the cassette is serialized to disk and takes 3 parameters: the recorded request, the recorded response, and the current cassette. It should return the modified request and response. If it returns None, it's not recorded at all.
What do you think?
from vcrpy.
Starting work on this in the filter branch
from vcrpy.
When is the correct time to filter out the information? Before adding it to the cassette, or before saving the cassette to disk?
from vcrpy.
When I filter out a querystring argument, requests like this:
http://www.someapi.com/?api_key=secret
get turned into this:
http://www.someapi.com/
and recorded in the cassette. But VCR will use the URL to match existing requests in a cassette, when you run your tests a second time, the cassette won't contain the request. In the default recording mode (once), this will raise an exception. This is definitely not useful behavior! The request is in the cassette, VCR just can't find it.
So, I think I'm going to apply the filters to the request before checking for matches in the cassette. This means that the callback will only get the request passed in, not the request and response, since when checking if a request exists in the cassette, the response is not always available.
from vcrpy.
Related Issues (20)
- autodoc fails for API documentation
- Integration tests fail with httpbin 0.10.0 HOT 3
- AIOHTTP not recording json body HOT 1
- How to run tests HOT 2
- [BUG] Doesn't work for URL's beginning with www.
- How is vcrpy importing from a parent directory in a test file HOT 1
- AttributeError: 'VCRHTTPResponse' object has no attribute 'json' HOT 2
- Add type annotations
- Backport patch to fix test failures with Python 3.12: AttributeError: type object 'VCRHTTPConnection[…]' has no attribute 'debuglevel' HOT 1
- [Duplicate of #719?] Upgrading to VCR.py that uses urllib3 v2+ causes string decoding problems with requests HOT 2
- 🛠️ Custom Persister HOT 1
- Release 6.0.0 to allow users to update to Python 3.12? HOT 20
- `TypeError: cannot pickle 'generator' object` with urllib 2.1.0, no error with urllib 1.26.18
- [Question] Would it make sense to support glob in `filter_headers`
- VCRHTTPResponse should have a `release_conn()` method
- record creation failed when using async HOT 2
- 'VCRHTTPConnection/...' has no attribute 'debuglevel'. Did you mean: 'set_debuglevel'
- Bug Report: Installation Failure of vcrpy>=5.0.0 in Conda Environment HOT 1
- Exception httpx.ResponseNotRead thrown randomly when using cassettes HOT 1
- Httpx Response content is not set/read, causing JSON parsing errors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vcrpy.