|
3 | 3 | from typing import Dict, Mapping, Optional, cast |
4 | 4 |
|
5 | 5 | from .exceptions import HyperbrowserError |
| 6 | +from .type_utils import is_plain_string |
6 | 7 |
|
7 | 8 | _INVALID_HEADER_NAME_CHARACTER_PATTERN = re.compile(r"[^!#$%&'*+\-.^_`|~0-9A-Za-z]") |
8 | 9 | _MAX_HEADER_NAME_LENGTH = 256 |
@@ -49,11 +50,11 @@ def normalize_headers( |
49 | 50 | for key, value in _read_header_items( |
50 | 51 | headers, mapping_error_message=mapping_error_message |
51 | 52 | ): |
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): |
53 | 54 | raise HyperbrowserError(effective_pair_error_message) |
54 | 55 | try: |
55 | 56 | normalized_key = key.strip() |
56 | | - if type(normalized_key) is not str: |
| 57 | + if not is_plain_string(normalized_key): |
57 | 58 | raise TypeError("normalized header name must be a string") |
58 | 59 | except HyperbrowserError: |
59 | 60 | raise |
@@ -104,7 +105,7 @@ def normalize_headers( |
104 | 105 | raise HyperbrowserError("headers must not contain control characters") |
105 | 106 | try: |
106 | 107 | canonical_header_name = normalized_key.lower() |
107 | | - if type(canonical_header_name) is not str: |
| 108 | + if not is_plain_string(canonical_header_name): |
108 | 109 | raise TypeError("canonical header name must be a string") |
109 | 110 | except HyperbrowserError: |
110 | 111 | raise |
@@ -157,11 +158,11 @@ def merge_headers( |
157 | 158 | def parse_headers_env_json(raw_headers: Optional[str]) -> Optional[Dict[str, str]]: |
158 | 159 | if raw_headers is None: |
159 | 160 | return None |
160 | | - if type(raw_headers) is not str: |
| 161 | + if not is_plain_string(raw_headers): |
161 | 162 | raise HyperbrowserError("HYPERBROWSER_HEADERS must be a string") |
162 | 163 | try: |
163 | 164 | normalized_raw_headers = raw_headers.strip() |
164 | | - if type(normalized_raw_headers) is not str: |
| 165 | + if not is_plain_string(normalized_raw_headers): |
165 | 166 | raise TypeError("normalized headers payload must be a string") |
166 | 167 | is_empty_headers_payload = len(normalized_raw_headers) == 0 |
167 | 168 | except HyperbrowserError: |
|
0 commit comments