2017-09-11 08:17:33 +01:00
|
|
|
import asyncio
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2017-09-12 04:40:43 +01:00
|
|
|
from asyncio import sleep as aio_sleep
|
2019-04-23 22:44:42 +01:00
|
|
|
from json import JSONDecodeError
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
from os import environ
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
import httpcore
|
2019-12-21 03:23:52 +00:00
|
|
|
import httpx
|
2021-04-06 21:32:01 +01:00
|
|
|
import platform
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
import pytest
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2021-01-28 07:22:22 +00:00
|
|
|
from sanic_testing.testing import HOST, SanicTestClient
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic import Sanic, server
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
from sanic.compat import OS_IS_WINDOWS
|
2017-09-11 08:17:33 +01:00
|
|
|
from sanic.response import text
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2020-06-05 15:14:18 +01:00
|
|
|
|
2019-02-06 18:29:33 +00:00
|
|
|
CONFIG_FOR_TESTS = {"KEEP_ALIVE_TIMEOUT": 2, "KEEP_ALIVE": True}
|
|
|
|
|
2020-03-26 04:42:46 +00:00
|
|
|
PORT = 42101 # test_keep_alive_timeout_reuse doesn't work with random port
|
2018-12-30 18:37:30 +00:00
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
class ReusableSanicConnectionPool(httpcore.AsyncConnectionPool):
|
|
|
|
last_reused_connection = None
|
2020-03-01 19:42:11 +00:00
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
async def _get_connection_from_pool(self, *args, **kwargs):
|
|
|
|
conn = await super()._get_connection_from_pool(*args, **kwargs)
|
|
|
|
self.__class__.last_reused_connection = conn
|
|
|
|
return conn
|
2019-04-23 22:44:42 +01:00
|
|
|
|
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
class ResusableSanicSession(httpx.AsyncClient):
|
2019-04-23 22:44:42 +01:00
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
2020-09-27 00:58:36 +01:00
|
|
|
transport = ReusableSanicConnectionPool()
|
|
|
|
super().__init__(transport=transport, *args, **kwargs)
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ReuseableSanicTestClient(SanicTestClient):
|
2017-09-12 04:09:42 +01:00
|
|
|
def __init__(self, app, loop=None):
|
2019-04-23 22:44:42 +01:00
|
|
|
super().__init__(app)
|
2017-09-12 04:09:42 +01:00
|
|
|
if loop is None:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
self._loop = loop
|
|
|
|
self._server = None
|
2017-09-11 08:17:33 +01:00
|
|
|
self._tcp_connector = None
|
|
|
|
self._session = None
|
|
|
|
|
2019-12-21 03:23:52 +00:00
|
|
|
def get_new_session(self):
|
|
|
|
return ResusableSanicSession()
|
|
|
|
|
2017-09-12 04:09:42 +01:00
|
|
|
# Copied from SanicTestClient, but with some changes to reuse the
|
|
|
|
# same loop for the same app.
|
2017-09-11 08:17:33 +01:00
|
|
|
def _sanic_endpoint_test(
|
2018-12-30 11:18:06 +00:00
|
|
|
self,
|
|
|
|
method="get",
|
|
|
|
uri="/",
|
|
|
|
gather_request=True,
|
|
|
|
debug=False,
|
2019-05-17 06:44:46 +01:00
|
|
|
server_kwargs=None,
|
2018-12-30 11:18:06 +00:00
|
|
|
*request_args,
|
2019-04-23 22:44:42 +01:00
|
|
|
**request_kwargs,
|
2018-12-30 11:18:06 +00:00
|
|
|
):
|
2017-09-12 04:09:42 +01:00
|
|
|
loop = self._loop
|
2017-09-11 08:17:33 +01:00
|
|
|
results = [None, None]
|
|
|
|
exceptions = []
|
2019-05-17 06:44:46 +01:00
|
|
|
server_kwargs = server_kwargs or {"return_asyncio_server": True}
|
2017-09-11 08:17:33 +01:00
|
|
|
if gather_request:
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2017-09-11 08:17:33 +01:00
|
|
|
def _collect_request(request):
|
|
|
|
if results[0] is None:
|
|
|
|
results[0] = request
|
|
|
|
|
|
|
|
self.app.request_middleware.appendleft(_collect_request)
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
if uri.startswith(
|
|
|
|
("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")
|
|
|
|
):
|
|
|
|
url = uri
|
|
|
|
else:
|
2020-02-25 20:01:13 +00:00
|
|
|
uri = uri if uri.startswith("/") else f"/{uri}"
|
2019-04-23 22:44:42 +01:00
|
|
|
scheme = "http"
|
2020-02-25 20:01:13 +00:00
|
|
|
url = f"{scheme}://{HOST}:{PORT}{uri}"
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@self.app.listener("after_server_start")
|
2017-09-12 04:09:42 +01:00
|
|
|
async def _collect_response(loop):
|
2017-09-11 08:17:33 +01:00
|
|
|
try:
|
|
|
|
response = await self._local_request(
|
2019-04-23 22:44:42 +01:00
|
|
|
method, url, *request_args, **request_kwargs
|
2018-12-30 11:18:06 +00:00
|
|
|
)
|
2017-09-11 08:17:33 +01:00
|
|
|
results[-1] = response
|
2017-09-12 04:40:43 +01:00
|
|
|
except Exception as e2:
|
|
|
|
exceptions.append(e2)
|
2017-09-12 04:09:42 +01:00
|
|
|
|
|
|
|
if self._server is not None:
|
|
|
|
_server = self._server
|
|
|
|
else:
|
2018-12-30 11:18:06 +00:00
|
|
|
_server_co = self.app.create_server(
|
|
|
|
host=HOST, debug=debug, port=PORT, **server_kwargs
|
|
|
|
)
|
2017-09-12 04:09:42 +01:00
|
|
|
|
|
|
|
server.trigger_events(
|
2018-12-30 11:18:06 +00:00
|
|
|
self.app.listeners["before_server_start"], loop
|
|
|
|
)
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2017-09-12 04:09:42 +01:00
|
|
|
try:
|
|
|
|
loop._stopping = False
|
2019-04-23 22:44:42 +01:00
|
|
|
_server = loop.run_until_complete(_server_co)
|
2017-09-12 04:40:43 +01:00
|
|
|
except Exception as e1:
|
|
|
|
raise e1
|
2019-04-23 22:44:42 +01:00
|
|
|
self._server = _server
|
2018-12-30 11:18:06 +00:00
|
|
|
server.trigger_events(self.app.listeners["after_server_start"], loop)
|
|
|
|
self.app.listeners["after_server_start"].pop()
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
if exceptions:
|
2020-02-25 20:01:13 +00:00
|
|
|
raise ValueError(f"Exception during request: {exceptions}")
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
if gather_request:
|
2017-09-12 04:09:42 +01:00
|
|
|
self.app.request_middleware.pop()
|
2017-09-11 08:17:33 +01:00
|
|
|
try:
|
|
|
|
request, response = results
|
|
|
|
return request, response
|
2018-10-09 03:40:36 +01:00
|
|
|
except Exception:
|
2017-09-11 08:17:33 +01:00
|
|
|
raise ValueError(
|
2020-02-25 20:01:13 +00:00
|
|
|
f"Request and response object expected, got ({results})"
|
2018-12-30 11:18:06 +00:00
|
|
|
)
|
2017-09-11 08:17:33 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return results[-1]
|
2018-10-09 03:40:36 +01:00
|
|
|
except Exception:
|
2020-06-05 15:14:18 +01:00
|
|
|
raise ValueError(f"Request object expected, got ({results})")
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
def kill_server(self):
|
|
|
|
try:
|
|
|
|
if self._server:
|
|
|
|
self._server.close()
|
|
|
|
self._loop.run_until_complete(self._server.wait_closed())
|
|
|
|
self._server = None
|
|
|
|
|
|
|
|
if self._session:
|
2020-03-01 19:42:11 +00:00
|
|
|
self._loop.run_until_complete(self._session.aclose())
|
2019-04-23 22:44:42 +01:00
|
|
|
self._session = None
|
|
|
|
|
|
|
|
except Exception as e3:
|
|
|
|
raise e3
|
|
|
|
|
2017-09-12 04:09:42 +01:00
|
|
|
# Copied from SanicTestClient, but with some changes to reuse the
|
|
|
|
# same TCPConnection and the sane ClientSession more than once.
|
|
|
|
# Note, you cannot use the same session if you are in a _different_
|
|
|
|
# loop, so the changes above are required too.
|
2019-04-23 22:44:42 +01:00
|
|
|
async def _local_request(self, method, url, *args, **kwargs):
|
|
|
|
raw_cookies = kwargs.pop("raw_cookies", None)
|
2018-12-30 11:18:06 +00:00
|
|
|
request_keepalive = kwargs.pop(
|
2019-02-06 18:29:33 +00:00
|
|
|
"request_keepalive", CONFIG_FOR_TESTS["KEEP_ALIVE_TIMEOUT"]
|
2018-12-30 11:18:06 +00:00
|
|
|
)
|
2019-05-17 06:44:46 +01:00
|
|
|
if not self._session:
|
2019-12-21 03:23:52 +00:00
|
|
|
self._session = self.get_new_session()
|
2019-05-17 06:44:46 +01:00
|
|
|
try:
|
|
|
|
response = await getattr(self._session, method.lower())(
|
2020-03-01 19:42:11 +00:00
|
|
|
url, timeout=request_keepalive, *args, **kwargs
|
2019-05-17 06:44:46 +01:00
|
|
|
)
|
|
|
|
except NameError:
|
|
|
|
raise Exception(response.status_code)
|
2017-09-11 08:17:33 +01:00
|
|
|
|
2019-05-17 06:44:46 +01:00
|
|
|
try:
|
|
|
|
response.json = response.json()
|
|
|
|
except (JSONDecodeError, UnicodeDecodeError):
|
|
|
|
response.json = None
|
|
|
|
|
2020-03-01 19:42:11 +00:00
|
|
|
response.body = await response.aread()
|
2019-05-17 06:44:46 +01:00
|
|
|
response.status = response.status_code
|
|
|
|
response.content_type = response.headers.get("content-type")
|
|
|
|
|
|
|
|
if raw_cookies:
|
|
|
|
response.raw_cookies = {}
|
|
|
|
for cookie in response.cookies:
|
|
|
|
response.raw_cookies[cookie.name] = cookie
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2017-09-12 04:09:42 +01:00
|
|
|
return response
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
keep_alive_timeout_app_reuse = Sanic("test_ka_timeout_reuse")
|
|
|
|
keep_alive_app_client_timeout = Sanic("test_ka_client_timeout")
|
|
|
|
keep_alive_app_server_timeout = Sanic("test_ka_server_timeout")
|
2021-03-17 18:55:52 +00:00
|
|
|
keep_alive_app_context = Sanic("keep_alive_app_context")
|
2017-09-12 04:09:42 +01:00
|
|
|
|
2018-12-30 18:37:30 +00:00
|
|
|
keep_alive_timeout_app_reuse.config.update(CONFIG_FOR_TESTS)
|
|
|
|
keep_alive_app_client_timeout.config.update(CONFIG_FOR_TESTS)
|
|
|
|
keep_alive_app_server_timeout.config.update(CONFIG_FOR_TESTS)
|
2021-03-17 18:55:52 +00:00
|
|
|
keep_alive_app_context.config.update(CONFIG_FOR_TESTS)
|
2018-12-30 18:37:30 +00:00
|
|
|
|
2017-09-12 04:09:42 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@keep_alive_timeout_app_reuse.route("/1")
|
2017-09-12 04:09:42 +01:00
|
|
|
async def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@keep_alive_app_client_timeout.route("/1")
|
2017-09-12 04:09:42 +01:00
|
|
|
async def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-09-11 08:17:33 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@keep_alive_app_server_timeout.route("/1")
|
2017-09-12 04:09:42 +01:00
|
|
|
async def handler3(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-09-12 04:09:42 +01:00
|
|
|
|
|
|
|
|
2021-03-17 18:55:52 +00:00
|
|
|
@keep_alive_app_context.post("/ctx")
|
|
|
|
def set_ctx(request):
|
|
|
|
request.conn_info.ctx.foo = "hello"
|
|
|
|
return text("OK")
|
|
|
|
|
|
|
|
|
|
|
|
@keep_alive_app_context.get("/ctx")
|
|
|
|
def get_ctx(request):
|
|
|
|
return text(request.conn_info.ctx.foo)
|
|
|
|
|
|
|
|
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
|
|
|
|
reason="Not testable with current client",
|
|
|
|
)
|
2018-02-27 06:18:21 +00:00
|
|
|
def test_keep_alive_timeout_reuse():
|
|
|
|
"""If the server keep-alive timeout and client keep-alive timeout are
|
2020-08-27 08:22:02 +01:00
|
|
|
both longer than the delay, the client _and_ server will successfully
|
|
|
|
reuse the existing connection."""
|
2019-05-17 08:25:46 +01:00
|
|
|
try:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop)
|
|
|
|
headers = {"Connection": "keep-alive"}
|
|
|
|
request, response = client.get("/1", headers=headers)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == "OK"
|
|
|
|
loop.run_until_complete(aio_sleep(1))
|
|
|
|
request, response = client.get("/1")
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == "OK"
|
2020-09-27 00:58:36 +01:00
|
|
|
assert ReusableSanicConnectionPool.last_reused_connection
|
2019-05-17 08:25:46 +01:00
|
|
|
finally:
|
|
|
|
client.kill_server()
|
2018-02-27 06:18:21 +00:00
|
|
|
|
|
|
|
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
@pytest.mark.skipif(
|
2021-04-06 21:32:01 +01:00
|
|
|
bool(environ.get("SANIC_NO_UVLOOP"))
|
|
|
|
or OS_IS_WINDOWS
|
|
|
|
or platform.system() != "Linux",
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
reason="Not testable with current client",
|
|
|
|
)
|
2018-02-27 06:18:21 +00:00
|
|
|
def test_keep_alive_client_timeout():
|
|
|
|
"""If the server keep-alive timeout is longer than the client
|
|
|
|
keep-alive timeout, client will try to create a new connection here."""
|
|
|
|
try:
|
2019-05-17 08:25:46 +01:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop)
|
|
|
|
headers = {"Connection": "keep-alive"}
|
2021-03-17 18:55:52 +00:00
|
|
|
_, response = client.get("/1", headers=headers, request_keepalive=1)
|
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == "OK"
|
2021-03-17 18:55:52 +00:00
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
loop.run_until_complete(aio_sleep(2))
|
2021-03-17 18:55:52 +00:00
|
|
|
_, response = client.get("/1", request_keepalive=1)
|
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
assert ReusableSanicConnectionPool.last_reused_connection is None
|
2019-05-17 08:25:46 +01:00
|
|
|
finally:
|
|
|
|
client.kill_server()
|
2018-02-27 06:18:21 +00:00
|
|
|
|
|
|
|
|
Streaming Server (#1876)
* Streaming request by async for.
* Make all requests streaming and preload body for non-streaming handlers.
* Cleanup of code and avoid mixing streaming responses.
* Async http protocol loop.
* Change of test: don't require early bad request error but only after CRLF-CRLF.
* Add back streaming requests.
* Rewritten request body parser.
* Misc. cleanup, down to 4 failing tests.
* All tests OK.
* Entirely remove request body queue.
* Let black f*ckup the layout
* Better testing error messages on protocol errors.
* Remove StreamBuffer tests because the type is about to be removed.
* Remove tests using the deprecated get_headers function that can no longer be supported. Chunked mode is now autodetected, so do not put content-length header if chunked mode is preferred.
* Major refactoring of HTTP protocol handling (new module http.py added), all requests made streaming. A few compatibility issues and a lot of cleanup to be done remain, 16 tests failing.
* Terminate check_timeouts once connection_task finishes.
* Code cleanup, 14 tests failing.
* Much cleanup, 12 failing...
* Even more cleanup and error checking, 8 failing tests.
* Remove keep-alive header from responses. First of all, it should say timeout=<value> which wasn't the case with existing implementation, and secondly none of the other web servers I tried include this header.
* Everything but CustomServer OK.
* Linter
* Disable custom protocol test
* Remove unnecessary variables, optimise performance.
* A test was missing that body_init/body_push/body_finish are never called. Rewritten using receive_body and case switching to make it fail if bypassed.
* Minor fixes.
* Remove unused code.
* Py 3.8 check for deprecated loop argument.
* Fix a middleware cancellation handling test with py38.
* Linter 'n fixes
* Typing
* Stricter handling of request header size
* More specific error messages on Payload Too Large.
* Init http.response = None
* Messages further tuned.
* Always try to consume request body, plus minor cleanup.
* Add a missing check in case of close_if_idle on a dead connection.
* Avoid error messages on PayloadTooLarge.
* Add test for new API.
* json takes str, not bytes
* Default to no maximum request size for streaming handlers.
* Fix chunked mode crash.
* Header values should be strictly ASCII but both UTF-8 and Latin-1 exist. Use UTF-8B to
cope with all.
* Refactoring and cleanup.
* Unify response header processing of ASGI and asyncio modes.
* Avoid special handling of StreamingHTTPResponse.
* 35 % speedup in HTTP/1.1 response formatting (not so much overall effect).
* Duplicate set-cookie headers were being produced.
* Cleanup processed_headers some more.
* Linting
* Import ordering
* Response middleware ran by async request.respond().
* Need to check if transport is closing to avoid getting stuck in sending loops after peer has disconnected.
* Middleware and error handling refactoring.
* Linter
* Fix tracking of HTTP stage when writing to transport fails.
* Add clarifying comment
* Add a check for request body functions and a test for NotImplementedError.
* Linter and typing
* These must be tuples + hack mypy warnings away.
* New streaming test and minor fixes.
* Constant receive buffer size.
* 256 KiB send and receive buffers.
* Revert "256 KiB send and receive buffers."
This reverts commit abc1e3edb21a5e6925fa4c856657559608a8d65b.
* app.handle_exception already sends the response.
* Improved handling of errors during request.
* An odd hack to avoid an httpx limitation that causes test failures.
* Limit request header size to 8 KiB at most.
* Remove unnecessary use of format string.
* Cleanup tests
* Remove artifact
* Fix type checking
* Mark test for skipping
* Cleanup some edge cases
* Add ignore_body flag to safe methods
* Add unit tests for timeout logic
* Add unit tests for timeout logic
* Fix Mock usage in timeout test
* Change logging test to only logger in handler
* Windows py3.8 logging issue with current testing client
* Add test_header_size_exceeded
* Resolve merge conflicts
* Add request middleware to hard exception handling
* Add request middleware to hard exception handling
* Request middleware on exception handlers
* Linting
* Cleanup deprecations
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
2021-01-10 22:45:36 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
|
|
|
|
reason="Not testable with current client",
|
|
|
|
)
|
2018-02-27 06:18:21 +00:00
|
|
|
def test_keep_alive_server_timeout():
|
|
|
|
"""If the client keep-alive timeout is longer than the server
|
|
|
|
keep-alive timeout, the client will either a 'Connection reset' error
|
|
|
|
_or_ a new connection. Depending on how the event-loop handles the
|
|
|
|
broken server connection."""
|
|
|
|
try:
|
2019-05-17 08:25:46 +01:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
client = ReuseableSanicTestClient(keep_alive_app_server_timeout, loop)
|
|
|
|
headers = {"Connection": "keep-alive"}
|
2021-03-17 18:55:52 +00:00
|
|
|
_, response = client.get("/1", headers=headers, request_keepalive=60)
|
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == "OK"
|
2021-03-17 18:55:52 +00:00
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
loop.run_until_complete(aio_sleep(3))
|
2021-03-17 18:55:52 +00:00
|
|
|
_, response = client.get("/1", request_keepalive=60)
|
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
assert ReusableSanicConnectionPool.last_reused_connection is None
|
2019-05-17 08:25:46 +01:00
|
|
|
finally:
|
|
|
|
client.kill_server()
|
2021-03-17 18:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
bool(environ.get("SANIC_NO_UVLOOP")) or OS_IS_WINDOWS,
|
|
|
|
reason="Not testable with current client",
|
|
|
|
)
|
|
|
|
def test_keep_alive_connection_context():
|
|
|
|
try:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
client = ReuseableSanicTestClient(keep_alive_app_context, loop)
|
|
|
|
headers = {"Connection": "keep-alive"}
|
|
|
|
request1, _ = client.post("/ctx", headers=headers)
|
|
|
|
|
|
|
|
loop.run_until_complete(aio_sleep(1))
|
|
|
|
request2, response = client.get("/ctx")
|
|
|
|
|
|
|
|
assert response.text == "hello"
|
|
|
|
assert id(request1.conn_info.ctx) == id(request2.conn_info.ctx)
|
|
|
|
assert (
|
|
|
|
request1.conn_info.ctx.foo == request2.conn_info.ctx.foo == "hello"
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
client.kill_server()
|