Skip to content

Commit 1efbd43

Browse files
Reject duplicate headers after key normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 100da37 commit 1efbd43

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

hyperbrowser/header_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def normalize_headers(
3030
or "\r" in value
3131
):
3232
raise HyperbrowserError("headers must not contain newline characters")
33+
if normalized_key in normalized_headers:
34+
raise HyperbrowserError(
35+
"duplicate header names are not allowed after normalization"
36+
)
3337
normalized_headers[normalized_key] = value
3438
return normalized_headers
3539

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ def test_client_config_normalizes_header_name_whitespace():
200200
assert config.headers == {"X-Correlation-Id": "value"}
201201

202202

203+
def test_client_config_rejects_duplicate_header_names_after_normalization():
204+
with pytest.raises(
205+
HyperbrowserError,
206+
match="duplicate header names are not allowed after normalization",
207+
):
208+
ClientConfig(
209+
api_key="test-key",
210+
headers={"X-Correlation-Id": "one", " X-Correlation-Id ": "two"},
211+
)
212+
213+
203214
def test_client_config_accepts_mapping_header_inputs():
204215
headers = MappingProxyType({"X-Correlation-Id": "abc123"})
205216
config = ClientConfig(api_key="test-key", headers=headers)

tests/test_header_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def test_normalize_headers_rejects_empty_header_name():
2121
)
2222

2323

24+
def test_normalize_headers_rejects_duplicate_names_after_normalization():
25+
with pytest.raises(
26+
HyperbrowserError,
27+
match="duplicate header names are not allowed after normalization",
28+
):
29+
normalize_headers(
30+
{"X-Trace-Id": "one", " X-Trace-Id ": "two"},
31+
mapping_error_message="headers must be a mapping of string pairs",
32+
)
33+
34+
2435
def test_parse_headers_env_json_ignores_blank_values():
2536
assert parse_headers_env_json(" ") is None
2637

0 commit comments

Comments
 (0)