kevin1024 / vcrpy

Automatically mock your HTTP interactions to simplify and speed up testing
MIT License
2.72k stars 389 forks source link

using `before_record_response` results in no cassette being recorded #863

Closed axiomofjoy closed 3 months ago

axiomofjoy commented 3 months ago
import vcr
from httpx import Client

@vcr.use_cassette(before_record_response=lambda r: print(r["headers"]))
def test_cassette_generation() -> None:
    client = Client()
    client.get("https://www.example.com")

if __name__ == "__main__":
    test_cassette_generation()

I am trying to record a cassette and remove some sensitive response headers. When I use the before_record_response parameter, no cassette is recorded.

Sorry if this is simple user error. I have not found the answer to the question anywhere.

axiomofjoy commented 3 months ago

The argument passed to before_record_response needs to return a response, otherwise, no cassette will be recorded (not even for the request). So instead of

def callback(response) -> None:
    response["headers"].pop("X-Cache", None)

I needed to do

def callback(response) -> None:
    response["headers"].pop("X-Cache", None)
    return response