Spaces:
Running
Running
File size: 16,267 Bytes
e435e1c c0bf682 e435e1c b68f023 e435e1c b68f023 c0bf682 e435e1c 44185c4 e435e1c 44185c4 c0bf682 44185c4 c0bf682 46de0de 44185c4 c0bf682 44185c4 c0bf682 44185c4 e435e1c b68f023 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | """Tests for ratelimit header forwarding in streaming responses.
Verifies that anthropic-ratelimit-* headers from the upstream API response
are forwarded to the client in StreamingResponse, even in SSE streaming mode.
This was a bug where non-streaming responses correctly forwarded all headers
via dict(response.headers), but streaming responses used StreamingResponse
without passing any upstream headers — silently dropping ratelimit info.
"""
import json
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
import headroom.proxy.handlers.streaming as streaming_module
from headroom.proxy.server import HeadroomProxy
class TestStreamingRatelimitHeaderForwarding:
"""Test that upstream ratelimit headers are forwarded in streaming responses."""
def _create_mock_proxy(self):
"""Create a HeadroomProxy with mocked internals for unit testing."""
proxy = object.__new__(HeadroomProxy)
proxy.http_client = MagicMock(spec=httpx.AsyncClient)
proxy.metrics = MagicMock()
proxy.metrics.record_request = AsyncMock(return_value=None)
proxy.metrics.record_failed = AsyncMock(return_value=None)
proxy.cost_tracker = MagicMock()
proxy.cost_tracker.estimate_cost.return_value = 0.001
proxy.cost_tracker.record_request.return_value = None
proxy.stats = {
"requests_total": 0,
"requests_optimized": 0,
"tokens": {"original": 0, "optimized": 0, "saved": 0},
"cost": {"total_usd": 0, "savings_usd": 0},
"errors": 0,
"active_requests": 0,
"requests_per_model": {},
}
proxy.memory_manager = None
proxy._config = MagicMock()
proxy._config.memory_enabled = False
proxy._config.ccr_inject_tool = False
proxy._config.retry_max_attempts = 3
proxy._config.retry_base_delay_ms = 0
proxy._config.retry_max_delay_ms = 0
proxy.config = proxy._config
proxy._parse_sse_usage_from_buffer = MagicMock(return_value=None)
proxy.memory_handler = None
return proxy
def _create_mock_upstream_response(self, extra_headers=None):
"""Create a mock httpx streaming response with ratelimit headers."""
mock_response = AsyncMock()
headers = {
"content-type": "text/event-stream",
"anthropic-ratelimit-tokens-limit": "80000",
"anthropic-ratelimit-tokens-remaining": "75000",
"anthropic-ratelimit-tokens-reset": "2026-03-25T12:00:00Z",
"anthropic-ratelimit-requests-limit": "60",
"anthropic-ratelimit-requests-remaining": "59",
"anthropic-ratelimit-requests-reset": "2026-03-25T12:00:00Z",
"anthropic-ratelimit-input-tokens-limit": "50000",
"anthropic-ratelimit-input-tokens-remaining": "48000",
"anthropic-ratelimit-input-tokens-reset": "2026-03-25T12:00:00Z",
"anthropic-ratelimit-output-tokens-limit": "30000",
"anthropic-ratelimit-output-tokens-remaining": "27000",
"anthropic-ratelimit-output-tokens-reset": "2026-03-25T12:00:00Z",
# Non-ratelimit headers that should NOT be forwarded
"x-request-id": "req-12345",
"cf-ray": "abc123",
}
if extra_headers:
headers.update(extra_headers)
mock_response.headers = httpx.Headers(headers)
mock_response.status_code = 200
# Simulate a simple SSE stream
sse_data = (
b'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_01"}}\n\n'
b'event: message_stop\ndata: {"type":"message_stop"}\n\n'
)
async def aiter_bytes():
yield sse_data
mock_response.aiter_bytes = aiter_bytes
mock_response.aclose = AsyncMock()
return mock_response
@pytest.mark.asyncio
async def test_ratelimit_headers_forwarded_in_streaming(self):
"""Ratelimit headers from upstream should appear in the StreamingResponse."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
# Mock build_request + send to return our mock response
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(return_value=mock_response)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test", "anthropic-version": "2023-06-01"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-123",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
# Verify ratelimit headers are present in the StreamingResponse
assert result.headers.get("anthropic-ratelimit-tokens-limit") == "80000"
assert result.headers.get("anthropic-ratelimit-tokens-remaining") == "75000"
assert result.headers.get("anthropic-ratelimit-tokens-reset") == "2026-03-25T12:00:00Z"
assert result.headers.get("anthropic-ratelimit-requests-limit") == "60"
assert result.headers.get("anthropic-ratelimit-requests-remaining") == "59"
assert result.headers.get("anthropic-ratelimit-input-tokens-limit") == "50000"
assert result.headers.get("anthropic-ratelimit-output-tokens-limit") == "30000"
@pytest.mark.asyncio
async def test_non_ratelimit_headers_not_forwarded(self):
"""Only ratelimit headers should be forwarded, not arbitrary upstream headers."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(return_value=mock_response)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-456",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
# Non-ratelimit headers should NOT be in the response
assert result.headers.get("x-request-id") is None
assert result.headers.get("cf-ray") is None
@pytest.mark.asyncio
async def test_no_ratelimit_headers_still_works(self):
"""When upstream has no ratelimit headers, streaming should still work."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
# Remove all ratelimit headers
mock_response.headers = httpx.Headers(
{
"content-type": "text/event-stream",
"x-request-id": "req-999",
}
)
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(return_value=mock_response)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-789",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
# Should still return a valid StreamingResponse
assert result.media_type == "text/event-stream"
# No ratelimit headers to forward
assert result.headers.get("anthropic-ratelimit-tokens-limit") is None
@pytest.mark.asyncio
async def test_upstream_http_error_preserves_status_body_and_metrics(self, monkeypatch):
"""Upstream non-200 streaming responses should preserve status/body and metrics."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
mock_response.status_code = 503
mock_response.headers = httpx.Headers(
{
"content-type": "application/json",
"content-encoding": "gzip",
"content-length": "42",
}
)
mock_response.aread = AsyncMock(return_value=b'{"error":{"message":"capacity exhausted"}}')
mock_response.aclose = AsyncMock()
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(return_value=mock_response)
fake_logger = MagicMock()
monkeypatch.setattr(streaming_module, "logger", fake_logger)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-http-error",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
assert result.status_code == 503
assert result.body == b'{"error":{"message":"capacity exhausted"}}'
assert result.headers.get("content-encoding") is None
fake_logger.warning.assert_any_call(
"[%s] Forwarding upstream streaming error status=%s url=%s",
"test-http-error",
503,
"https://api.anthropic.com/v1/messages",
)
proxy.metrics.record_request.assert_awaited_once()
proxy.cost_tracker.record_tokens.assert_called_once()
mock_response.aclose.assert_awaited_once()
@pytest.mark.asyncio
async def test_upstream_http_error_closes_response_when_body_read_fails(self, monkeypatch):
"""Reading a streaming error body should still close the upstream response."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
mock_response.status_code = 502
mock_response.aread = AsyncMock(side_effect=RuntimeError("boom"))
mock_response.aclose = AsyncMock()
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(return_value=mock_response)
fake_logger = MagicMock()
monkeypatch.setattr(streaming_module, "logger", fake_logger)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-http-error-read-fail",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
assert result.status_code == 502
assert result.headers.get("content-type") == "application/json"
assert b"Failed to read upstream error response body" in result.body
fake_logger.warning.assert_any_call(
"[%s] Failed reading upstream error body status=%s url=%s error=%s",
"test-http-error-read-fail",
502,
"https://api.anthropic.com/v1/messages",
mock_response.aread.side_effect,
)
mock_response.aclose.assert_awaited_once()
@pytest.mark.asyncio
async def test_connect_error_returns_sse_error(self):
"""Connection errors should return an SSE error event (not crash)."""
proxy = self._create_mock_proxy()
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
proxy.http_client.send = AsyncMock(side_effect=httpx.ConnectError("Connection refused"))
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-error",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
# Should return a StreamingResponse with error SSE event
assert result.media_type == "text/event-stream"
# Consume the generator to get the error event
chunks = []
async for chunk in result.body_iterator:
chunks.append(chunk)
assert len(chunks) == 1
raw = chunks[0].decode("utf-8")
assert "event: error" in raw
error_data = json.loads(raw.split("data: ")[1].strip())
assert error_data["error"]["type"] == "connection_error"
assert "Connection refused" in error_data["error"]["message"]
@pytest.mark.asyncio
async def test_connect_timeout_retries_before_returning_stream(self):
"""Transient connect timeouts should retry before failing the stream."""
proxy = self._create_mock_proxy()
mock_response = self._create_mock_upstream_response()
mock_request = MagicMock()
proxy.http_client.build_request = MagicMock(return_value=mock_request)
attempts = {"count": 0}
async def flaky_send(*args, **kwargs):
attempts["count"] += 1
if attempts["count"] == 1:
raise httpx.ConnectTimeout("timed out")
return mock_response
proxy.http_client.send = AsyncMock(side_effect=flaky_send)
result = await proxy._stream_response(
url="https://api.anthropic.com/v1/messages",
headers={"x-api-key": "sk-test"},
body={
"model": "claude-sonnet-4-20250514",
"max_tokens": 100,
"stream": True,
"messages": [{"role": "user", "content": "hi"}],
},
provider="anthropic",
model="claude-sonnet-4-20250514",
request_id="test-retry",
original_tokens=10,
optimized_tokens=10,
tokens_saved=0,
transforms_applied=[],
tags={},
optimization_latency=0.0,
)
chunks = []
async for chunk in result.body_iterator:
chunks.append(chunk)
assert attempts["count"] == 2
assert chunks
|