File size: 1,336 Bytes
fa85955
 
 
2ed2bd7
fa85955
2ed2bd7
 
fa85955
 
2ed2bd7
fa85955
 
 
 
 
 
 
 
8ca285d
2ed2bd7
8ca285d
2ed2bd7
8ca285d
2ed2bd7
 
 
 
8ca285d
 
 
 
fa85955
 
 
 
 
 
 
2ed2bd7
 
 
fa85955
 
2ed2bd7
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
"""
Tests for error handling and request id propagation.
"""

from unittest.mock import patch

import pytest
from starlette.testclient import TestClient

from app.main import app
from tests.test_services import StubAsyncClient

client = TestClient(app)


@pytest.mark.integration
def test_httpx_error_returns_502():
    """Test that httpx errors return 502 status."""
    import httpx

    from tests.test_services import StubAsyncClient

    # Mock httpx to raise HTTPError
    with patch(
        "httpx.AsyncClient",
        return_value=StubAsyncClient(post_exc=httpx.HTTPError("Connection failed")),
    ):
        resp = client.post("/api/v1/summarize/", json={"text": "hi"})
        assert resp.status_code == 502
        data = resp.json()
        assert "Summarization failed" in data["detail"]


def test_request_id_header_propagated(sample_text, mock_ollama_response):
    """Verify X-Request-ID appears in response headers."""
    from tests.test_services import StubAsyncResponse

    stub_response = StubAsyncResponse(json_data=mock_ollama_response)
    with patch(
        "httpx.AsyncClient", return_value=StubAsyncClient(post_result=stub_response)
    ):
        resp = client.post("/api/v1/summarize/", json={"text": sample_text})
        assert resp.status_code == 200
        assert resp.headers.get("X-Request-ID")