Skip to content

Commit 3cb0a43

Browse files
Require concrete env string inputs for config and headers
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent d3a77a5 commit 3cb0a43

File tree

4 files changed

+10
-61
lines changed

4 files changed

+10
-61
lines changed

hyperbrowser/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def parse_headers_from_env(raw_headers: Optional[str]) -> Optional[Dict[str, str
350350
def resolve_base_url_from_env(raw_base_url: Optional[str]) -> str:
351351
if raw_base_url is None:
352352
return "https://api.hyperbrowser.ai"
353-
if not isinstance(raw_base_url, str):
353+
if type(raw_base_url) is not str:
354354
raise HyperbrowserError("HYPERBROWSER_BASE_URL must be a string")
355355
try:
356356
normalized_env_base_url = raw_base_url.strip()

hyperbrowser/header_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def merge_headers(
157157
def parse_headers_env_json(raw_headers: Optional[str]) -> Optional[Dict[str, str]]:
158158
if raw_headers is None:
159159
return None
160-
if not isinstance(raw_headers, str):
160+
if type(raw_headers) is not str:
161161
raise HyperbrowserError("HYPERBROWSER_HEADERS must be a string")
162162
try:
163163
normalized_raw_headers = raw_headers.strip()

tests/test_config.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,7 @@ def strip(self, chars=None): # type: ignore[override]
333333
assert isinstance(exc_info.value.original_error, TypeError)
334334

335335

336-
def test_client_config_resolve_base_url_from_env_wraps_strip_runtime_errors():
337-
class _BrokenBaseUrl(str):
338-
def strip(self, chars=None): # type: ignore[override]
339-
_ = chars
340-
raise RuntimeError("environment base_url strip exploded")
341-
342-
with pytest.raises(
343-
HyperbrowserError, match="Failed to normalize HYPERBROWSER_BASE_URL"
344-
) as exc_info:
345-
ClientConfig.resolve_base_url_from_env(_BrokenBaseUrl("https://example.local"))
346-
347-
assert isinstance(exc_info.value.original_error, RuntimeError)
348-
349-
350-
def test_client_config_resolve_base_url_from_env_wraps_string_subclass_strip_results():
336+
def test_client_config_resolve_base_url_from_env_rejects_string_subclass_inputs():
351337
class _BrokenBaseUrl(str):
352338
class _NormalizedBaseUrl(str):
353339
pass
@@ -357,12 +343,10 @@ def strip(self, chars=None): # type: ignore[override]
357343
return self._NormalizedBaseUrl("https://example.local")
358344

359345
with pytest.raises(
360-
HyperbrowserError, match="Failed to normalize HYPERBROWSER_BASE_URL"
361-
) as exc_info:
346+
HyperbrowserError, match="HYPERBROWSER_BASE_URL must be a string"
347+
):
362348
ClientConfig.resolve_base_url_from_env(_BrokenBaseUrl("https://example.local"))
363349

364-
assert isinstance(exc_info.value.original_error, TypeError)
365-
366350

367351
def test_client_config_wraps_api_key_strip_runtime_errors():
368352
class _BrokenApiKey(str):

tests/test_header_utils.py

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ def strip(self, chars=None): # type: ignore[override]
4545
return self._NormalizedKey("X-Trace-Id")
4646

4747

48-
class _BrokenHeadersEnvString(str):
49-
def strip(self, chars=None): # type: ignore[override]
50-
_ = chars
51-
raise RuntimeError("headers env strip exploded")
52-
53-
5448
class _NonStringHeadersEnvStripResult(str):
5549
def strip(self, chars=None): # type: ignore[override]
5650
_ = chars
@@ -168,46 +162,17 @@ def test_parse_headers_env_json_rejects_non_string_input():
168162
parse_headers_env_json(123) # type: ignore[arg-type]
169163

170164

171-
def test_parse_headers_env_json_wraps_strip_runtime_errors():
172-
with pytest.raises(
173-
HyperbrowserError, match="Failed to normalize HYPERBROWSER_HEADERS"
174-
) as exc_info:
175-
parse_headers_env_json(_BrokenHeadersEnvString('{"X-Trace-Id":"abc123"}'))
176-
177-
assert isinstance(exc_info.value.original_error, RuntimeError)
178-
179-
180-
def test_parse_headers_env_json_preserves_hyperbrowser_strip_errors():
181-
class _BrokenHeadersEnvString(str):
182-
def strip(self, chars=None): # type: ignore[override]
183-
_ = chars
184-
raise HyperbrowserError("custom headers strip failure")
185-
186-
with pytest.raises(
187-
HyperbrowserError, match="custom headers strip failure"
188-
) as exc_info:
189-
parse_headers_env_json(_BrokenHeadersEnvString('{"X-Trace-Id":"abc123"}'))
190-
191-
assert exc_info.value.original_error is None
192-
193-
194-
def test_parse_headers_env_json_wraps_non_string_strip_results():
165+
def test_parse_headers_env_json_rejects_string_subclass_input_values():
195166
with pytest.raises(
196-
HyperbrowserError, match="Failed to normalize HYPERBROWSER_HEADERS"
197-
) as exc_info:
167+
HyperbrowserError, match="HYPERBROWSER_HEADERS must be a string"
168+
):
198169
parse_headers_env_json(_NonStringHeadersEnvStripResult('{"X-Trace-Id":"abc123"}'))
199170

200-
assert isinstance(exc_info.value.original_error, TypeError)
201-
202-
203-
def test_parse_headers_env_json_wraps_string_subclass_strip_results():
204171
with pytest.raises(
205-
HyperbrowserError, match="Failed to normalize HYPERBROWSER_HEADERS"
206-
) as exc_info:
172+
HyperbrowserError, match="HYPERBROWSER_HEADERS must be a string"
173+
):
207174
parse_headers_env_json(_StringSubclassHeadersEnvStripResult('{"X-Trace-Id":"abc123"}'))
208175

209-
assert isinstance(exc_info.value.original_error, TypeError)
210-
211176

212177
def test_parse_headers_env_json_rejects_invalid_json():
213178
with pytest.raises(

0 commit comments

Comments
 (0)