|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hyperbrowser.exceptions import HyperbrowserError |
| 4 | +from hyperbrowser.transport.async_transport import AsyncTransport |
| 5 | +from hyperbrowser.transport.sync import SyncTransport |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize("transport_class", [SyncTransport, AsyncTransport]) |
| 9 | +def test_transport_wraps_api_key_strip_runtime_errors(transport_class): |
| 10 | + class _BrokenStripApiKey(str): |
| 11 | + def strip(self, chars=None): # type: ignore[override] |
| 12 | + _ = chars |
| 13 | + raise RuntimeError("api key strip exploded") |
| 14 | + |
| 15 | + with pytest.raises(HyperbrowserError, match="Failed to normalize api_key") as exc_info: |
| 16 | + transport_class(api_key=_BrokenStripApiKey("test-key")) |
| 17 | + |
| 18 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("transport_class", [SyncTransport, AsyncTransport]) |
| 22 | +def test_transport_preserves_hyperbrowser_api_key_strip_errors(transport_class): |
| 23 | + class _BrokenStripApiKey(str): |
| 24 | + def strip(self, chars=None): # type: ignore[override] |
| 25 | + _ = chars |
| 26 | + raise HyperbrowserError("custom strip failure") |
| 27 | + |
| 28 | + with pytest.raises(HyperbrowserError, match="custom strip failure") as exc_info: |
| 29 | + transport_class(api_key=_BrokenStripApiKey("test-key")) |
| 30 | + |
| 31 | + assert exc_info.value.original_error is None |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("transport_class", [SyncTransport, AsyncTransport]) |
| 35 | +def test_transport_wraps_non_string_api_key_strip_results(transport_class): |
| 36 | + class _NonStringStripResultApiKey(str): |
| 37 | + def strip(self, chars=None): # type: ignore[override] |
| 38 | + _ = chars |
| 39 | + return object() |
| 40 | + |
| 41 | + with pytest.raises(HyperbrowserError, match="Failed to normalize api_key") as exc_info: |
| 42 | + transport_class(api_key=_NonStringStripResultApiKey("test-key")) |
| 43 | + |
| 44 | + assert isinstance(exc_info.value.original_error, TypeError) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("transport_class", [SyncTransport, AsyncTransport]) |
| 48 | +def test_transport_wraps_api_key_character_iteration_failures(transport_class): |
| 49 | + class _BrokenIterApiKey(str): |
| 50 | + def strip(self, chars=None): # type: ignore[override] |
| 51 | + _ = chars |
| 52 | + return self |
| 53 | + |
| 54 | + def __iter__(self): |
| 55 | + raise RuntimeError("api key iteration exploded") |
| 56 | + |
| 57 | + with pytest.raises( |
| 58 | + HyperbrowserError, match="Failed to validate api_key characters" |
| 59 | + ) as exc_info: |
| 60 | + transport_class(api_key=_BrokenIterApiKey("test-key")) |
| 61 | + |
| 62 | + assert isinstance(exc_info.value.original_error, RuntimeError) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("transport_class", [SyncTransport, AsyncTransport]) |
| 66 | +def test_transport_preserves_hyperbrowser_api_key_character_iteration_failures( |
| 67 | + transport_class, |
| 68 | +): |
| 69 | + class _BrokenIterApiKey(str): |
| 70 | + def strip(self, chars=None): # type: ignore[override] |
| 71 | + _ = chars |
| 72 | + return self |
| 73 | + |
| 74 | + def __iter__(self): |
| 75 | + raise HyperbrowserError("custom iteration failure") |
| 76 | + |
| 77 | + with pytest.raises(HyperbrowserError, match="custom iteration failure") as exc_info: |
| 78 | + transport_class(api_key=_BrokenIterApiKey("test-key")) |
| 79 | + |
| 80 | + assert exc_info.value.original_error is None |
0 commit comments