|
1 | 1 | import uuid |
2 | 2 |
|
| 3 | +import httpx |
3 | 4 | import pytest |
4 | 5 | import respx |
5 | 6 | from httpx import Response |
6 | 7 |
|
7 | 8 | from zodiac_core.context import set_request_id |
8 | | -from zodiac_core.http import ZodiacClient, ZodiacSyncClient, init_http_client |
| 9 | +from zodiac_core.exceptions import UpstreamRequestError, UpstreamServiceError |
| 10 | +from zodiac_core.http import ( |
| 11 | + ZodiacClient, |
| 12 | + ZodiacSyncClient, |
| 13 | + init_http_client, |
| 14 | + translate_upstream_errors, |
| 15 | +) |
9 | 16 |
|
10 | 17 |
|
11 | 18 | class TestZodiacHttpClients: |
@@ -128,3 +135,130 @@ async def custom_hook(request): |
128 | 135 | headers = mock.calls.last.request.headers |
129 | 136 | assert headers["X-Request-ID"] == trace_id |
130 | 137 | assert headers["X-Resource"] == "yes" |
| 138 | + |
| 139 | + @pytest.mark.asyncio |
| 140 | + async def test_translate_upstream_errors_maps_async_422_to_request_error(self): |
| 141 | + """HTTP 400/422 status errors are treated as upstream request failures.""" |
| 142 | + |
| 143 | + async with respx.mock(base_url="http://test") as mock: |
| 144 | + mock.get("/invalid").mock(return_value=Response(422, json={"code": 422})) |
| 145 | + |
| 146 | + async with ZodiacClient(base_url="http://test") as client: |
| 147 | + |
| 148 | + @translate_upstream_errors(service="identity_and_access") |
| 149 | + async def fetch_invalid(): |
| 150 | + response = await client.get("/invalid") |
| 151 | + response.raise_for_status() |
| 152 | + |
| 153 | + with pytest.raises(UpstreamRequestError) as exc_info: |
| 154 | + await fetch_invalid() |
| 155 | + |
| 156 | + exc = exc_info.value |
| 157 | + assert exc.service == "identity_and_access" |
| 158 | + assert exc.error_code == "UPSTREAM_REQUEST_ERROR" |
| 159 | + assert exc.upstream_status == 422 |
| 160 | + |
| 161 | + @pytest.mark.asyncio |
| 162 | + async def test_translate_upstream_errors_maps_async_5xx_to_service_error(self): |
| 163 | + """Non-contract HTTP status errors are treated as upstream service failures.""" |
| 164 | + |
| 165 | + async with respx.mock(base_url="http://test") as mock: |
| 166 | + mock.get("/unavailable").mock(return_value=Response(503, json={"code": 503})) |
| 167 | + |
| 168 | + async with ZodiacClient(base_url="http://test") as client: |
| 169 | + |
| 170 | + @translate_upstream_errors(service="production") |
| 171 | + async def fetch_unavailable(): |
| 172 | + response = await client.get("/unavailable") |
| 173 | + response.raise_for_status() |
| 174 | + |
| 175 | + with pytest.raises(UpstreamServiceError) as exc_info: |
| 176 | + await fetch_unavailable() |
| 177 | + |
| 178 | + exc = exc_info.value |
| 179 | + assert not isinstance(exc, UpstreamRequestError) |
| 180 | + assert exc.service == "production" |
| 181 | + assert exc.error_code == "UPSTREAM_SERVICE_ERROR" |
| 182 | + assert exc.upstream_status == 503 |
| 183 | + |
| 184 | + def test_translate_upstream_errors_maps_sync_transport_error(self): |
| 185 | + """Transport failures are treated as upstream service failures.""" |
| 186 | + |
| 187 | + @translate_upstream_errors(service="deliverable_hub") |
| 188 | + def fetch_with_transport_failure(): |
| 189 | + request = httpx.Request("GET", "http://deliverable-hub.test") |
| 190 | + raise httpx.ConnectError("connect failed", request=request) |
| 191 | + |
| 192 | + with pytest.raises(UpstreamServiceError) as exc_info: |
| 193 | + fetch_with_transport_failure() |
| 194 | + |
| 195 | + exc = exc_info.value |
| 196 | + assert exc.service == "deliverable_hub" |
| 197 | + assert exc.error_code == "UPSTREAM_SERVICE_ERROR" |
| 198 | + assert exc.upstream_status is None |
| 199 | + |
| 200 | + def test_translate_upstream_errors_maps_sync_request_error(self): |
| 201 | + """Non-transport request failures are also treated as upstream service failures.""" |
| 202 | + |
| 203 | + @translate_upstream_errors(service="redirecting_service") |
| 204 | + def fetch_with_request_failure(): |
| 205 | + request = httpx.Request("GET", "http://redirecting-service.test") |
| 206 | + raise httpx.TooManyRedirects("too many redirects", request=request) |
| 207 | + |
| 208 | + with pytest.raises(UpstreamServiceError) as exc_info: |
| 209 | + fetch_with_request_failure() |
| 210 | + |
| 211 | + exc = exc_info.value |
| 212 | + assert exc.service == "redirecting_service" |
| 213 | + assert exc.error_code == "UPSTREAM_SERVICE_ERROR" |
| 214 | + assert exc.upstream_status is None |
| 215 | + |
| 216 | + def test_translate_upstream_errors_preserves_local_exception_handling(self): |
| 217 | + """If user code catches the httpx error itself, the decorator does not interfere.""" |
| 218 | + |
| 219 | + @translate_upstream_errors(service="billing") |
| 220 | + def fetch_with_local_handling(): |
| 221 | + request = httpx.Request("GET", "http://billing.test") |
| 222 | + try: |
| 223 | + raise httpx.ConnectError("connect failed", request=request) |
| 224 | + except httpx.ConnectError: |
| 225 | + return {"handled": True} |
| 226 | + |
| 227 | + assert fetch_with_local_handling() == {"handled": True} |
| 228 | + |
| 229 | + @pytest.mark.asyncio |
| 230 | + async def test_translated_upstream_error_is_handled_by_registered_fastapi_app(self): |
| 231 | + """Decorator + register_exception_handlers is the complete integration path.""" |
| 232 | + from fastapi import FastAPI |
| 233 | + |
| 234 | + from zodiac_core.exception_handlers import register_exception_handlers |
| 235 | + |
| 236 | + app = FastAPI() |
| 237 | + register_exception_handlers(app) |
| 238 | + |
| 239 | + @translate_upstream_errors(service="identity_and_access") |
| 240 | + async def call_upstream(): |
| 241 | + async with ZodiacClient(base_url="http://upstream") as client: |
| 242 | + response = await client.get("/invalid") |
| 243 | + response.raise_for_status() |
| 244 | + |
| 245 | + @app.get("/proxy") |
| 246 | + async def proxy(): |
| 247 | + await call_upstream() |
| 248 | + return {"ok": True} |
| 249 | + |
| 250 | + async with respx.mock(base_url="http://upstream") as mock: |
| 251 | + mock.get("/invalid").mock(return_value=Response(422, json={"code": 422})) |
| 252 | + transport = httpx.ASGITransport(app=app, raise_app_exceptions=False) |
| 253 | + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: |
| 254 | + response = await client.get("/proxy") |
| 255 | + |
| 256 | + assert response.status_code == 400 |
| 257 | + assert response.json() == { |
| 258 | + "code": 400, |
| 259 | + "message": "Upstream request failed", |
| 260 | + "data": { |
| 261 | + "service": "identity_and_access", |
| 262 | + "error_code": "UPSTREAM_REQUEST_ERROR", |
| 263 | + }, |
| 264 | + } |
0 commit comments