Skip to content

Commit 6f9ee0a

Browse files
Validate APIResponse status code types
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent b35bd34 commit 6f9ee0a

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

hyperbrowser/transport/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class APIResponse(Generic[T]):
5555
"""
5656

5757
def __init__(self, data: Optional[Union[dict, T]] = None, status_code: int = 200):
58+
if isinstance(status_code, bool) or not isinstance(status_code, int):
59+
raise HyperbrowserError("status_code must be an integer")
5860
self.data = data
5961
self.status_code = status_code
6062

tests/test_transport_base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,18 @@ def test_api_response_from_json_preserves_hyperbrowser_errors() -> None:
233233
APIResponse.from_json({}, _RaisesHyperbrowserModel)
234234

235235
assert exc_info.value.original_error is None
236+
237+
238+
def test_api_response_constructor_rejects_non_integer_status_code() -> None:
239+
with pytest.raises(HyperbrowserError, match="status_code must be an integer"):
240+
APIResponse(status_code="200") # type: ignore[arg-type]
241+
242+
243+
def test_api_response_constructor_rejects_boolean_status_code() -> None:
244+
with pytest.raises(HyperbrowserError, match="status_code must be an integer"):
245+
APIResponse(status_code=True)
246+
247+
248+
def test_api_response_from_status_rejects_boolean_status_code() -> None:
249+
with pytest.raises(HyperbrowserError, match="status_code must be an integer"):
250+
APIResponse.from_status(True) # type: ignore[arg-type]

0 commit comments

Comments
 (0)