Skip to content

Commit 68fa983

Browse files
Use shared plain-string helper in header normalization
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent e268d94 commit 68fa983

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

hyperbrowser/header_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Dict, Mapping, Optional, cast
44

55
from .exceptions import HyperbrowserError
6+
from .type_utils import is_plain_string
67

78
_INVALID_HEADER_NAME_CHARACTER_PATTERN = re.compile(r"[^!#$%&'*+\-.^_`|~0-9A-Za-z]")
89
_MAX_HEADER_NAME_LENGTH = 256
@@ -49,11 +50,11 @@ def normalize_headers(
4950
for key, value in _read_header_items(
5051
headers, mapping_error_message=mapping_error_message
5152
):
52-
if type(key) is not str or type(value) is not str:
53+
if not is_plain_string(key) or not is_plain_string(value):
5354
raise HyperbrowserError(effective_pair_error_message)
5455
try:
5556
normalized_key = key.strip()
56-
if type(normalized_key) is not str:
57+
if not is_plain_string(normalized_key):
5758
raise TypeError("normalized header name must be a string")
5859
except HyperbrowserError:
5960
raise
@@ -104,7 +105,7 @@ def normalize_headers(
104105
raise HyperbrowserError("headers must not contain control characters")
105106
try:
106107
canonical_header_name = normalized_key.lower()
107-
if type(canonical_header_name) is not str:
108+
if not is_plain_string(canonical_header_name):
108109
raise TypeError("canonical header name must be a string")
109110
except HyperbrowserError:
110111
raise
@@ -157,11 +158,11 @@ def merge_headers(
157158
def parse_headers_env_json(raw_headers: Optional[str]) -> Optional[Dict[str, str]]:
158159
if raw_headers is None:
159160
return None
160-
if type(raw_headers) is not str:
161+
if not is_plain_string(raw_headers):
161162
raise HyperbrowserError("HYPERBROWSER_HEADERS must be a string")
162163
try:
163164
normalized_raw_headers = raw_headers.strip()
164-
if type(normalized_raw_headers) is not str:
165+
if not is_plain_string(normalized_raw_headers):
165166
raise TypeError("normalized headers payload must be a string")
166167
is_empty_headers_payload = len(normalized_raw_headers) == 0
167168
except HyperbrowserError:

0 commit comments

Comments
 (0)