TestClient.request does not honor timeout
See original GitHub issueChecklist
- The bug is reproducible against the latest release and/or
master
. - There are no similar issues or pull requests to fix it yet.
Describe the bug
timeout
parameter exposed in testclient.request
has no effect. Looking through the code, it seems as though we use the same interface as requests
package but only a subset of those features are supported by testclient
To reproduce
import time
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.testclient import TestClient
mock_service = Starlette()
@mock_service.route("/slow_response")
def slow_response(request):
time.sleep(0.01)
return JSONResponse({"mock": "slow example"})
def test_timeout():
""" no timeout exception raised -- this test passes when it should fail """
client = TestClient(mock_service, raise_server_exceptions=True)
response = client.get("/slow_response", timeout=0.001)
assert response.json() == {"mock": "slow example"}
Expected behavior
A TimeoutException should be raised if the timeout specified in request is shorter than the time the request takes.
Actual behavior
No timeout exceptions are raised when setting timeout.
Additional context
In CI/CD context, testing our own endpoints which, during integration testing, may timeout, is very helpful. If this is unsupported, it might be nice to have a list of differences between the interface of requests/testclient (this might include #1102).
I did look a bit how the requests package implemented their timeout exceptions, and to be completely honest I got a bit lost going down the rabbit hole of requests
-> urllib3
-> http.client
. Since we’re using a different adapter for the testclient, it might be some work to reimplement all the way down the timeout exceptions in the same way.
I did notice that the async_asgi_testclient
works with a timeout. Of course, it requires an async endpoint. But their implementation might still be useful?
Found that httpx
had a similar issue with timeout having no effect when mounting an app with its client.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top GitHub Comments
This may help: https://github.com/encode/starlette/commit/25a52fe52a1fa4f55da4096fb0c7e6f6de0ae980
Okay - that comment is a bit brief. But see #1109. Frameworks such as Flask and Django have always had this constraint. It’s something that we can live with as well.