CannotOverwriteExistingCassetteException with no matchers failed
See original GitHub issueI’m trying to remove access tokens from the response body of some http transactions that are being recorded so that I can check the sanitized cassettes into source control so I’ve added a before_record_request
to the vcr.
import json
def scrub_access_token(response):
body = response.get("body", {}).get("string")
if body is None:
return response
try:
body = json.loads(body)
except Exception:
return response
if "access_token" not in body:
return response
body["access_token"] = "REDACTED"
new_body = json.dumps(body)
response["body"]["string"] = new_body
response["headers"]["Content-Length"] = len(new_body)
return response
@pytest.mark.vcr(before_record_response=scrub_access_token)
def test_foo:
...
It runs successfully the first run and correctly removes the field from the body, but then on subsequent runs, I get an error when requesting that request.
E vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing cassette ('/home/bmatase/cassettes/test_become_active.yaml') in your current record mode ('none').
E No match for the request (<Request (GET) http://169.254.169.254/metadata/identity/oauth2/token?resource=https%3A%2F%2Fmanagement.core.windows.net%2F&api-version=2018-02-01>) was found.
E Found 1 similar requests with 0 different matcher(s) :
E
E 1 - (<Request (GET) http://169.254.169.254/metadata/identity/oauth2/token?resource=https%3A%2F%2Fmanagement.core.windows.net%2F&api-version=2018-02-01>).
E Matchers succeeded : ['method', 'scheme', 'host', 'port', 'path', 'query']
E Matchers failed :
I don’t understand why the two requests aren’t matching if all of the matchers matched. It seems odd that the requests aren’t matching because I’m modifying the reponse. I originally thought that it was because of the content length not matching, but it still occurs after that addition.
I don’t think this is a duplicate of #516 since there aren’t two identical requests in the error message.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:22
- Comments:16
Top GitHub Comments
Indeed,
allow_playback_repeats=True
solves the issue. I guess we could just modify the error message to say that there is a matching request, but it has already been used and that this option should be provided to allow reusing it.Hey folks!
Found this issue after having spent half an hour trying to figure out the mysterious case with “no matchers failed”. Turns out that indeed there was an unwanted repeat-request in my code.
Having a more helpful failure message would have saved me a bit of head-scratching. Would it make sense to change the message that prints out the fact that the request was repeated and even suggests turning on
allow_playback_repeats
?