New Features
Public Headers API for AsyncTestResponse
The AsyncTestResponse class now provides a public headers property, eliminating the need to access the protected _response member for header retrieval.
Before
# Previously required accessing protected member
location = response._response.headers.get("location")After
# Clean public API
location = response.headers.get("location")Improvements
- Better API Design: Headers are now accessible through a clean, public property interface
- Consistent Error Handling: Attempting to access headers on WebSocket connections raises
InvalidResponseTypeErrorwith a clear message - Case-Insensitive Access: Headers can be accessed case-insensitively through
httpx.Headersinterface
Testing
- Added comprehensive tests for HTTP response header access
- Added tests for redirect responses with Location header
- Added WebSocket error handling tests to ensure proper exceptions are raised
Example Usage
async with create_test_server() as server:
@server.app.post("/login")
async def login_endpoint():
return Response(
status_code=302,
headers={"Location": "/dashboard"}
)
response = await server.client.post("/login", follow_redirects=False)
# Access headers directly
assert response.status_code == 302
assert response.headers["Location"] == "/dashboard"
# Case-insensitive access
location = response.headers.get("location") # Works!Breaking Changes
None - this is a backwards-compatible enhancement.
Full Changelog: v0.3.1...v0.3.2