|
1 | 1 | import json |
| 2 | +from functools import partial |
2 | 3 | from typing import Any, Dict, Optional |
3 | 4 |
|
4 | 5 | import pytest |
@@ -383,3 +384,37 @@ def run_sql_query(query: Annotated[str, Query()]): |
383 | 384 | # THEN the parameter with commas should be correctly passed to the handler |
384 | 385 | body = json.loads(result["response"]["responseBody"]["application/json"]["body"]) |
385 | 386 | assert body["result"] == "SELECT a.source_name, b.thing FROM table" |
| 387 | + |
| 388 | + |
| 389 | +def test_bedrock_agent_with_default_serializer_escapes_non_ascii(): |
| 390 | + # GIVEN a Bedrock Agent resolver using the default serializer |
| 391 | + app = BedrockAgentResolver() |
| 392 | + |
| 393 | + @app.get("/claims", description="Gets claims") |
| 394 | + def claims() -> Dict[str, Any]: |
| 395 | + return {"output": "잔액은 1,000원입니다 💰"} |
| 396 | + |
| 397 | + # WHEN calling the event handler |
| 398 | + result = app(load_event("bedrockAgentEvent.json"), {}) |
| 399 | + |
| 400 | + # THEN the body is valid JSON and non-ASCII characters are escaped (default json.dumps behavior) |
| 401 | + body = result["response"]["responseBody"]["application/json"]["body"] |
| 402 | + assert "\\uc794" in body # "잔" escaped |
| 403 | + assert json.loads(body) == {"output": "잔액은 1,000원입니다 💰"} |
| 404 | + |
| 405 | + |
| 406 | +def test_bedrock_agent_with_custom_serializer_preserves_non_ascii(): |
| 407 | + # GIVEN a Bedrock Agent resolver initialized with a custom serializer that keeps non-ASCII characters |
| 408 | + app = BedrockAgentResolver(serializer=partial(json.dumps, ensure_ascii=False)) |
| 409 | + |
| 410 | + @app.get("/claims", description="Gets claims") |
| 411 | + def claims() -> Dict[str, Any]: |
| 412 | + return {"output": "잔액은 1,000원입니다 💰"} |
| 413 | + |
| 414 | + # WHEN calling the event handler |
| 415 | + result = app(load_event("bedrockAgentEvent.json"), {}) |
| 416 | + |
| 417 | + # THEN the non-ASCII characters are preserved verbatim in the response body |
| 418 | + body = result["response"]["responseBody"]["application/json"]["body"] |
| 419 | + assert "잔액은 1,000원입니다 💰" in body |
| 420 | + assert json.loads(body) == {"output": "잔액은 1,000원입니다 💰"} |
0 commit comments