Skip to content

Commit e016ecb

Browse files
Enforce concrete header name key types
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent e20ac43 commit e016ecb

File tree

2 files changed

+4
-81
lines changed

2 files changed

+4
-81
lines changed

hyperbrowser/header_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def normalize_headers(
4949
for key, value in _read_header_items(
5050
headers, mapping_error_message=mapping_error_message
5151
):
52-
if not isinstance(key, str) or not isinstance(value, str):
52+
if type(key) is not str or not isinstance(value, str):
5353
raise HyperbrowserError(effective_pair_error_message)
5454
try:
5555
normalized_key = key.strip()

tests/test_header_utils.py

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,6 @@ def items(self):
3636
return [self._broken_item]
3737

3838

39-
class _BrokenStripHeaderName(str):
40-
def strip(self, chars=None): # type: ignore[override]
41-
_ = chars
42-
raise RuntimeError("header strip exploded")
43-
44-
45-
class _BrokenLowerHeaderName(str):
46-
def strip(self, chars=None): # type: ignore[override]
47-
_ = chars
48-
return self
49-
50-
def lower(self): # type: ignore[override]
51-
raise RuntimeError("header lower exploded")
52-
53-
54-
class _NonStringLowerHeaderName(str):
55-
def strip(self, chars=None): # type: ignore[override]
56-
_ = chars
57-
return self
58-
59-
def lower(self): # type: ignore[override]
60-
return object()
61-
62-
6339
class _StringSubclassStripResultHeaderName(str):
6440
class _NormalizedKey(str):
6541
pass
@@ -118,68 +94,15 @@ def test_normalize_headers_rejects_empty_header_name():
11894
)
11995

12096

121-
def test_normalize_headers_wraps_header_name_strip_failures():
97+
def test_normalize_headers_rejects_string_subclass_header_names():
12298
with pytest.raises(
123-
HyperbrowserError, match="Failed to normalize header name"
124-
) as exc_info:
125-
normalize_headers(
126-
{_BrokenStripHeaderName("X-Trace-Id"): "trace-1"},
127-
mapping_error_message="headers must be a mapping of string pairs",
128-
)
129-
130-
assert exc_info.value.original_error is not None
131-
132-
133-
def test_normalize_headers_preserves_hyperbrowser_header_name_strip_failures():
134-
class _BrokenStripHeaderName(str):
135-
def strip(self, chars=None): # type: ignore[override]
136-
_ = chars
137-
raise HyperbrowserError("custom strip failure")
138-
139-
with pytest.raises(HyperbrowserError, match="custom strip failure") as exc_info:
140-
normalize_headers(
141-
{_BrokenStripHeaderName("X-Trace-Id"): "trace-1"},
142-
mapping_error_message="headers must be a mapping of string pairs",
143-
)
144-
145-
assert exc_info.value.original_error is None
146-
147-
148-
def test_normalize_headers_wraps_header_name_lower_failures():
149-
with pytest.raises(
150-
HyperbrowserError, match="Failed to normalize header name"
151-
) as exc_info:
152-
normalize_headers(
153-
{_BrokenLowerHeaderName("X-Trace-Id"): "trace-1"},
154-
mapping_error_message="headers must be a mapping of string pairs",
155-
)
156-
157-
assert exc_info.value.original_error is not None
158-
159-
160-
def test_normalize_headers_wraps_non_string_header_name_lower_results():
161-
with pytest.raises(
162-
HyperbrowserError, match="Failed to normalize header name"
163-
) as exc_info:
164-
normalize_headers(
165-
{_NonStringLowerHeaderName("X-Trace-Id"): "trace-1"},
166-
mapping_error_message="headers must be a mapping of string pairs",
167-
)
168-
169-
assert exc_info.value.original_error is not None
170-
171-
172-
def test_normalize_headers_wraps_string_subclass_header_name_strip_results():
173-
with pytest.raises(
174-
HyperbrowserError, match="Failed to normalize header name"
175-
) as exc_info:
99+
HyperbrowserError, match="headers must be a mapping of string pairs"
100+
):
176101
normalize_headers(
177102
{_StringSubclassStripResultHeaderName("X-Trace-Id"): "trace-1"},
178103
mapping_error_message="headers must be a mapping of string pairs",
179104
)
180105

181-
assert isinstance(exc_info.value.original_error, TypeError)
182-
183106

184107
def test_normalize_headers_rejects_overly_long_header_names():
185108
long_header_name = "X-" + ("a" * 255)

0 commit comments

Comments
 (0)