Skip to content

Commit cf08d9a

Browse files
Require host component in configured base URLs
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent e39a432 commit cf08d9a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

hyperbrowser/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22
import json
3+
from urllib.parse import urlparse
34
from typing import Dict, Mapping, Optional
45
import os
56

@@ -27,10 +28,14 @@ def __post_init__(self) -> None:
2728
self.base_url = self.base_url.strip().rstrip("/")
2829
if not self.base_url:
2930
raise HyperbrowserError("base_url must not be empty")
30-
if not (
31-
self.base_url.startswith("https://") or self.base_url.startswith("http://")
31+
parsed_base_url = urlparse(self.base_url)
32+
if (
33+
parsed_base_url.scheme not in {"https", "http"}
34+
or not parsed_base_url.netloc
3235
):
33-
raise HyperbrowserError("base_url must start with 'https://' or 'http://'")
36+
raise HyperbrowserError(
37+
"base_url must start with 'https://' or 'http://' and include a host"
38+
)
3439
if self.headers is not None:
3540
normalized_headers: Dict[str, str] = {}
3641
for key, value in self.headers.items():

tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ def test_client_config_from_env_rejects_invalid_base_url(monkeypatch):
117117
ClientConfig.from_env()
118118

119119

120+
def test_client_config_from_env_rejects_base_url_without_host(monkeypatch):
121+
monkeypatch.setenv("HYPERBROWSER_API_KEY", "test-key")
122+
monkeypatch.setenv("HYPERBROWSER_BASE_URL", "https://")
123+
124+
with pytest.raises(HyperbrowserError, match="include a host"):
125+
ClientConfig.from_env()
126+
127+
120128
def test_client_config_normalizes_whitespace_and_trailing_slash():
121129
config = ClientConfig(api_key=" test-key ", base_url=" https://example.local/ ")
122130

@@ -145,6 +153,9 @@ def test_client_config_rejects_empty_or_invalid_base_url():
145153
with pytest.raises(HyperbrowserError, match="base_url must start with"):
146154
ClientConfig(api_key="test-key", base_url="api.hyperbrowser.ai")
147155

156+
with pytest.raises(HyperbrowserError, match="include a host"):
157+
ClientConfig(api_key="test-key", base_url="http://")
158+
148159

149160
def test_client_config_normalizes_headers_to_internal_copy():
150161
headers = {"X-Correlation-Id": "abc123"}

0 commit comments

Comments
 (0)