Merge pull request #1614 from huge-success/asgi-custom-request

Add custom request support to ASGI mode; fix a couple tests
This commit is contained in:
7
2019-07-06 11:47:58 -07:00
committed by GitHub
5 changed files with 68 additions and 40 deletions

View File

@@ -5,8 +5,11 @@ from collections import deque
import pytest
import uvicorn
from sanic import Sanic
from sanic.asgi import MockTransport
from sanic.exceptions import InvalidUsage
from sanic.request import Request
from sanic.response import text
from sanic.websocket import WebSocketConnection
@@ -201,3 +204,28 @@ def test_improper_websocket_connection(transport, send, receive):
transport.create_websocket_connection(send, receive)
connection = transport.get_websocket_connection()
assert isinstance(connection, WebSocketConnection)
@pytest.mark.asyncio
async def test_request_class_regular(app):
@app.get("/regular")
def regular_request(request):
return text(request.__class__.__name__)
_, response = await app.asgi_client.get("/regular")
assert response.body == b"Request"
@pytest.mark.asyncio
async def test_request_class_custom():
class MyCustomRequest(Request):
pass
app = Sanic(request_class=MyCustomRequest)
@app.get("/custom")
def custom_request(request):
return text(request.__class__.__name__)
_, response = await app.asgi_client.get("/custom")
assert response.body == b"MyCustomRequest"

View File

@@ -19,8 +19,8 @@ def temp_path():
class ConfigTest:
not_for_config = 'should not be used'
CONFIG_VALUE = 'should be used'
not_for_config = "should not be used"
CONFIG_VALUE = "should be used"
def test_load_from_object(app):
@@ -31,15 +31,15 @@ def test_load_from_object(app):
def test_load_from_object_string(app):
app.config.from_object('test_config.ConfigTest')
assert 'CONFIG_VALUE' in app.config
assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config
app.config.from_object("test_config.ConfigTest")
assert "CONFIG_VALUE" in app.config
assert app.config.CONFIG_VALUE == "should be used"
assert "not_for_config" not in app.config
def test_load_from_object_string_exception(app):
with pytest.raises(ImportError):
app.config.from_object('test_config.Config.test')
app.config.from_object("test_config.Config.test")
def test_auto_load_env():

View File

@@ -1,8 +1,9 @@
import inspect
import pytest
from sanic import helpers
from sanic.config import Config
import pytest
def test_has_message_body():
@@ -63,15 +64,15 @@ def test_remove_entity_headers():
def test_import_string_class():
obj = helpers.import_string('sanic.config.Config')
obj = helpers.import_string("sanic.config.Config")
assert isinstance(obj, Config)
def test_import_string_module():
module = helpers.import_string('sanic.config')
module = helpers.import_string("sanic.config")
assert inspect.ismodule(module)
def test_import_string_exception():
with pytest.raises(ImportError):
helpers.import_string('test.test.test')
helpers.import_string("test.test.test")