Skip to content

Commit 8c2c9a9

Browse files
Enforce HTTP status range in APIResponse
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent c445731 commit 8c2c9a9

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

hyperbrowser/transport/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
_TRUNCATED_DISPLAY_SUFFIX = "... (truncated)"
99
_MAX_MODEL_NAME_DISPLAY_LENGTH = 120
1010
_MAX_MAPPING_KEY_DISPLAY_LENGTH = 120
11+
_MIN_HTTP_STATUS_CODE = 100
12+
_MAX_HTTP_STATUS_CODE = 599
1113

1214

1315
def _sanitize_display_text(value: str, *, max_length: int) -> str:
@@ -57,6 +59,8 @@ class APIResponse(Generic[T]):
5759
def __init__(self, data: Optional[Union[dict, T]] = None, status_code: int = 200):
5860
if isinstance(status_code, bool) or not isinstance(status_code, int):
5961
raise HyperbrowserError("status_code must be an integer")
62+
if not (_MIN_HTTP_STATUS_CODE <= status_code <= _MAX_HTTP_STATUS_CODE):
63+
raise HyperbrowserError("status_code must be between 100 and 599")
6064
self.data = data
6165
self.status_code = status_code
6266

tests/test_transport_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,22 @@ def test_api_response_constructor_rejects_boolean_status_code() -> None:
245245
APIResponse(status_code=True)
246246

247247

248+
@pytest.mark.parametrize("status_code", [99, 600])
249+
def test_api_response_constructor_rejects_out_of_range_status_code(
250+
status_code: int,
251+
) -> None:
252+
with pytest.raises(HyperbrowserError, match="status_code must be between 100 and 599"):
253+
APIResponse(status_code=status_code)
254+
255+
248256
def test_api_response_from_status_rejects_boolean_status_code() -> None:
249257
with pytest.raises(HyperbrowserError, match="status_code must be an integer"):
250258
APIResponse.from_status(True) # type: ignore[arg-type]
259+
260+
261+
@pytest.mark.parametrize("status_code", [99, 600])
262+
def test_api_response_from_status_rejects_out_of_range_status_code(
263+
status_code: int,
264+
) -> None:
265+
with pytest.raises(HyperbrowserError, match="status_code must be between 100 and 599"):
266+
APIResponse.from_status(status_code)

0 commit comments

Comments
 (0)