Skip to content

Commit 9a3627e

Browse files
Require concrete string path normalization outputs in URL builder
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent cf8f959 commit 9a3627e

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

hyperbrowser/client/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,22 @@ def _build_url(self, path: str) -> str:
150150
raise HyperbrowserError("path must be a string")
151151
try:
152152
stripped_path = path.strip()
153-
if not isinstance(stripped_path, str):
153+
if type(stripped_path) is not str:
154154
raise TypeError("normalized path must be a string")
155+
has_surrounding_whitespace = stripped_path != path
156+
is_empty_path = len(stripped_path) == 0
155157
except HyperbrowserError:
156158
raise
157159
except Exception as exc:
158160
raise HyperbrowserError(
159161
"Failed to normalize path",
160162
original_error=exc,
161163
) from exc
162-
if stripped_path != path:
164+
if has_surrounding_whitespace:
163165
raise HyperbrowserError(
164166
"path must not contain leading or trailing whitespace"
165167
)
166-
if not stripped_path:
168+
if is_empty_path:
167169
raise HyperbrowserError("path must not be empty")
168170
if "\\" in stripped_path:
169171
raise HyperbrowserError("path must not contain backslashes")

tests/test_url_building.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,25 @@ def strip(self, chars=None): # type: ignore[override]
401401
client.close()
402402

403403

404+
def test_client_build_url_wraps_string_subclass_path_strip_results():
405+
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
406+
try:
407+
class _BrokenPath(str):
408+
class _NormalizedPath(str):
409+
pass
410+
411+
def strip(self, chars=None): # type: ignore[override]
412+
_ = chars
413+
return self._NormalizedPath("/session")
414+
415+
with pytest.raises(HyperbrowserError, match="Failed to normalize path") as exc_info:
416+
client._build_url(_BrokenPath("/session"))
417+
418+
assert isinstance(exc_info.value.original_error, TypeError)
419+
finally:
420+
client.close()
421+
422+
404423
def test_client_build_url_allows_query_values_containing_absolute_urls():
405424
client = Hyperbrowser(config=ClientConfig(api_key="test-key"))
406425
try:

0 commit comments

Comments
 (0)