sanic/tests/test_request_timeout.py

102 lines
3.0 KiB
Python
Raw Normal View History

import asyncio
2020-09-27 00:58:36 +01:00
import httpcore
import httpx
2021-04-05 16:01:48 +01:00
from sanic_testing.testing import SanicTestClient
2020-09-27 00:58:36 +01:00
from sanic import Sanic
from sanic.response import text
2020-09-27 00:58:36 +01:00
class DelayableHTTPConnection(httpcore._async.connection.AsyncHTTPConnection):
async def arequest(self, *args, **kwargs):
await asyncio.sleep(2)
return await super().arequest(*args, **kwargs)
2020-09-27 00:58:36 +01:00
async def _open_socket(self, *args, **kwargs):
retval = await super()._open_socket(*args, **kwargs)
if self._request_delay:
await asyncio.sleep(self._request_delay)
2020-09-27 00:58:36 +01:00
return retval
2020-09-27 00:58:36 +01:00
class DelayableSanicConnectionPool(httpcore.AsyncConnectionPool):
def __init__(self, request_delay=None, *args, **kwargs):
self._request_delay = request_delay
super().__init__(*args, **kwargs)
2020-09-27 00:58:36 +01:00
async def _add_to_pool(self, connection, timeout):
connection.__class__ = DelayableHTTPConnection
connection._request_delay = self._request_delay
await super()._add_to_pool(connection, timeout)
class DelayableSanicSession(httpx.AsyncClient):
def __init__(self, request_delay=None, *args, **kwargs) -> None:
2020-09-27 00:58:36 +01:00
transport = DelayableSanicConnectionPool(request_delay=request_delay)
super().__init__(transport=transport, *args, **kwargs)
class DelayableSanicTestClient(SanicTestClient):
def __init__(self, app, request_delay=None):
super().__init__(app)
self._request_delay = request_delay
self._loop = None
def get_new_session(self):
return DelayableSanicSession(request_delay=self._request_delay)
2018-12-30 11:18:06 +00:00
request_timeout_default_app = Sanic("test_request_timeout_default")
request_no_timeout_app = Sanic("test_request_no_timeout")
request_timeout_default_app.config.REQUEST_TIMEOUT = 0.6
request_no_timeout_app.config.REQUEST_TIMEOUT = 0.6
2018-12-30 11:18:06 +00:00
@request_timeout_default_app.route("/1")
async def handler1(request):
2018-12-30 11:18:06 +00:00
return text("OK")
2018-12-30 11:18:06 +00:00
@request_no_timeout_app.route("/1")
async def handler2(request):
2018-12-30 11:18:06 +00:00
return text("OK")
2018-12-30 11:18:06 +00:00
@request_timeout_default_app.websocket("/ws1")
async def ws_handler1(request, ws):
2018-12-30 11:18:06 +00:00
await ws.send("OK")
def test_default_server_error_request_timeout():
client = DelayableSanicTestClient(request_timeout_default_app, 2)
2018-12-30 11:18:06 +00:00
request, response = client.get("/1")
assert response.status == 408
assert "Request Timeout" in response.text
def test_default_server_error_request_dont_timeout():
client = DelayableSanicTestClient(request_no_timeout_app, 0.2)
2018-12-30 11:18:06 +00:00
request, response = client.get("/1")
assert response.status == 200
2018-12-30 11:18:06 +00:00
assert response.text == "OK"
def test_default_server_error_websocket_request_timeout():
2018-12-30 11:18:06 +00:00
headers = {
"Upgrade": "websocket",
"Connection": "upgrade",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version": "13",
}
client = DelayableSanicTestClient(request_timeout_default_app, 2)
2018-12-30 11:18:06 +00:00
request, response = client.get("/ws1", headers=headers)
assert response.status == 408
assert "Request Timeout" in response.text