|
| 1 | +"""Unit tests for ``WrappedBotoClient`` HTTP error surfacing. |
| 2 | +
|
| 3 | +The shim talks to the LLM Gateway over httpx instead of AWS. It must call |
| 4 | +``raise_for_status()`` so gateway HTTP errors (e.g. 403 License-not-available) |
| 5 | +propagate as exceptions, rather than being parsed as a normal result and then |
| 6 | +mis-reported downstream (langchain_aws raises a misleading "No 'output' key" |
| 7 | +``ValueError`` when the response lacks the expected fields). |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | + |
| 12 | +import httpx |
| 13 | +import pytest |
| 14 | +from uipath_langchain_client.clients.bedrock.utils import WrappedBotoClient |
| 15 | + |
| 16 | +_ERROR_BODY = { |
| 17 | + "title": "License not available", |
| 18 | + "status": 403, |
| 19 | + "detail": "License not available for LLM usage.", |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +def _wrapped(handler: object) -> WrappedBotoClient: |
| 24 | + transport = httpx.MockTransport(handler) # type: ignore[arg-type] |
| 25 | + return WrappedBotoClient( |
| 26 | + httpx_client=httpx.Client(transport=transport, base_url="http://gateway") |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def test_converse_raises_on_http_error() -> None: |
| 31 | + client = _wrapped(lambda request: httpx.Response(403, json=_ERROR_BODY)) |
| 32 | + with pytest.raises(httpx.HTTPStatusError): |
| 33 | + client.converse(messages=[{"role": "user", "content": [{"text": "hi"}]}]) |
| 34 | + |
| 35 | + |
| 36 | +def test_converse_returns_body_on_success() -> None: |
| 37 | + payload = {"output": {"message": {"role": "assistant", "content": [{"text": "ok"}]}}} |
| 38 | + client = _wrapped(lambda request: httpx.Response(200, json=payload)) |
| 39 | + assert client.converse(messages=[]) == payload |
| 40 | + |
| 41 | + |
| 42 | +def test_invoke_model_raises_on_http_error() -> None: |
| 43 | + client = _wrapped(lambda request: httpx.Response(403, json=_ERROR_BODY)) |
| 44 | + with pytest.raises(httpx.HTTPStatusError): |
| 45 | + client.invoke_model(body=json.dumps({"prompt": "hi"})) |
| 46 | + |
| 47 | + |
| 48 | +def test_converse_stream_raises_on_http_error() -> None: |
| 49 | + # The generator defers work until iterated, so the error surfaces on consume. |
| 50 | + client = _wrapped(lambda request: httpx.Response(403, json=_ERROR_BODY)) |
| 51 | + stream = client.converse_stream(messages=[])["stream"] |
| 52 | + with pytest.raises(httpx.HTTPStatusError): |
| 53 | + list(stream) |
| 54 | + |
| 55 | + |
| 56 | +def test_invoke_model_with_response_stream_raises_on_http_error() -> None: |
| 57 | + client = _wrapped(lambda request: httpx.Response(403, json=_ERROR_BODY)) |
| 58 | + stream = client.invoke_model_with_response_stream(body=json.dumps({"prompt": "hi"}))["body"] |
| 59 | + with pytest.raises(httpx.HTTPStatusError): |
| 60 | + list(stream) |
0 commit comments