2018-12-13 17:50:50 +00:00
|
|
|
import asyncio
|
2018-12-22 15:21:45 +00:00
|
|
|
import logging
|
2019-01-15 15:27:41 +00:00
|
|
|
import sys
|
2019-01-15 13:11:38 +00:00
|
|
|
|
2019-01-15 12:52:53 +00:00
|
|
|
from inspect import isawaitable
|
2018-12-13 17:50:50 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sanic.exceptions import SanicException
|
|
|
|
from sanic.response import text
|
|
|
|
|
|
|
|
|
2019-01-15 15:32:40 +00:00
|
|
|
def uvloop_installed():
|
|
|
|
try:
|
|
|
|
import uvloop
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-12-13 17:50:50 +00:00
|
|
|
def test_app_loop_running(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/test")
|
2018-12-13 17:50:50 +00:00
|
|
|
async def handler(request):
|
|
|
|
assert isinstance(app.loop, asyncio.AbstractEventLoop)
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("pass")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test")
|
|
|
|
assert response.text == "pass"
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
|
2019-01-15 15:45:47 +00:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7),
|
2019-01-15 15:27:41 +00:00
|
|
|
reason="requires python3.7 or higher")
|
2019-01-15 11:38:47 +00:00
|
|
|
def test_create_asyncio_server(app):
|
2019-01-15 15:45:47 +00:00
|
|
|
if not uvloop_installed():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio_srv_coro = app.create_server(
|
|
|
|
return_asyncio_server=True)
|
|
|
|
assert isawaitable(asyncio_srv_coro)
|
|
|
|
srv = loop.run_until_complete(asyncio_srv_coro)
|
|
|
|
assert srv.is_serving() is True
|
2019-01-15 12:52:53 +00:00
|
|
|
|
|
|
|
|
2019-01-15 15:45:47 +00:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7),
|
2019-01-15 15:27:41 +00:00
|
|
|
reason="requires python3.7 or higher")
|
2019-01-15 12:52:53 +00:00
|
|
|
def test_asyncio_server_start_serving(app):
|
2019-01-15 15:45:47 +00:00
|
|
|
if not uvloop_installed():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
asyncio_srv_coro = app.create_server(
|
|
|
|
return_asyncio_server=True,
|
|
|
|
asyncio_server_kwargs=dict(
|
|
|
|
start_serving=False
|
|
|
|
))
|
|
|
|
srv = loop.run_until_complete(asyncio_srv_coro)
|
|
|
|
assert srv.is_serving() is False
|
2019-01-15 11:38:47 +00:00
|
|
|
|
|
|
|
|
2018-12-13 17:50:50 +00:00
|
|
|
def test_app_loop_not_running(app):
|
|
|
|
with pytest.raises(SanicException) as excinfo:
|
|
|
|
app.loop
|
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
2018-12-30 11:18:06 +00:00
|
|
|
"Loop can only be retrieved after the app has started "
|
|
|
|
"running. Not supported with `create_server` function"
|
2018-12-13 17:50:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_run_raise_type_error(app):
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2018-12-30 11:18:06 +00:00
|
|
|
app.run(loop="loop")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
2018-12-30 11:18:06 +00:00
|
|
|
"loop is not a valid argument. To use an existing loop, "
|
|
|
|
"change to create_server().\nSee more: "
|
|
|
|
"https://sanic.readthedocs.io/en/latest/sanic/deploying.html"
|
|
|
|
"#asynchronous-support"
|
2018-12-13 17:50:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_route_raise_value_error(app):
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2018-12-30 11:18:06 +00:00
|
|
|
|
|
|
|
@app.route("/test")
|
2018-12-13 17:50:50 +00:00
|
|
|
async def handler():
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("test")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert (
|
|
|
|
str(excinfo.value)
|
|
|
|
== "Required parameter `request` missing in the handler() route?"
|
|
|
|
)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_app_handle_request_handler_is_none(app, monkeypatch):
|
|
|
|
def mockreturn(*args, **kwargs):
|
2018-12-30 11:18:06 +00:00
|
|
|
return None, [], {}, ""
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
# Not sure how to make app.router.get() return None, so use mock here.
|
2018-12-30 11:18:06 +00:00
|
|
|
monkeypatch.setattr(app.router, "get", mockreturn)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/test")
|
2018-12-13 17:50:50 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("test")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert (
|
|
|
|
response.text
|
|
|
|
== "Error: 'None' was returned while requesting a handler from the router"
|
|
|
|
)
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("websocket_enabled", [True, False])
|
|
|
|
@pytest.mark.parametrize("enable", [True, False])
|
2018-12-22 15:21:45 +00:00
|
|
|
def test_app_enable_websocket(app, websocket_enabled, enable):
|
2018-12-13 17:50:50 +00:00
|
|
|
app.websocket_enabled = websocket_enabled
|
|
|
|
app.enable_websocket(enable=enable)
|
|
|
|
|
|
|
|
assert app.websocket_enabled == enable
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.websocket("/ws")
|
2018-12-13 17:50:50 +00:00
|
|
|
async def handler(request, ws):
|
2018-12-30 11:18:06 +00:00
|
|
|
await ws.send("test")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
assert app.websocket_enabled == True
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_handle_request_with_nested_exception(app, monkeypatch):
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
err_msg = "Mock Exception"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
# Not sure how to raise an exception in app.error_handler.response(), use mock here
|
|
|
|
def mock_error_handler_response(*args, **kwargs):
|
|
|
|
raise Exception(err_msg)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
app.error_handler, "response", mock_error_handler_response
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
|
|
|
raise Exception
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 500
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "An error occurred while handling an error"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_handle_request_with_nested_exception_debug(app, monkeypatch):
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
err_msg = "Mock Exception"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
# Not sure how to raise an exception in app.error_handler.response(), use mock here
|
|
|
|
def mock_error_handler_response(*args, **kwargs):
|
|
|
|
raise Exception(err_msg)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
app.error_handler, "response", mock_error_handler_response
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
|
|
|
raise Exception
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/", debug=True)
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 500
|
|
|
|
assert response.text.startswith(
|
2018-12-30 11:18:06 +00:00
|
|
|
"Error while handling error: {}\nStack: Traceback (most recent call last):\n".format(
|
|
|
|
err_msg
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_request_with_nested_sanic_exception(app, monkeypatch, caplog):
|
|
|
|
|
|
|
|
# Not sure how to raise an exception in app.error_handler.response(), use mock here
|
|
|
|
def mock_error_handler_response(*args, **kwargs):
|
2018-12-30 11:18:06 +00:00
|
|
|
raise SanicException("Mock SanicException")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
monkeypatch.setattr(
|
|
|
|
app.error_handler, "response", mock_error_handler_response
|
|
|
|
)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
|
|
|
raise Exception
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 500
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Error: Mock SanicException"
|
2019-01-08 15:15:23 +00:00
|
|
|
assert (
|
2018-12-30 11:18:06 +00:00
|
|
|
"sanic.root",
|
2018-12-22 15:21:45 +00:00
|
|
|
logging.ERROR,
|
2018-12-30 11:18:06 +00:00
|
|
|
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
2019-01-08 15:15:23 +00:00
|
|
|
) in caplog.record_tuples
|