File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 11from dataclasses import dataclass
22import json
3+ from urllib .parse import urlparse
34from typing import Dict , Mapping , Optional
45import 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 ():
Original file line number Diff line number Diff 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+
120128def 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
149160def test_client_config_normalizes_headers_to_internal_copy ():
150161 headers = {"X-Correlation-Id" : "abc123" }
You can’t perform that action at this time.
0 commit comments