|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from pydantic import ValidationError |
| 7 | + |
| 8 | +from hyperbrowser import AsyncHyperbrowser, Hyperbrowser |
| 9 | +from hyperbrowser.exceptions import HyperbrowserError |
| 10 | +from hyperbrowser.models import ( |
| 11 | + CreateSessionParams, |
| 12 | + ProxyRoute, |
| 13 | + SessionLaunchState, |
| 14 | + WebBotAuthParams, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(params=[False, True], ids=["sync", "async"]) |
| 19 | +def sdk(request): |
| 20 | + """Exercise the same wire contract through both public clients and transports.""" |
| 21 | + is_async = request.param |
| 22 | + loop = asyncio.new_event_loop() |
| 23 | + client = (AsyncHyperbrowser if is_async else Hyperbrowser)(api_key="test-key") |
| 24 | + calls = [] |
| 25 | + |
| 26 | + def handler(req): |
| 27 | + calls.append(req) |
| 28 | + return httpx.Response(400, json={"message": "fixture stops before parsing"}) |
| 29 | + |
| 30 | + def call(method, *args, **kwargs): |
| 31 | + result = method(*args, **kwargs) |
| 32 | + return loop.run_until_complete(result) if is_async else result |
| 33 | + |
| 34 | + call(client.transport.close) |
| 35 | + client.transport.client = (httpx.AsyncClient if is_async else httpx.Client)( |
| 36 | + transport=httpx.MockTransport(handler), |
| 37 | + headers={"x-api-key": "test-key"}, |
| 38 | + timeout=30, |
| 39 | + ) |
| 40 | + if is_async: |
| 41 | + client.transport._closed = False |
| 42 | + yield client, call, calls |
| 43 | + call(client.close) |
| 44 | + loop.close() |
| 45 | + |
| 46 | + |
| 47 | +def sent_bodies(sdk, method, params_list): |
| 48 | + client, call, calls = sdk |
| 49 | + for params in params_list: |
| 50 | + with pytest.raises(HyperbrowserError): |
| 51 | + call(method, params) |
| 52 | + return [json.loads(req.content) for req in calls] |
| 53 | + |
| 54 | + |
| 55 | +def test_web_bot_auth_on_the_wire(sdk): |
| 56 | + client = sdk[0] |
| 57 | + bodies = sent_bodies( |
| 58 | + sdk, |
| 59 | + client.sessions.create, |
| 60 | + [ |
| 61 | + {}, |
| 62 | + {"web_bot_auth": True}, |
| 63 | + {"web_bot_auth": False}, |
| 64 | + {"web_bot_auth": {"only_domains": ["example.com", "shop.example.org"]}}, |
| 65 | + CreateSessionParams(web_bot_auth=True), |
| 66 | + CreateSessionParams( |
| 67 | + web_bot_auth=WebBotAuthParams(only_domains=["example.com"]) |
| 68 | + ), |
| 69 | + ], |
| 70 | + ) |
| 71 | + assert "webBotAuth" not in bodies[0] |
| 72 | + assert bodies[1]["webBotAuth"] is True |
| 73 | + assert bodies[2]["webBotAuth"] is False |
| 74 | + assert bodies[3]["webBotAuth"] == { |
| 75 | + "onlyDomains": ["example.com", "shop.example.org"] |
| 76 | + } |
| 77 | + assert bodies[4]["webBotAuth"] is True |
| 78 | + assert bodies[5]["webBotAuth"] == {"onlyDomains": ["example.com"]} |
| 79 | + |
| 80 | + |
| 81 | +def test_proxy_routes_on_the_wire_in_order(sdk): |
| 82 | + client = sdk[0] |
| 83 | + routes = [ |
| 84 | + {"match": "api.example.com", "proxy": "default"}, |
| 85 | + {"match": ["*.example.com", "cdn.example.net"], "proxy": "direct"}, |
| 86 | + ] |
| 87 | + bodies = sent_bodies( |
| 88 | + sdk, |
| 89 | + client.sessions.create, |
| 90 | + [ |
| 91 | + {"use_proxy": True, "proxy_routes": routes}, |
| 92 | + CreateSessionParams( |
| 93 | + use_proxy=True, |
| 94 | + proxy_routes=[ProxyRoute(**route) for route in routes], |
| 95 | + ), |
| 96 | + ], |
| 97 | + ) |
| 98 | + for body in bodies: |
| 99 | + assert body["useProxy"] is True |
| 100 | + assert body["proxyRoutes"] == routes |
| 101 | + |
| 102 | + |
| 103 | +def test_agent_tasks_pass_web_bot_auth_through_session_options(sdk): |
| 104 | + client = sdk[0] |
| 105 | + bodies = sent_bodies( |
| 106 | + sdk, |
| 107 | + client.agents.browser_use.start, |
| 108 | + [ |
| 109 | + { |
| 110 | + "task": "Open example.com", |
| 111 | + "session_options": {"web_bot_auth": {"only_domains": ["example.com"]}}, |
| 112 | + } |
| 113 | + ], |
| 114 | + ) |
| 115 | + assert sdk[2][0].url.path == "/api/task/browser-use" |
| 116 | + assert bodies[0]["sessionOptions"]["webBotAuth"] == {"onlyDomains": ["example.com"]} |
| 117 | + |
| 118 | + |
| 119 | +def test_misspelled_or_invalid_options_fail_before_sending(sdk): |
| 120 | + client, call, calls = sdk |
| 121 | + for params in [ |
| 122 | + # A dropped key would sign requests to every host instead of example.com. |
| 123 | + {"web_bot_auth": {"onlyDomains": ["example.com"]}}, |
| 124 | + {"proxy_routes": [{"match": "example.com", "proxy": "upstream"}]}, |
| 125 | + {"proxy_routes": [{"match": "example.com"}]}, |
| 126 | + {"proxy_routes": [{"match": "example.com", "proxy": "direct", "port": 443}]}, |
| 127 | + ]: |
| 128 | + with pytest.raises(ValidationError): |
| 129 | + call(client.sessions.create, params) |
| 130 | + assert calls == [] |
| 131 | + |
| 132 | + |
| 133 | +def test_launch_state_reports_how_a_session_signs(): |
| 134 | + state = SessionLaunchState.model_validate( |
| 135 | + { |
| 136 | + "webBotAuth": { |
| 137 | + "signatureAgent": "https://www.hyperbrowser.ai", |
| 138 | + "onlyDomains": ["example.com"], |
| 139 | + } |
| 140 | + } |
| 141 | + ) |
| 142 | + assert state.web_bot_auth is not None |
| 143 | + assert state.web_bot_auth.signature_agent == "https://www.hyperbrowser.ai" |
| 144 | + assert state.web_bot_auth.only_domains == ["example.com"] |
| 145 | + unscoped = SessionLaunchState.model_validate( |
| 146 | + {"webBotAuth": {"signatureAgent": "https://www.hyperbrowser.ai"}} |
| 147 | + ) |
| 148 | + assert unscoped.web_bot_auth is not None |
| 149 | + assert unscoped.web_bot_auth.only_domains is None |
| 150 | + assert SessionLaunchState().web_bot_auth is None |
0 commit comments