Skip to content

Commit ea60733

Browse files
fpighinogates
authored andcommitted
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.
1 parent d4e3e28 commit ea60733

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

tests/conftest.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,27 @@ def pytest_recording_configure(config, vcr):
319319
from vcr import matchers
320320
from vcr.util import read_body
321321

322-
is_text_json = matchers._header_checker("text/json")
323-
transformer = matchers._transform_json
322+
def is_json(headers):
323+
return matchers._header_checker("text/json")(headers) or matchers._header_checker("application/json")(headers)
324+
325+
def normalize_utc_offset(value):
326+
# Cassettes record UTC datetimes as "...Z", but the client serializes
327+
# tz-aware datetimes via datetime.isoformat() as "...+00:00". The two
328+
# denote the same instant, so treat them as equal when matching bodies.
329+
if isinstance(value, dict):
330+
return {key: normalize_utc_offset(val) for key, val in value.items()}
331+
if isinstance(value, list):
332+
return [normalize_utc_offset(item) for item in value]
333+
if isinstance(value, str) and value.endswith("+00:00"):
334+
return value[:-6] + "Z"
335+
return value
336+
337+
def transform(request):
338+
return normalize_utc_offset(matchers._transform_json(read_body(request)))
324339

325340
def body(r1, r2):
326-
if is_text_json(r1.headers) and is_text_json(r2.headers):
327-
assert transformer(read_body(r1)) == transformer(read_body(r2))
341+
if is_json(r1.headers) and is_json(r2.headers):
342+
assert transform(r1) == transform(r2)
328343
else:
329344
matchers.body(r1, r2)
330345

0 commit comments

Comments
 (0)