question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CannotOverwriteExistingCassetteException with no matchers failed

See original GitHub issue

I’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:open
  • Created 3 years ago
  • Reactions:22
  • Comments:16

github_iconTop GitHub Comments

4reactions
Alexander-Serovcommented, Nov 30, 2021

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.

2reactions
salomvarycommented, Oct 6, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debugging — vcrpy 4.2.1 documentation
This can help you with debugging custom matchers. CannotOverwriteExistingCassetteException¶. When a request failed to be found in an existing cassette, VCR.py ...
Read more >
kevin1024/vcrpy - Gitter
Error : vcr.errors.CannotOverwriteExistingCassetteException: No match for the request (<Request (GET) http://webservices.coala.io/list/bears>) was found. Can't ...
Read more >
python - mocked service call is not being used - Stack Overflow
However when executing the test it fails with the error. E vcr.errors.CannotOverwriteExistingCassetteException: Can't overwrite existing ...
Read more >
T196315 tests.api_tests.TestCachedRequest failure (vcr = True)
CannotOverwriteExistingCassetteException : No match for the request (<Request (POST) https://en.wikipedia.org/w/api.php>) was found. Can't overwrite existing ...
Read more >
Use a cassette to record HTTP requests — use_cassette • vcr
List of request matchers to use to determine what recorded HTTP interaction ... line numbers for failed tests and one way to not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found