0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
143 lines
4.9 KiB
Python
143 lines
4.9 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
import httpx
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.testclient import TestClient
|
|
|
|
from headroom.providers.openai_responses import (
|
|
OPENAI_RESPONSES_ROOT_PATHS,
|
|
OPENAI_RESPONSES_SUBPATH_ROUTES,
|
|
OPENAI_RESPONSES_WEBSOCKET_PATHS,
|
|
OpenAIResponsesSubpathRoute,
|
|
handle_openai_responses_subpath,
|
|
normalize_openai_responses_headers,
|
|
openai_responses_subpath_url,
|
|
)
|
|
|
|
|
|
def test_openai_responses_route_aliases_are_explicit() -> None:
|
|
assert OPENAI_RESPONSES_ROOT_PATHS == (
|
|
"/v1/responses",
|
|
"/v1/codex/responses",
|
|
"/backend-api/responses",
|
|
"/backend-api/codex/responses",
|
|
)
|
|
assert OPENAI_RESPONSES_WEBSOCKET_PATHS == OPENAI_RESPONSES_ROOT_PATHS
|
|
assert OPENAI_RESPONSES_SUBPATH_ROUTES == (
|
|
OpenAIResponsesSubpathRoute("/v1/responses/{sub_path:path}", ("GET", "POST", "DELETE")),
|
|
OpenAIResponsesSubpathRoute(
|
|
"/v1/codex/responses/{sub_path:path}",
|
|
("GET", "POST", "DELETE"),
|
|
),
|
|
OpenAIResponsesSubpathRoute(
|
|
"/backend-api/responses/{sub_path:path}",
|
|
("GET", "POST", "DELETE"),
|
|
),
|
|
OpenAIResponsesSubpathRoute(
|
|
"/backend-api/codex/responses/{sub_path:path}",
|
|
("GET", "POST", "DELETE"),
|
|
),
|
|
)
|
|
|
|
|
|
def test_openai_responses_route_aliases_are_unique() -> None:
|
|
root_keys = {("POST", path) for path in OPENAI_RESPONSES_ROOT_PATHS}
|
|
websocket_keys = {("WS", path) for path in OPENAI_RESPONSES_WEBSOCKET_PATHS}
|
|
subpath_keys = {
|
|
(method, spec.path) for spec in OPENAI_RESPONSES_SUBPATH_ROUTES for method in spec.methods
|
|
}
|
|
|
|
assert len(root_keys) == len(OPENAI_RESPONSES_ROOT_PATHS)
|
|
assert len(websocket_keys) == len(OPENAI_RESPONSES_WEBSOCKET_PATHS)
|
|
assert len(subpath_keys) == sum(len(spec.methods) for spec in OPENAI_RESPONSES_SUBPATH_ROUTES)
|
|
|
|
|
|
def test_openai_responses_subpath_url_includes_optional_query() -> None:
|
|
assert (
|
|
openai_responses_subpath_url(
|
|
"https://api.openai.example/",
|
|
"items/resp_1",
|
|
"trace=2",
|
|
)
|
|
== "https://api.openai.example/v1/responses/items/resp_1?trace=2"
|
|
)
|
|
assert (
|
|
openai_responses_subpath_url("https://api.openai.example", "compact")
|
|
== "https://api.openai.example/v1/responses/compact"
|
|
)
|
|
|
|
|
|
def test_normalize_openai_responses_headers_drops_host() -> None:
|
|
assert normalize_openai_responses_headers(
|
|
{"host": "localhost:8000", "authorization": "Bearer test"}
|
|
) == {"authorization": "Bearer test"}
|
|
|
|
|
|
def test_handle_openai_responses_subpath_forwards_body_headers_and_query() -> None:
|
|
class FakeAsyncClient:
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple[str, str, dict[str, object]]] = []
|
|
|
|
async def request(self, method: str, url: str, **kwargs: object) -> httpx.Response:
|
|
self.calls.append((method, url, kwargs))
|
|
return httpx.Response(202, json={"ok": True}, headers={"x-upstream": "yes"})
|
|
|
|
fake = FakeAsyncClient()
|
|
app = FastAPI()
|
|
|
|
@app.post("/probe/{sub_path:path}")
|
|
async def probe(request: Request, sub_path: str):
|
|
return await handle_openai_responses_subpath(
|
|
fake,
|
|
request,
|
|
"https://api.openai.example",
|
|
sub_path,
|
|
)
|
|
|
|
with TestClient(app) as client:
|
|
response = client.post(
|
|
"/probe/items/resp_1?trace=1",
|
|
headers={"Authorization": "Bearer test"},
|
|
json={"model": "gpt-4o"},
|
|
)
|
|
|
|
assert response.status_code == 202
|
|
assert response.headers["x-upstream"] == "yes"
|
|
assert len(fake.calls) == 1
|
|
method, url, kwargs = fake.calls[0]
|
|
assert method == "POST"
|
|
assert url == "https://api.openai.example/v1/responses/items/resp_1?trace=1"
|
|
assert kwargs["headers"]["authorization"] == "Bearer test" # type: ignore[index]
|
|
assert kwargs["content"] == b'{"model":"gpt-4o"}'
|
|
assert kwargs["timeout"] == 120.0
|
|
|
|
|
|
def test_handle_openai_responses_subpath_returns_502_on_failure() -> None:
|
|
class FailingAsyncClient:
|
|
async def request(self, method: str, url: str, **kwargs: object) -> httpx.Response:
|
|
raise RuntimeError(f"boom: {method} {url}")
|
|
|
|
app = FastAPI()
|
|
|
|
@app.delete("/probe/{sub_path:path}")
|
|
async def probe(request: Request, sub_path: str):
|
|
return await handle_openai_responses_subpath(
|
|
FailingAsyncClient(),
|
|
request,
|
|
"https://api.openai.example",
|
|
sub_path,
|
|
)
|
|
|
|
with TestClient(app) as client:
|
|
with patch("headroom.providers.openai_responses.logger") as logger:
|
|
response = client.delete("/probe/items/resp_1?trace=1")
|
|
|
|
assert response.status_code == 502
|
|
assert response.text == "Upstream request failed."
|
|
logger.error.assert_called_once()
|
|
assert "boom: DELETE https://api.openai.example/v1/responses/items/resp_1?trace=1" in str(
|
|
logger.error.call_args
|
|
)
|