Repository navigation
It would be very helpful to document what the "magic url" "http://testserver" means. #2210
Replies: 1 comment
|
The hostname is not the switch that selects the app. The configured transport does that. With HTTPX 0.28.1, make the in-process transport explicit (the old app= client shortcut has been removed): import asyncio
import httpx
async def app(scope, receive, send):
await send({"type": "http.response.start", "status": 200, "headers": []})
await send({"type": "http.response.body", "body": b"in-process"})
async def main():
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://not-a-real-host.invalid") as client:
response = await client.get("/example")
assert response.content == b"in-process"
asyncio.run(main())The deliberately nonexistent host still reaches app because ASGITransport sends the request directly into that callable, without DNS or a TCP connection. Conversely, a normal Client with only base_url="http://testserver" attempts a network request; that name has no reserved HTTPX routing meaning. The URL is still relevant as the request's scheme/host/path and for host-sensitive middleware, cookie rules, and redirects. Use a base URL appropriate for those test expectations. WSGITransport is the synchronous in-process alternative for WSGI apps. ASGI lifespan startup/shutdown is a separate concern; ASGITransport does not run it automatically. Validated the complete example locally. ASGI transport. Validation: HTTPX 0.28.1, Python 3.11.9. |
Uh oh!
There was an error while loading. Please reload this page.
After a day and a half of debugging my test, I've come to understand that using
http://testserveras abase_urlwhen instantiating a Client or AsyncClient tells the client to query the givenappinstance. Although use of this URL is shown on the Advanced Usage and Async Support pages, there is no discussion or indication of whathttp://testserveractually means. As such, I took the value to be an example that should be replaced with the actual value of my own server's URL. Of course, I understand now thathttp://testservermakes more sense. I just would have would preferred to understand this when I was reading through the documentation the first time.The only reason that I discovered what was going on was because I happened upon this mention here:
fastapi/fastapi#1876 (comment)
All reactions