|
1 | 1 | import pytest |
2 | 2 | from urllib.parse import quote |
3 | 3 |
|
| 4 | +import hyperbrowser.client.base as client_base_module |
4 | 5 | from hyperbrowser import Hyperbrowser |
5 | 6 | from hyperbrowser.config import ClientConfig |
6 | 7 | from hyperbrowser.exceptions import HyperbrowserError |
@@ -392,3 +393,135 @@ def test_client_build_url_normalizes_runtime_trailing_slashes(): |
392 | 393 | assert client._build_url("/session") == "https://example.local/api/session" |
393 | 394 | finally: |
394 | 395 | client.close() |
| 396 | + |
| 397 | + |
| 398 | +def test_client_build_url_wraps_path_parse_runtime_errors( |
| 399 | + monkeypatch: pytest.MonkeyPatch, |
| 400 | +): |
| 401 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 402 | + try: |
| 403 | + def _raise_parse_runtime_error(_value: str): |
| 404 | + raise RuntimeError("path parse exploded") |
| 405 | + |
| 406 | + monkeypatch.setattr(client_base_module, "urlparse", _raise_parse_runtime_error) |
| 407 | + |
| 408 | + with pytest.raises(HyperbrowserError, match="Failed to parse path") as exc_info: |
| 409 | + client._build_url("/session") |
| 410 | + |
| 411 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 412 | + finally: |
| 413 | + client.close() |
| 414 | + |
| 415 | + |
| 416 | +def test_client_build_url_preserves_hyperbrowser_path_parse_errors( |
| 417 | + monkeypatch: pytest.MonkeyPatch, |
| 418 | +): |
| 419 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 420 | + try: |
| 421 | + def _raise_parse_hyperbrowser_error(_value: str): |
| 422 | + raise HyperbrowserError("custom path parse failure") |
| 423 | + |
| 424 | + monkeypatch.setattr( |
| 425 | + client_base_module, |
| 426 | + "urlparse", |
| 427 | + _raise_parse_hyperbrowser_error, |
| 428 | + ) |
| 429 | + |
| 430 | + with pytest.raises( |
| 431 | + HyperbrowserError, match="custom path parse failure" |
| 432 | + ) as exc_info: |
| 433 | + client._build_url("/session") |
| 434 | + |
| 435 | + assert exc_info.value.original_error is None |
| 436 | + finally: |
| 437 | + client.close() |
| 438 | + |
| 439 | + |
| 440 | +def test_client_build_url_wraps_path_component_access_runtime_errors( |
| 441 | + monkeypatch: pytest.MonkeyPatch, |
| 442 | +): |
| 443 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 444 | + try: |
| 445 | + class _BrokenParsedPath: |
| 446 | + @property |
| 447 | + def scheme(self) -> str: |
| 448 | + raise RuntimeError("path scheme exploded") |
| 449 | + |
| 450 | + monkeypatch.setattr(client_base_module, "urlparse", lambda _value: _BrokenParsedPath()) |
| 451 | + |
| 452 | + with pytest.raises( |
| 453 | + HyperbrowserError, match="Failed to parse path components" |
| 454 | + ) as exc_info: |
| 455 | + client._build_url("/session") |
| 456 | + |
| 457 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 458 | + finally: |
| 459 | + client.close() |
| 460 | + |
| 461 | + |
| 462 | +def test_client_build_url_rejects_invalid_path_parser_component_types( |
| 463 | + monkeypatch: pytest.MonkeyPatch, |
| 464 | +): |
| 465 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 466 | + try: |
| 467 | + class _InvalidParsedPath: |
| 468 | + scheme = object() |
| 469 | + netloc = "" |
| 470 | + path = "/session" |
| 471 | + query = "" |
| 472 | + fragment = "" |
| 473 | + |
| 474 | + monkeypatch.setattr(client_base_module, "urlparse", lambda _value: _InvalidParsedPath()) |
| 475 | + |
| 476 | + with pytest.raises( |
| 477 | + HyperbrowserError, match="path parser returned invalid URL components" |
| 478 | + ): |
| 479 | + client._build_url("/session") |
| 480 | + finally: |
| 481 | + client.close() |
| 482 | + |
| 483 | + |
| 484 | +def test_client_build_url_wraps_normalized_path_parse_runtime_errors( |
| 485 | + monkeypatch: pytest.MonkeyPatch, |
| 486 | +): |
| 487 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 488 | + try: |
| 489 | + original_urlparse = client_base_module.urlparse |
| 490 | + |
| 491 | + def _conditional_urlparse(value: str): |
| 492 | + if value == "/session": |
| 493 | + raise RuntimeError("normalized path parse exploded") |
| 494 | + return original_urlparse(value) |
| 495 | + |
| 496 | + monkeypatch.setattr(client_base_module, "urlparse", _conditional_urlparse) |
| 497 | + |
| 498 | + with pytest.raises( |
| 499 | + HyperbrowserError, match="Failed to parse normalized path" |
| 500 | + ) as exc_info: |
| 501 | + client._build_url("session") |
| 502 | + |
| 503 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 504 | + finally: |
| 505 | + client.close() |
| 506 | + |
| 507 | + |
| 508 | +def test_client_build_url_wraps_base_url_parse_runtime_errors( |
| 509 | + monkeypatch: pytest.MonkeyPatch, |
| 510 | +): |
| 511 | + client = Hyperbrowser(config=ClientConfig(api_key="test-key")) |
| 512 | + try: |
| 513 | + original_urlparse = client_base_module.urlparse |
| 514 | + |
| 515 | + def _conditional_urlparse(value: str): |
| 516 | + if value == "https://api.hyperbrowser.ai": |
| 517 | + raise RuntimeError("base_url parse exploded") |
| 518 | + return original_urlparse(value) |
| 519 | + |
| 520 | + monkeypatch.setattr(client_base_module, "urlparse", _conditional_urlparse) |
| 521 | + |
| 522 | + with pytest.raises(HyperbrowserError, match="Failed to parse base_url") as exc_info: |
| 523 | + client._build_url("/session") |
| 524 | + |
| 525 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 526 | + finally: |
| 527 | + client.close() |
0 commit comments