[docs]classCustomSerializer:"""Custom serializer for VCR cassettes using YAML and gzip. We're using a custom serializer to avoid the default yaml serializer used by VCR, which is not designed to be safe for untrusted input. This step is an extra precaution necessary because the cassette files are in compressed YAML format, which makes it more difficult to inspect their contents during development or debugging. """
[docs]@staticmethoddefserialize(cassette_dict:dict)->bytes:"""Convert cassette to YAML and compress it."""cassette_dict["requests"]=[request._to_dict()forrequestincassette_dict["requests"]]yml=yaml.safe_dump(cassette_dict)returngzip.compress(yml.encode("utf-8"))
[docs]@staticmethoddefdeserialize(data:bytes)->dict:"""Decompress data and convert it from YAML."""text=gzip.decompress(data).decode("utf-8")cassette=yaml.safe_load(text)cassette["requests"]=[Request._from_dict(request)forrequestincassette["requests"]]returncassette
[docs]classCustomPersister:"""A custom persister for VCR that uses the CustomSerializer."""
[docs]@classmethoddefload_cassette(cls,cassette_path:Union[str,PathLike[str]],serializer:CustomSerializer)->tuple[dict,dict]:"""Load a cassette from a file."""# If cassette path is already Path this is a no-opcassette_path=Path(cassette_path)ifnotcassette_path.is_file():msg=f"Cassette file {cassette_path} does not exist."raiseCassetteNotFoundError(msg)withcassette_path.open(mode="rb")asf:data=f.read()deser=serializer.deserialize(data)returndeser["requests"],deser["responses"]
[docs]@staticmethoddefsave_cassette(cassette_path:Union[str,PathLike[str]],cassette_dict:dict,serializer:CustomSerializer,)->None:"""Save a cassette to a file."""data=serializer.serialize(cassette_dict)# if cassette path is already Path this is no operationcassette_path=Path(cassette_path)cassette_folder=cassette_path.parentifnotcassette_folder.exists():cassette_folder.mkdir(parents=True)withcassette_path.open("wb")asf:f.write(data)
# A list of headers that should be filtered out of the cassettes.# These are typically associated with sensitive information and should# not be stored in cassettes._BASE_FILTER_HEADERS=[("authorization","PLACEHOLDER"),("x-api-key","PLACEHOLDER"),("api-key","PLACEHOLDER"),]@pytest.fixture(scope="session")def_base_vcr_config()->dict:"""Configuration that every cassette will receive. (Anything permitted by vcr.VCR(**kwargs) can be put here.) """return{"record_mode":"once","filter_headers":_BASE_FILTER_HEADERS.copy(),"match_on":["method","uri","body"],"allow_playback_repeats":True,"decode_compressed_response":True,"cassette_library_dir":"tests/cassettes","path_transformer":VCR.ensure_suffix(".yaml"),}@pytest.fixture(scope="session")defvcr_config(_base_vcr_config:dict)->dict:return_base_vcr_config