Skip to content

Commit 7df3aa3

Browse files
committed
Normalize headers in session_with_stored_responses
- Use example from https://requests-cache.readthedocs.io/en/stable/user_guide/matching.html#custom-header-normalization - Allow existing, recorded responses to be used with Python 3.14.
1 parent 704cbd3 commit 7df3aa3

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

sdmx/testing/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
if TYPE_CHECKING:
2828
import pytest
29+
from requests import PreparedRequest
2930

3031
log = logging.getLogger(__name__)
3132

@@ -323,7 +324,25 @@ def session_with_stored_responses(pytestconfig):
323324
3. is treated with :func:`.offline`, so that *only* stored responses can be
324325
returned.
325326
"""
326-
session = Session(backend="memory")
327+
from requests_cache import create_key
328+
329+
def _key_fn(request: "PreparedRequest", **kwargs) -> str:
330+
"""Match existing stored responses with different `Accept-Encoding` headers.
331+
332+
Stored responses in sdmx-test-data have "Accept-Encoding: gzip, deflate"; with
333+
Python 3.14, the prepared request has "gzip, deflate, zstd`. Simplify so the
334+
existing keys match.
335+
"""
336+
exp = "gzip, deflate"
337+
if exp in request.headers.get("Accept-Encoding", ""):
338+
# Don't modify the original request that's about to be sent
339+
request = request.copy()
340+
request.headers["Accept-Encoding"] = exp
341+
342+
# Use the default key function to do the rest of the work
343+
return create_key(request, **kwargs)
344+
345+
session = Session(backend="memory", key_fn=_key_fn)
327346

328347
data.add_responses(
329348
session,

0 commit comments

Comments
 (0)