2019-04-23 22:44:42 +01:00
|
|
|
import asyncio
|
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
import httpx
|
2017-09-13 01:18:36 +01:00
|
|
|
|
2016-11-26 04:55:45 +00:00
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import text
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.testing import SanicTestClient
|
2017-09-13 01:18:36 +01:00
|
|
|
|
2018-08-16 06:00:23 +01:00
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
class DelayableHTTPConnection(httpx.dispatch.connection.HTTPConnection):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._request_delay = None
|
|
|
|
if "request_delay" in kwargs:
|
|
|
|
self._request_delay = kwargs.pop("request_delay")
|
2019-04-23 22:44:42 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
async def send(self, request, timeout=None):
|
|
|
|
|
|
|
|
if self.connection is None:
|
|
|
|
self.connection = (await self.connect(timeout=timeout))
|
2019-12-21 03:23:52 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
if self._request_delay:
|
|
|
|
await asyncio.sleep(self._request_delay)
|
2019-12-21 03:23:52 +00:00
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
response = await self.connection.send(request, timeout=timeout)
|
2019-12-21 03:23:52 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
class DelayableSanicConnectionPool(
|
|
|
|
httpx.dispatch.connection_pool.ConnectionPool
|
|
|
|
):
|
|
|
|
def __init__(self, request_delay=None, *args, **kwargs):
|
|
|
|
self._request_delay = request_delay
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
async def acquire_connection(self, origin, timeout=None):
|
|
|
|
connection = self.pop_connection(origin)
|
|
|
|
|
|
|
|
if connection is None:
|
|
|
|
pool_timeout = None if timeout is None else timeout.pool_timeout
|
|
|
|
|
|
|
|
await self.max_connections.acquire(timeout=pool_timeout)
|
|
|
|
connection = DelayableHTTPConnection(
|
|
|
|
origin,
|
2020-03-01 19:42:11 +00:00
|
|
|
ssl=self.ssl,
|
2019-12-21 03:23:52 +00:00
|
|
|
backend=self.backend,
|
|
|
|
release_func=self.release_connection,
|
|
|
|
uds=self.uds,
|
|
|
|
request_delay=self._request_delay,
|
|
|
|
)
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
self.active_connections.add(connection)
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
return connection
|
|
|
|
|
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
class DelayableSanicSession(httpx.AsyncClient):
|
2019-04-23 22:44:42 +01:00
|
|
|
def __init__(self, request_delay=None, *args, **kwargs) -> None:
|
2019-12-21 03:23:52 +00:00
|
|
|
dispatch = DelayableSanicConnectionPool(request_delay=request_delay)
|
|
|
|
super().__init__(dispatch=dispatch, *args, **kwargs)
|
2016-11-26 04:55:45 +00:00
|
|
|
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
class DelayableSanicTestClient(SanicTestClient):
|
2019-04-23 22:44:42 +01:00
|
|
|
def __init__(self, app, request_delay=None):
|
|
|
|
super().__init__(app)
|
2017-09-11 08:17:33 +01:00
|
|
|
self._request_delay = request_delay
|
2017-09-13 01:18:36 +01:00
|
|
|
self._loop = None
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
def get_new_session(self):
|
|
|
|
return DelayableSanicSession(request_delay=self._request_delay)
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
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")
|
2018-12-30 18:37:30 +00:00
|
|
|
request_timeout_default_app.config.REQUEST_TIMEOUT = 0.6
|
|
|
|
request_no_timeout_app.config.REQUEST_TIMEOUT = 0.6
|
2016-11-26 04:55:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@request_timeout_default_app.route("/1")
|
2017-09-13 01:18:36 +01:00
|
|
|
async def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-09-13 01:18:36 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@request_no_timeout_app.route("/1")
|
2017-09-13 01:18:36 +01:00
|
|
|
async def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-11-26 04:55:45 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@request_timeout_default_app.websocket("/ws1")
|
2018-12-22 15:21:45 +00:00
|
|
|
async def ws_handler1(request, ws):
|
2018-12-30 11:18:06 +00:00
|
|
|
await ws.send("OK")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2016-11-26 04:55:45 +00:00
|
|
|
def test_default_server_error_request_timeout():
|
2019-04-23 22:44:42 +01:00
|
|
|
client = DelayableSanicTestClient(request_timeout_default_app, 2)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = client.get("/1")
|
2016-11-26 04:55:45 +00:00
|
|
|
assert response.status == 408
|
2020-01-20 14:58:14 +00:00
|
|
|
assert "Request Timeout" in response.text
|
2017-09-13 01:18:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_default_server_error_request_dont_timeout():
|
2019-04-23 22:44:42 +01:00
|
|
|
client = DelayableSanicTestClient(request_no_timeout_app, 0.2)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = client.get("/1")
|
2017-09-13 01:18:36 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
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",
|
2018-12-22 15:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
client = DelayableSanicTestClient(request_timeout_default_app, 2)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = client.get("/ws1", headers=headers)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 408
|
2020-01-20 14:58:14 +00:00
|
|
|
assert "Request Timeout" in response.text
|