|
| 1 | +"""Tests for opt-in passthrough compression (issue #1546). |
| 2 | +
|
| 3 | +Requests whose path doesn't match a built-in API route fall through to |
| 4 | +``handle_passthrough``, which historically forwarded the body verbatim — no |
| 5 | +compression. With ``compress_passthrough`` enabled, OpenAI Responses-shaped |
| 6 | +bodies (path ends in ``/responses``) are routed through the same |
| 7 | +ContentRouter/Kompress path the native ``/v1/responses`` handler uses. |
| 8 | +
|
| 9 | +``_maybe_compress_passthrough_responses`` is the fail-open core: any parse or |
| 10 | +compressor failure returns the original body so a catch-all request is never |
| 11 | +dropped by opting into compression. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import json |
| 17 | +from types import SimpleNamespace |
| 18 | + |
| 19 | +from headroom.proxy.handlers.openai import OpenAIHandlerMixin |
| 20 | + |
| 21 | + |
| 22 | +def _make_handler(compress_impl): |
| 23 | + """Bare mixin instance with just the two collaborators the helper needs.""" |
| 24 | + handler = OpenAIHandlerMixin.__new__(OpenAIHandlerMixin) |
| 25 | + handler.config = SimpleNamespace(optimize=True, compress_passthrough=True) |
| 26 | + |
| 27 | + async def _next_request_id(): |
| 28 | + return "req-test" |
| 29 | + |
| 30 | + handler._next_request_id = _next_request_id |
| 31 | + handler._compress_openai_responses_payload_in_executor = compress_impl |
| 32 | + return handler |
| 33 | + |
| 34 | + |
| 35 | +def _shrinking_compressor(marker: str = "[C]"): |
| 36 | + async def _impl(payload, *, model, request_id): |
| 37 | + new = dict(payload) |
| 38 | + new["input"] = marker |
| 39 | + return (new, True, 5, ["kompress"], None, 100, 40, 5, {}) |
| 40 | + |
| 41 | + return _impl |
| 42 | + |
| 43 | + |
| 44 | +async def test_compresses_responses_shaped_body() -> None: |
| 45 | + handler = _make_handler(_shrinking_compressor()) |
| 46 | + body = json.dumps({"model": "gpt-5.4", "input": [{"role": "user"}]}).encode() |
| 47 | + |
| 48 | + out = await handler._maybe_compress_passthrough_responses(body) |
| 49 | + |
| 50 | + assert out != body |
| 51 | + assert json.loads(out)["input"] == "[C]" |
| 52 | + |
| 53 | + |
| 54 | +async def test_non_json_body_passes_through() -> None: |
| 55 | + handler = _make_handler(_shrinking_compressor()) |
| 56 | + body = b"not json at all" |
| 57 | + |
| 58 | + assert await handler._maybe_compress_passthrough_responses(body) == body |
| 59 | + |
| 60 | + |
| 61 | +async def test_non_responses_payload_passes_through() -> None: |
| 62 | + # No `input` key → not a Responses payload; must not be touched. |
| 63 | + handler = _make_handler(_shrinking_compressor()) |
| 64 | + body = json.dumps({"model": "gpt-5.4", "messages": []}).encode() |
| 65 | + |
| 66 | + assert await handler._maybe_compress_passthrough_responses(body) == body |
| 67 | + |
| 68 | + |
| 69 | +async def test_unmodified_result_returns_original_bytes() -> None: |
| 70 | + async def _noop(payload, *, model, request_id): |
| 71 | + return (payload, False, 0, [], "no-op", 0, 0, 0, {}) |
| 72 | + |
| 73 | + handler = _make_handler(_noop) |
| 74 | + body = json.dumps({"input": [{"role": "user"}]}).encode() |
| 75 | + |
| 76 | + assert await handler._maybe_compress_passthrough_responses(body) == body |
| 77 | + |
| 78 | + |
| 79 | +async def test_compressor_error_fails_open() -> None: |
| 80 | + async def _boom(payload, *, model, request_id): |
| 81 | + raise RuntimeError("kompress exploded") |
| 82 | + |
| 83 | + handler = _make_handler(_boom) |
| 84 | + body = json.dumps({"input": [{"role": "user"}]}).encode() |
| 85 | + |
| 86 | + # Fail-open: original body forwarded, exception swallowed. |
| 87 | + assert await handler._maybe_compress_passthrough_responses(body) == body |
| 88 | + |
| 89 | + |
| 90 | +def test_config_defaults_off() -> None: |
| 91 | + from headroom.proxy.models import ProxyConfig |
| 92 | + |
| 93 | + assert ProxyConfig().compress_passthrough is False |
0 commit comments