Release 22.6 (#2487)

This commit is contained in:
Adam Hopkins
2022-06-28 15:25:46 +03:00
committed by GitHub
parent aba333bfb6
commit 13d5a44278
22 changed files with 155 additions and 371 deletions

View File

@@ -4,7 +4,6 @@ import re
from collections import Counter
from inspect import isawaitable
from os import environ
from unittest.mock import Mock, patch
import pytest
@@ -113,19 +112,6 @@ def test_create_server_main_convenience(app, caplog):
) in caplog.record_tuples
def test_create_server_init(app, caplog):
loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server(return_asyncio_server=True)
server = loop.run_until_complete(asyncio_srv_coro)
message = (
"AsyncioServer.init has been deprecated and will be removed in v22.6. "
"Use Sanic.state.is_started instead."
)
with pytest.warns(DeprecationWarning, match=message):
server.init
def test_app_loop_not_running(app):
with pytest.raises(SanicException) as excinfo:
app.loop
@@ -385,40 +371,6 @@ def test_get_app_default_ambiguous():
Sanic.get_app()
def test_app_no_registry():
Sanic("no-register", register=False)
with pytest.raises(
SanicException, match='Sanic app name "no-register" not found.'
):
Sanic.get_app("no-register")
def test_app_no_registry_deprecation_message():
with pytest.warns(DeprecationWarning) as records:
Sanic("no-register", register=False)
Sanic("yes-register", register=True)
message = (
"[DEPRECATION v22.6] The register argument is deprecated and will "
"stop working in v22.6. After v22.6 all apps will be added to the "
"Sanic app registry."
)
assert len(records) == 2
for record in records:
assert record.message.args[0] == message
def test_app_no_registry_env():
environ["SANIC_REGISTER"] = "False"
Sanic("no-register")
with pytest.raises(
SanicException, match='Sanic app name "no-register" not found.'
):
Sanic.get_app("no-register")
del environ["SANIC_REGISTER"]
def test_app_set_attribute_warning(app):
message = (
"Setting variables on Sanic instances is not allowed. You should "

View File

@@ -371,15 +371,6 @@ def test_update_from_lowercase_key(app: Sanic):
assert "test_setting_value" not in app.config
def test_deprecation_notice_when_setting_logo(app: Sanic):
message = (
"Setting the config.LOGO is deprecated and will no longer be "
"supported starting in v22.6."
)
with pytest.warns(DeprecationWarning, match=message):
app.config.LOGO = "My Custom Logo"
def test_config_set_methods(app: Sanic, monkeypatch: MonkeyPatch):
post_set = Mock()
monkeypatch.setattr(Config, "_post_set", post_set)

View File

@@ -13,13 +13,7 @@ from sanic import Sanic, handlers
from sanic.exceptions import BadRequest, Forbidden, NotFound, ServerError
from sanic.handlers import ErrorHandler
from sanic.request import Request
from sanic.response import stream, text
async def sample_streaming_fn(response):
await response.write("foo,")
await asyncio.sleep(0.001)
await response.write("bar")
from sanic.response import text
class ErrorWithRequestCtx(ServerError):
@@ -81,10 +75,10 @@ def exception_handler_app():
@exception_handler_app.exception(Forbidden)
async def async_handler_exception(request, exception):
return stream(
sample_streaming_fn,
content_type="text/csv",
)
response = await request.respond(content_type="text/csv")
await response.send("foo,")
await asyncio.sleep(0.001)
await response.send("bar")
@exception_handler_app.middleware
async def some_request_middleware(request):
@@ -183,7 +177,7 @@ def test_exception_handler_lookup(exception_handler_app: Sanic):
class ModuleNotFoundError(ImportError):
pass
handler = ErrorHandler("auto")
handler = ErrorHandler()
handler.add(ImportError, import_error_handler)
handler.add(CustomError, custom_error_handler)
handler.add(ServerError, server_error_handler)
@@ -261,7 +255,6 @@ def test_exception_handler_response_was_sent(
_, response = app.test_client.get("/1")
assert "some text" in response.text
# Change to assert warning not in the records in the future version.
message_in_records(
caplog.records,
(

View File

@@ -19,35 +19,6 @@ def test_logo_base(app, run_startup):
assert logs[0][2] == BASE_LOGO
def test_logo_false(app, run_startup):
app.config.LOGO = False
logs = run_startup(app)
banner, port = logs[1][2].rsplit(":", 1)
assert logs[0][1] == logging.INFO
assert banner == "Goin' Fast @ http://127.0.0.1"
assert int(port) > 0
def test_logo_true(app, run_startup):
app.config.LOGO = True
logs = run_startup(app)
assert logs[0][1] == logging.DEBUG
assert logs[0][2] == BASE_LOGO
def test_logo_custom(app, run_startup):
app.config.LOGO = "My Custom Logo"
logs = run_startup(app)
assert logs[0][1] == logging.DEBUG
assert logs[0][2] == "My Custom Logo"
def test_motd_with_expected_info(app, run_startup):
logs = run_startup(app)

View File

@@ -3,7 +3,7 @@ import contextlib
import pytest
from sanic.response import stream, text
from sanic.response import text
@pytest.mark.asyncio
@@ -43,18 +43,16 @@ async def test_stream_request_cancel_when_conn_lost(app):
async def post(request, id):
assert isinstance(request.stream, asyncio.Queue)
async def streaming(response):
while True:
body = await request.stream.get()
if body is None:
break
await response.write(body.decode("utf-8"))
response = await request.respond()
await asyncio.sleep(1.0)
# at this point client is already disconnected
app.ctx.still_serving_cancelled_request = True
return stream(streaming)
while True:
body = await request.stream.get()
if body is None:
break
await response.send(body.decode("utf-8"))
# schedule client call
loop = asyncio.get_event_loop()

View File

@@ -27,7 +27,6 @@ from sanic.response import (
file_stream,
json,
raw,
stream,
text,
)
@@ -49,10 +48,13 @@ def test_response_body_not_a_string(app):
assert b"Internal Server Error" in response.body
async def sample_streaming_fn(response):
await response.write("foo,")
async def sample_streaming_fn(request, response=None):
if not response:
response = await request.respond(content_type="text/csv")
await response.send("foo,")
await asyncio.sleep(0.001)
await response.write("bar")
await response.send("bar")
await response.eof()
def test_method_not_allowed():
@@ -217,10 +219,7 @@ def test_no_content(json_app):
def streaming_app(app):
@app.route("/")
async def test(request: Request):
return stream(
sample_streaming_fn,
content_type="text/csv",
)
await sample_streaming_fn(request)
return app
@@ -229,11 +228,11 @@ def streaming_app(app):
def non_chunked_streaming_app(app):
@app.route("/")
async def test(request: Request):
return stream(
sample_streaming_fn,
response = await request.respond(
headers={"Content-Length": "7"},
content_type="text/csv",
)
await sample_streaming_fn(request, response)
return app
@@ -283,18 +282,6 @@ def test_non_chunked_streaming_returns_correct_content(
assert response.text == "foo,bar"
def test_stream_response_with_cookies_legacy(app):
@app.route("/")
async def test(request: Request):
response = stream(sample_streaming_fn, content_type="text/csv")
response.cookies["test"] = "modified"
response.cookies["test"] = "pass"
return response
request, response = app.test_client.get("/")
assert response.cookies["test"] == "pass"
def test_stream_response_with_cookies(app):
@app.route("/")
async def test(request: Request):
@@ -317,7 +304,7 @@ def test_stream_response_with_cookies(app):
def test_stream_response_without_cookies(app):
@app.route("/")
async def test(request: Request):
return stream(sample_streaming_fn, content_type="text/csv")
await sample_streaming_fn(request)
request, response = app.test_client.get("/")
assert response.cookies == {}