Skip to content

Commit 5b1cc86

Browse files
Validate base_url format and avoid double /api paths
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent b300e0e commit 5b1cc86

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

hyperbrowser/client/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ def __init__(
4949

5050
def _build_url(self, path: str) -> str:
5151
normalized_path = path if path.startswith("/") else f"/{path}"
52+
if normalized_path == "/api" or normalized_path.startswith("/api/"):
53+
return f"{self.config.base_url}{normalized_path}"
5254
return f"{self.config.base_url}/api{normalized_path}"

hyperbrowser/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ def __post_init__(self) -> None:
2020
raise HyperbrowserError("base_url must be a string")
2121
self.api_key = self.api_key.strip()
2222
self.base_url = self.base_url.strip().rstrip("/")
23+
if not self.base_url:
24+
raise HyperbrowserError("base_url must not be empty")
25+
if not (
26+
self.base_url.startswith("https://")
27+
or self.base_url.startswith("http://")
28+
):
29+
raise HyperbrowserError(
30+
"base_url must start with 'https://' or 'http://'"
31+
)
2332

2433
@classmethod
2534
def from_env(cls) -> "ClientConfig":

tests/test_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ def test_client_config_rejects_non_string_values():
4141

4242
with pytest.raises(HyperbrowserError, match="base_url must be a string"):
4343
ClientConfig(api_key="test-key", base_url=None) # type: ignore[arg-type]
44+
45+
46+
def test_client_config_rejects_empty_or_invalid_base_url():
47+
with pytest.raises(HyperbrowserError, match="base_url must not be empty"):
48+
ClientConfig(api_key="test-key", base_url=" ")
49+
50+
with pytest.raises(HyperbrowserError, match="base_url must start with"):
51+
ClientConfig(api_key="test-key", base_url="api.hyperbrowser.ai")

tests/test_url_building.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def test_client_build_url_normalizes_leading_slash():
77
try:
88
assert client._build_url("/session") == "https://api.hyperbrowser.ai/api/session"
99
assert client._build_url("session") == "https://api.hyperbrowser.ai/api/session"
10+
assert client._build_url("/api/session") == "https://api.hyperbrowser.ai/api/session"
1011
finally:
1112
client.close()
1213

0 commit comments

Comments
 (0)