|
6 | 6 |
|
7 | 7 | from hyperbrowser import AsyncHyperbrowser, Hyperbrowser |
8 | 8 | from hyperbrowser.models.scrape import ScrapeOptions, StartScrapeJobParams |
| 9 | +from hyperbrowser.models.session import UpdateSessionSolveCaptchasParams |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def _read_json_body(handler: BaseHTTPRequestHandler): |
@@ -62,6 +63,35 @@ def do_GET(self): |
62 | 63 |
|
63 | 64 | _send_json(self, 404, {"message": f"unexpected route {self.path}"}) |
64 | 65 |
|
| 66 | + def do_PUT(self): |
| 67 | + body = _read_json_body(self) |
| 68 | + requests.append( |
| 69 | + { |
| 70 | + "method": self.command, |
| 71 | + "path": self.path, |
| 72 | + "api_key": self.headers.get("x-api-key"), |
| 73 | + "content_type": self.headers.get("content-type"), |
| 74 | + "body": body, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + if ( |
| 79 | + self.path |
| 80 | + == "/api/session/52dd29fb-75a2-43f9-9831-8ff377fedb0a/update" |
| 81 | + and body.get("type") == "solveCaptchas" |
| 82 | + ): |
| 83 | + _send_json( |
| 84 | + self, |
| 85 | + 200, |
| 86 | + { |
| 87 | + "success": True, |
| 88 | + "solveCaptchas": bool(body.get("params", {}).get("enabled")), |
| 89 | + }, |
| 90 | + ) |
| 91 | + return |
| 92 | + |
| 93 | + _send_json(self, 404, {"message": f"unexpected route {self.path}"}) |
| 94 | + |
65 | 95 | def log_message(self, format, *args): |
66 | 96 | return |
67 | 97 |
|
@@ -149,3 +179,102 @@ async def test_async_client_uses_configured_api_endpoint_and_parses_responses(): |
149 | 179 | "body": None, |
150 | 180 | }, |
151 | 181 | ] |
| 182 | + |
| 183 | + |
| 184 | +def test_sync_session_captcha_solving_update_starts_and_stops_automatic_solving(): |
| 185 | + server, base_url, requests = _start_server() |
| 186 | + client = Hyperbrowser(api_key="test-api-key", base_url=base_url) |
| 187 | + try: |
| 188 | + started = client.sessions.start_captcha_solving( |
| 189 | + "52dd29fb-75a2-43f9-9831-8ff377fedb0a", |
| 190 | + UpdateSessionSolveCaptchasParams(solver_type="visual"), |
| 191 | + ) |
| 192 | + stopped = client.sessions.stop_captcha_solving( |
| 193 | + "52dd29fb-75a2-43f9-9831-8ff377fedb0a" |
| 194 | + ) |
| 195 | + finally: |
| 196 | + client.close() |
| 197 | + server.shutdown() |
| 198 | + server.server_close() |
| 199 | + |
| 200 | + assert started.success is True |
| 201 | + assert started.solve_captchas is True |
| 202 | + assert stopped.success is True |
| 203 | + assert stopped.solve_captchas is False |
| 204 | + assert requests == [ |
| 205 | + { |
| 206 | + "method": "PUT", |
| 207 | + "path": "/api/session/52dd29fb-75a2-43f9-9831-8ff377fedb0a/update", |
| 208 | + "api_key": "test-api-key", |
| 209 | + "content_type": "application/json", |
| 210 | + "body": { |
| 211 | + "type": "solveCaptchas", |
| 212 | + "params": { |
| 213 | + "enabled": True, |
| 214 | + "solverType": "visual", |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + "method": "PUT", |
| 220 | + "path": "/api/session/52dd29fb-75a2-43f9-9831-8ff377fedb0a/update", |
| 221 | + "api_key": "test-api-key", |
| 222 | + "content_type": "application/json", |
| 223 | + "body": { |
| 224 | + "type": "solveCaptchas", |
| 225 | + "params": { |
| 226 | + "enabled": False, |
| 227 | + }, |
| 228 | + }, |
| 229 | + }, |
| 230 | + ] |
| 231 | + |
| 232 | + |
| 233 | +@pytest.mark.anyio |
| 234 | +async def test_async_session_captcha_solving_update_starts_and_stops_automatic_solving(): |
| 235 | + server, base_url, requests = _start_server() |
| 236 | + client = AsyncHyperbrowser(api_key="test-api-key", base_url=base_url) |
| 237 | + try: |
| 238 | + started = await client.sessions.start_captcha_solving( |
| 239 | + "52dd29fb-75a2-43f9-9831-8ff377fedb0a", |
| 240 | + UpdateSessionSolveCaptchasParams(solver_type="visual"), |
| 241 | + ) |
| 242 | + stopped = await client.sessions.stop_captcha_solving( |
| 243 | + "52dd29fb-75a2-43f9-9831-8ff377fedb0a" |
| 244 | + ) |
| 245 | + finally: |
| 246 | + await client.close() |
| 247 | + server.shutdown() |
| 248 | + server.server_close() |
| 249 | + |
| 250 | + assert started.success is True |
| 251 | + assert started.solve_captchas is True |
| 252 | + assert stopped.success is True |
| 253 | + assert stopped.solve_captchas is False |
| 254 | + assert requests == [ |
| 255 | + { |
| 256 | + "method": "PUT", |
| 257 | + "path": "/api/session/52dd29fb-75a2-43f9-9831-8ff377fedb0a/update", |
| 258 | + "api_key": "test-api-key", |
| 259 | + "content_type": "application/json", |
| 260 | + "body": { |
| 261 | + "type": "solveCaptchas", |
| 262 | + "params": { |
| 263 | + "enabled": True, |
| 264 | + "solverType": "visual", |
| 265 | + }, |
| 266 | + }, |
| 267 | + }, |
| 268 | + { |
| 269 | + "method": "PUT", |
| 270 | + "path": "/api/session/52dd29fb-75a2-43f9-9831-8ff377fedb0a/update", |
| 271 | + "api_key": "test-api-key", |
| 272 | + "content_type": "application/json", |
| 273 | + "body": { |
| 274 | + "type": "solveCaptchas", |
| 275 | + "params": { |
| 276 | + "enabled": False, |
| 277 | + }, |
| 278 | + }, |
| 279 | + }, |
| 280 | + ] |
0 commit comments