From a84b26c42cf3819dbea836c1713c95d131e2482b Mon Sep 17 00:00:00 2001 From: Francesco Pighi Date: Tue, 7 Jul 2026 11:14:04 +0200 Subject: [PATCH] tests: match request bodies with equivalent datetime timezone notations The Python client serializes tz-aware datetimes via datetime.isoformat(), producing a "+00:00" UTC offset, while recorded cassettes use "Z". Both denote the same instant, but the VCR replay body matcher compared them as differing strings, so replay-only scenarios that send a datetime in the request body (e.g. DORA ListDORADeployments) fail to match their cassette. The matcher also only recognized "text/json", so "application/json" bodies skipped JSON-aware comparison entirely and fell back to a raw byte compare. --- tests/conftest.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 5a67143b4b..3b2e477dc2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -319,12 +319,27 @@ def pytest_recording_configure(config, vcr): from vcr import matchers from vcr.util import read_body - is_text_json = matchers._header_checker("text/json") - transformer = matchers._transform_json + def is_json(headers): + return matchers._header_checker("text/json")(headers) or matchers._header_checker("application/json")(headers) + + def normalize_utc_offset(value): + # Cassettes record UTC datetimes as "...Z", but the client serializes + # tz-aware datetimes via datetime.isoformat() as "...+00:00". The two + # denote the same instant, so treat them as equal when matching bodies. + if isinstance(value, dict): + return {key: normalize_utc_offset(val) for key, val in value.items()} + if isinstance(value, list): + return [normalize_utc_offset(item) for item in value] + if isinstance(value, str) and value.endswith("+00:00"): + return value[:-6] + "Z" + return value + + def transform(request): + return normalize_utc_offset(matchers._transform_json(read_body(request))) def body(r1, r2): - if is_text_json(r1.headers) and is_text_json(r2.headers): - assert transformer(read_body(r1)) == transformer(read_body(r2)) + if is_json(r1.headers) and is_json(r2.headers): + assert transform(r1) == transform(r2) else: matchers.body(r1, r2)