Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
"typing-extensions>=4.13.2,<5.0.0",
"uvicorn>=0.34.2",
"websockets>=13.0",
"httpx2>=2.12.0",
]

[tool.hatch.metadata]
Expand Down
30 changes: 30 additions & 0 deletions tests/bedrock_agentcore/runtime/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import time
from datetime import datetime
from decimal import Decimal
from typing import AsyncIterator, TypedDict
from unittest.mock import MagicMock, patch

import httpx2
import pytest
from starlette.testclient import TestClient

Expand Down Expand Up @@ -404,6 +406,34 @@ async def lifespan(app):
assert startup_called is True
assert shutdown_called is True

def test_lifespan_state_access(self):
""" Test that we can access lifespan state """

expected_greeting = "hello world"

class AppState(TypedDict):
greeting: str

@contextlib.asynccontextmanager
async def lifespan(app) -> AsyncIterator[AppState]:
async with httpx2.AsyncClient() as client:
yield { "greeting": expected_greeting }

app = BedrockAgentCoreApp(lifespan=lifespan)

@app.entrypoint
async def invoke(payload, context):
greet = context.request.state["greeting"]
return {"greeting": greet}

with TestClient(app) as client:
response = client.post("/invocations", json={"input": "test"})
assert response.status_code == 200

payload = response.json()
assert "greeting" in payload
assert payload["greeting"] == expected_greeting

def test_initialization_without_lifespan(self):
"""Test that BedrockAgentCoreApp still works without lifespan."""
app = BedrockAgentCoreApp() # No lifespan parameter
Expand Down
Loading