Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : SPIDER — Universal Web Intelligence Engine7# File : tests/unit/sdk/sdk_testkit.py8# Module : Tests9# Repository : https://git.spboucher.ai/spider10# Project Website : Not specified11# Created : 2026-12-0412# Last Modified : 2026-12-0413#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development : dev@spboucher.ai23# Support : support@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Scripted HTTP transport shared by the unit tests of the Python SDK."""42import json43import sys44from pathlib import Path4546import httpx4748ROOT = Path(__file__).resolve().parents[3]49sys.path.insert(0, str(ROOT / "sdk" / "python"))5051from spider_sdk import AsyncSpider5253API_KEY = "sk_test_unit_0123456789"545556class Script:57 """A scripted HTTP server: each request pops the next step (a response, an exception or a callable)."""5859 def __init__(self, *steps):60 self.steps = list(steps)61 self.requests: list[httpx.Request] = []62 self.sleeps: list[float] = []6364 def handler(self, request: httpx.Request) -> httpx.Response:65 self.requests.append(request)66 assert self.steps, f"unexpected request {request.method} {request.url}"67 step = self.steps.pop(0)68 if callable(step):69 step = step(request)70 if isinstance(step, Exception):71 raise step72 return step7374 async def sleep(self, seconds: float) -> None:75 self.sleeps.append(seconds)7677 def client(self, **kw) -> AsyncSpider:78 return AsyncSpider("http://spider.test", API_KEY, transport=httpx.MockTransport(self.handler),79 sleep=self.sleep, **kw)8081 def bodies(self) -> list:82 return [json.loads(r.content) if r.content else None for r in self.requests]838485def ok(body=None, status=200, **kw) -> httpx.Response:86 return httpx.Response(status, json={} if body is None else body, **kw)878889def err(status, code, message="nope", details=None, headers=None) -> httpx.Response:90 return httpx.Response(status, headers={"X-Request-Id": "rid-1", **(headers or {})}, json={91 "error": {"code": code, "message": message, "details": details, "request_id": "rid-1"}})92