Skip to content

Commit ec52966

Browse files
Validate non-negative client timeout configuration
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 2874cc4 commit ec52966

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

hyperbrowser/client/async_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Mapping, Optional
22

3+
from ..exceptions import HyperbrowserError
34
from ..config import ClientConfig
45
from ..transport.async_transport import AsyncTransport
56
from .base import HyperbrowserBase
@@ -24,8 +25,10 @@ def __init__(
2425
api_key: Optional[str] = None,
2526
base_url: Optional[str] = None,
2627
headers: Optional[Mapping[str, str]] = None,
27-
timeout: Optional[int] = 30,
28+
timeout: Optional[float] = 30,
2829
):
30+
if timeout is not None and timeout < 0:
31+
raise HyperbrowserError("timeout must be non-negative")
2932
super().__init__(AsyncTransport, config, api_key, base_url, headers)
3033
self.transport.client.timeout = timeout
3134
self.sessions = SessionManager(self)

hyperbrowser/client/sync.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Mapping, Optional
22

3+
from ..exceptions import HyperbrowserError
34
from ..config import ClientConfig
45
from ..transport.sync import SyncTransport
56
from .base import HyperbrowserBase
@@ -24,8 +25,10 @@ def __init__(
2425
api_key: Optional[str] = None,
2526
base_url: Optional[str] = None,
2627
headers: Optional[Mapping[str, str]] = None,
27-
timeout: Optional[int] = 30,
28+
timeout: Optional[float] = 30,
2829
):
30+
if timeout is not None and timeout < 0:
31+
raise HyperbrowserError("timeout must be non-negative")
2932
super().__init__(SyncTransport, config, api_key, base_url, headers)
3033
self.transport.client.timeout = timeout
3134
self.sessions = SessionManager(self)

tests/test_client_timeout.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from hyperbrowser import AsyncHyperbrowser, Hyperbrowser
6+
from hyperbrowser.exceptions import HyperbrowserError
7+
8+
9+
def test_sync_client_rejects_negative_timeout():
10+
with pytest.raises(HyperbrowserError, match="timeout must be non-negative"):
11+
Hyperbrowser(api_key="test-key", timeout=-1)
12+
13+
14+
def test_async_client_rejects_negative_timeout():
15+
with pytest.raises(HyperbrowserError, match="timeout must be non-negative"):
16+
AsyncHyperbrowser(api_key="test-key", timeout=-1)
17+
18+
19+
def test_sync_client_accepts_none_timeout():
20+
client = Hyperbrowser(api_key="test-key", timeout=None)
21+
client.close()
22+
23+
24+
def test_async_client_accepts_none_timeout():
25+
async def run() -> None:
26+
client = AsyncHyperbrowser(api_key="test-key", timeout=None)
27+
await client.close()
28+
29+
asyncio.run(run())

0 commit comments

Comments
 (0)