Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/apify_client/_http_clients/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ def _prepare_request_call(
if json is not None and data is not None:
raise ValueError('Cannot pass both "json" and "data" parameters at the same time!')

if not headers:
headers = {}
headers = dict(headers) if headers else {}

# Dump JSON data to string so it can be gzipped.
if json is not None:
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_http_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ def test_prepare_request_call_with_params() -> None:
assert params == {'limit': 10, 'flag': 'true'}


def test_prepare_request_call_does_not_mutate_caller_headers() -> None:
"""Test _prepare_request_call does not mutate the caller's headers dict.

A caller that reuses a shared headers dict across calls must not see stale
`Content-Type`/`Content-Encoding` headers leak in from a prior JSON/body call.
"""
client = _ConcreteHttpClient()

caller_headers = {'x-trace-id': 'abc-123'}
original = dict(caller_headers)

client._prepare_request_call(headers=caller_headers, json={'x': 1})
assert caller_headers == original

client._prepare_request_call(headers=caller_headers, data='payload')
assert caller_headers == original


def test_build_url_with_params_none() -> None:
"""Test _build_url_with_params with None params."""
client = _ConcreteHttpClient()
Expand Down