2017-02-21 06:41:53 +00:00
|
|
|
import asyncio
|
2016-10-24 09:21:06 +01:00
|
|
|
import inspect
|
2018-07-21 06:31:15 +01:00
|
|
|
import os
|
2018-11-25 19:56:34 +00:00
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
import pytest
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2018-11-25 19:56:34 +00:00
|
|
|
from sanic.app import Sanic
|
2016-10-16 09:48:51 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
2017-07-13 04:18:56 +01:00
|
|
|
from sanic.constants import HTTP_METHODS
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.exceptions import InvalidUsage, NotFound, ServerError
|
2018-11-25 19:56:34 +00:00
|
|
|
from sanic.request import Request
|
2019-04-23 22:44:42 +01:00
|
|
|
from sanic.response import json, text
|
2018-11-25 19:56:34 +00:00
|
|
|
from sanic.views import CompositionView
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
2018-11-25 19:56:34 +00:00
|
|
|
def static_file_directory():
|
|
|
|
"""The static directory to serve"""
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
current_directory = os.path.dirname(os.path.abspath(current_file))
|
2018-12-30 11:18:06 +00:00
|
|
|
static_directory = os.path.join(current_directory, "static")
|
2018-11-25 19:56:34 +00:00
|
|
|
return static_directory
|
|
|
|
|
|
|
|
|
2018-07-21 06:31:15 +01:00
|
|
|
def get_file_path(static_file_directory, file_name):
|
|
|
|
return os.path.join(static_file_directory, file_name)
|
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-07-21 06:31:15 +01:00
|
|
|
def get_file_content(static_file_directory, file_name):
|
|
|
|
"""The content of the static file to check"""
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(get_file_path(static_file_directory, file_name), "rb") as file:
|
2018-07-21 06:31:15 +01:00
|
|
|
return file.read()
|
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("method", HTTP_METHODS)
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_versioned_routes_get(app, method):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text")
|
2017-07-13 04:18:56 +01:00
|
|
|
|
|
|
|
method = method.lower()
|
|
|
|
|
|
|
|
func = getattr(bp, method)
|
|
|
|
if callable(func):
|
2018-12-30 11:18:06 +00:00
|
|
|
|
|
|
|
@func("/{}".format(method), version=1)
|
2017-07-13 04:18:56 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
|
|
|
|
2017-07-13 04:18:56 +01:00
|
|
|
else:
|
|
|
|
print(func)
|
2018-11-25 19:56:34 +00:00
|
|
|
raise Exception("{} is not callable".format(func))
|
2017-07-13 04:18:56 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
client_method = getattr(app.test_client, method)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = client_method("/v1/{}".format(method))
|
2017-07-13 04:18:56 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.route("/")
|
2016-10-15 20:38:12 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Hello"
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_strict_slash(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text")
|
2017-03-24 01:37:06 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.get("/get", strict_slashes=True)
|
2018-10-14 14:31:13 +01:00
|
|
|
def get_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-03-24 01:37:06 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/post/", strict_slashes=True)
|
2018-10-14 14:31:13 +01:00
|
|
|
def post_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-03-24 01:37:06 +00:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get")
|
|
|
|
assert response.text == "OK"
|
2018-10-14 14:31:13 +01:00
|
|
|
assert response.json is None
|
2017-03-24 01:37:06 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get/")
|
2017-03-24 01:37:06 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post/")
|
|
|
|
assert response.text == "OK"
|
2017-03-24 01:37:06 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post")
|
2017-03-24 01:37:06 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_strict_slash_default_value(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text", strict_slashes=True)
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.get("/get")
|
2018-10-14 14:31:13 +01:00
|
|
|
def get_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/post/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def post_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get/")
|
2017-08-21 07:37:22 +01:00
|
|
|
assert response.status == 404
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post")
|
2017-08-21 07:37:22 +01:00
|
|
|
assert response.status == 404
|
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_strict_slash_without_passing_default_value(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.get("/get")
|
2018-10-14 14:31:13 +01:00
|
|
|
def get_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/post/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def post_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get/")
|
|
|
|
assert response.text == "OK"
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post")
|
|
|
|
assert response.text == "OK"
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_strict_slash_default_value_can_be_overwritten(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text", strict_slashes=True)
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.get("/get", strict_slashes=False)
|
2018-10-14 14:31:13 +01:00
|
|
|
def get_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.post("/post/", strict_slashes=False)
|
2018-10-14 14:31:13 +01:00
|
|
|
def post_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-08-21 07:37:22 +01:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get/")
|
|
|
|
assert response.text == "OK"
|
2017-08-21 07:37:22 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post")
|
|
|
|
assert response.text == "OK"
|
2017-03-24 01:37:06 +00:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_with_url_prefix(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text", url_prefix="/test1")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.route("/")
|
2016-10-15 20:38:12 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test1/")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Hello"
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_several_bp_with_url_prefix(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text", url_prefix="/test1")
|
|
|
|
bp2 = Blueprint("test_text2", url_prefix="/test2")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.route("/")
|
2016-10-15 20:38:12 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp2.route("/")
|
2016-10-15 20:38:12 +01:00
|
|
|
def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello2")
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
app.blueprint(bp2)
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test1/")
|
|
|
|
assert response.text == "Hello"
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test2/")
|
|
|
|
assert response.text == "Hello2"
|
2016-10-15 20:38:12 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_with_host(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_bp_host", url_prefix="/test1", host="example.com")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.route("/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp.route("/", host="sub.example.com")
|
2018-10-14 14:31:13 +01:00
|
|
|
def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello subdomain!")
|
2017-01-11 06:08:15 +00:00
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
app.blueprint(bp)
|
|
|
|
headers = {"Host": "example.com"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test1/", headers=headers)
|
|
|
|
assert response.text == "Hello"
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2017-01-11 06:08:15 +00:00
|
|
|
headers = {"Host": "sub.example.com"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test1/", headers=headers)
|
2017-01-11 06:08:15 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Hello subdomain!"
|
2017-01-11 06:08:15 +00:00
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_several_bp_with_host(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("test_text", url_prefix="/test", host="example.com")
|
|
|
|
bp2 = Blueprint("test_text2", url_prefix="/test", host="sub.example.com")
|
|
|
|
|
|
|
|
@bp.route("/")
|
2017-01-11 02:07:58 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp2.route("/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello2")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp2.route("/other/")
|
2017-01-11 02:07:58 +00:00
|
|
|
def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("Hello3")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
app.blueprint(bp2)
|
|
|
|
|
|
|
|
assert bp.host == "example.com"
|
|
|
|
headers = {"Host": "example.com"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test/", headers=headers)
|
|
|
|
assert response.text == "Hello"
|
2017-01-11 02:07:58 +00:00
|
|
|
|
|
|
|
assert bp2.host == "sub.example.com"
|
|
|
|
headers = {"Host": "sub.example.com"}
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/test/", headers=headers)
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Hello2"
|
|
|
|
request, response = app.test_client.get("/test/other/", headers=headers)
|
|
|
|
assert response.text == "Hello3"
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_middleware(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_middleware")
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.middleware("response")
|
2016-10-16 09:48:51 +01:00
|
|
|
async def process_response(request, response):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-16 09:48:51 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("FAIL")
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(blueprint)
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-16 09:48:51 +01:00
|
|
|
|
|
|
|
assert response.status == 200
|
2019-12-20 16:01:05 +00:00
|
|
|
assert response.text == "FAIL"
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_exception_handler(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_middleware")
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.route("/1")
|
2016-10-16 09:48:51 +01:00
|
|
|
def handler_1(request):
|
|
|
|
raise InvalidUsage("OK")
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.route("/2")
|
2016-10-16 09:48:51 +01:00
|
|
|
def handler_2(request):
|
|
|
|
raise ServerError("OK")
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.route("/3")
|
2016-10-16 09:48:51 +01:00
|
|
|
def handler_3(request):
|
|
|
|
raise NotFound("OK")
|
|
|
|
|
|
|
|
@blueprint.exception(NotFound, ServerError)
|
|
|
|
def handler_exception(request, exception):
|
|
|
|
return text("OK")
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(blueprint)
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/1")
|
2016-10-16 09:48:51 +01:00
|
|
|
assert response.status == 400
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/2")
|
2016-10-16 09:48:51 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/3")
|
2016-10-21 12:11:18 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_listeners(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_middleware")
|
2016-10-21 12:11:18 +01:00
|
|
|
|
|
|
|
order = []
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("before_server_start")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_1(sanic, loop):
|
|
|
|
order.append(1)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("after_server_start")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_2(sanic, loop):
|
|
|
|
order.append(2)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("after_server_start")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_3(sanic, loop):
|
|
|
|
order.append(3)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("before_server_stop")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_4(sanic, loop):
|
|
|
|
order.append(5)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("before_server_stop")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_5(sanic, loop):
|
|
|
|
order.append(4)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.listener("after_server_stop")
|
2016-10-21 12:11:18 +01:00
|
|
|
def handler_6(sanic, loop):
|
|
|
|
order.append(6)
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(blueprint)
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
assert order == [1, 2, 3, 4, 5, 6]
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_static(app):
|
2016-10-24 09:21:06 +01:00
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(current_file, "rb") as file:
|
2016-10-24 09:21:06 +01:00
|
|
|
current_file_contents = file.read()
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_static")
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint.static("/testing.file", current_file)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
app.blueprint(blueprint)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2016-10-24 09:21:06 +01:00
|
|
|
assert response.status == 200
|
2017-01-11 02:07:58 +00:00
|
|
|
assert response.body == current_file_contents
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.html"])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_static_content_type(app, file_name):
|
2018-07-21 06:31:15 +01:00
|
|
|
# This is done here, since no other test loads a file here
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
current_directory = os.path.dirname(os.path.abspath(current_file))
|
2018-12-30 11:18:06 +00:00
|
|
|
static_directory = os.path.join(current_directory, "static")
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_static")
|
2018-07-21 06:31:15 +01:00
|
|
|
blueprint.static(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/testing.file",
|
2018-07-21 06:31:15 +01:00
|
|
|
get_file_path(static_directory, file_name),
|
2018-12-30 11:18:06 +00:00
|
|
|
content_type="text/html; charset=utf-8",
|
2018-07-21 06:31:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
app.blueprint(blueprint)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/testing.file")
|
2018-07-21 06:31:15 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == get_file_content(static_directory, file_name)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_shorthand(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
blueprint = Blueprint("test_shorhand_routes")
|
2017-02-21 06:41:53 +00:00
|
|
|
ev = asyncio.Event()
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.get("/get")
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.put("/put")
|
2018-10-14 14:31:13 +01:00
|
|
|
def put_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.post("/post")
|
2018-10-14 14:31:13 +01:00
|
|
|
def post_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.head("/head")
|
2018-10-14 14:31:13 +01:00
|
|
|
def head_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.options("/options")
|
2018-10-14 14:31:13 +01:00
|
|
|
def options_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.patch("/patch")
|
2018-10-14 14:31:13 +01:00
|
|
|
def patch_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.delete("/delete")
|
2018-10-14 14:31:13 +01:00
|
|
|
def delete_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@blueprint.websocket("/ws/", strict_slashes=True)
|
2018-10-14 14:31:13 +01:00
|
|
|
async def websocket_handler(request, ws):
|
2017-02-21 06:41:53 +00:00
|
|
|
ev.set()
|
|
|
|
|
2017-01-30 07:20:38 +00:00
|
|
|
app.blueprint(blueprint)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/get")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/get")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.put("/put")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/post")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.post("/post")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/post")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.head("/head")
|
2017-01-30 07:21:00 +00:00
|
|
|
assert response.status == 200
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/head")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.options("/options")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/options")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.patch("/patch")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/patch")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.delete("/delete")
|
|
|
|
assert response.text == "OK"
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/delete")
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
2017-02-21 06:41:53 +00:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
request, response = app.test_client.websocket("/ws/")
|
|
|
|
assert response.opened is True
|
2017-02-21 06:41:53 +00:00
|
|
|
assert ev.is_set()
|
2018-01-19 01:20:51 +00:00
|
|
|
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_bp_group(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
deep_0 = Blueprint("deep_0", url_prefix="/deep")
|
|
|
|
deep_1 = Blueprint("deep_1", url_prefix="/deep1")
|
2018-01-19 01:20:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@deep_0.route("/")
|
2018-01-19 01:20:51 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("D0_OK")
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@deep_1.route("/bottom")
|
2018-10-14 14:31:13 +01:00
|
|
|
def bottom_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("D1B_OK")
|
2018-01-19 01:20:51 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
mid_0 = Blueprint.group(deep_0, deep_1, url_prefix="/mid")
|
|
|
|
mid_1 = Blueprint("mid_tier", url_prefix="/mid1")
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@mid_1.route("/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def handler1(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("M1_OK")
|
2018-01-19 01:20:51 +00:00
|
|
|
|
|
|
|
top = Blueprint.group(mid_0, mid_1)
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-01-19 01:20:51 +00:00
|
|
|
app.blueprint(top)
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2018-10-14 14:31:13 +01:00
|
|
|
def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("TOP_OK")
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
|
|
|
assert response.text == "TOP_OK"
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/mid1")
|
|
|
|
assert response.text == "M1_OK"
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/mid/deep")
|
|
|
|
assert response.text == "D0_OK"
|
2018-07-21 06:31:15 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/mid/deep1/bottom")
|
|
|
|
assert response.text == "D1B_OK"
|
2018-09-29 11:23:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_bp_group_with_default_url_prefix(app):
|
|
|
|
from sanic.response import json
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp_resources = Blueprint("bp_resources")
|
|
|
|
|
|
|
|
@bp_resources.get("/")
|
2018-09-29 11:23:16 +01:00
|
|
|
def list_resources_handler(request):
|
|
|
|
resource = {}
|
|
|
|
return json([resource])
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp_resource = Blueprint("bp_resource", url_prefix="/<resource_id>")
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp_resource.get("/")
|
2018-09-29 11:23:16 +01:00
|
|
|
def get_resource_hander(request, resource_id):
|
2018-12-30 11:18:06 +00:00
|
|
|
resource = {"resource_id": resource_id}
|
2018-09-29 11:23:16 +01:00
|
|
|
return json(resource)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp_resources_group = Blueprint.group(
|
|
|
|
bp_resources, bp_resource, url_prefix="/resources"
|
|
|
|
)
|
|
|
|
bp_api_v1 = Blueprint("bp_api_v1")
|
2018-10-14 14:31:13 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@bp_api_v1.get("/info")
|
2018-09-29 11:23:16 +01:00
|
|
|
def api_v1_info(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("api_version: v1")
|
2018-09-29 11:23:16 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
bp_api_v1_group = Blueprint.group(
|
|
|
|
bp_api_v1, bp_resources_group, url_prefix="/v1"
|
|
|
|
)
|
|
|
|
bp_api_group = Blueprint.group(bp_api_v1_group, url_prefix="/api")
|
2018-09-29 11:23:16 +01:00
|
|
|
app.blueprint(bp_api_group)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/api/v1/info")
|
|
|
|
assert response.text == "api_version: v1"
|
2018-09-29 11:23:16 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/api/v1/resources")
|
2018-09-29 11:23:16 +01:00
|
|
|
assert response.json == [{}]
|
|
|
|
|
|
|
|
from uuid import uuid4
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2018-09-29 11:23:16 +01:00
|
|
|
resource_id = str(uuid4())
|
2018-10-14 14:31:13 +01:00
|
|
|
request, response = app.test_client.get(
|
2018-12-30 11:18:06 +00:00
|
|
|
"/api/v1/resources/{0}".format(resource_id)
|
|
|
|
)
|
|
|
|
assert response.json == {"resource_id": resource_id}
|
2018-11-25 19:56:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_blueprint_middleware_with_args(app: Sanic):
|
|
|
|
bp = Blueprint(name="with_args_bp", url_prefix="/wa")
|
|
|
|
|
|
|
|
@bp.middleware
|
|
|
|
def middleware_with_no_tag(request: Request):
|
|
|
|
if request.headers.get("content-type") == "application/json":
|
|
|
|
request.headers["accepts"] = "plain/text"
|
|
|
|
else:
|
|
|
|
request.headers["accepts"] = "application/json"
|
|
|
|
|
|
|
|
@bp.route("/")
|
|
|
|
def default_route(request):
|
|
|
|
if request.headers.get("accepts") == "application/json":
|
|
|
|
return json({"test": "value"})
|
|
|
|
else:
|
|
|
|
return text("value")
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
_, response = app.test_client.get(
|
|
|
|
"/wa", headers={"content-type": "application/json"}
|
|
|
|
)
|
2018-11-25 19:56:34 +00:00
|
|
|
assert response.text == "value"
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
_, response = app.test_client.get(
|
|
|
|
"/wa", headers={"content-type": "plain/text"}
|
|
|
|
)
|
2018-11-25 19:56:34 +00:00
|
|
|
assert response.json.get("test") == "value"
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("file_name", ["test.file"])
|
2018-11-25 19:56:34 +00:00
|
|
|
def test_static_blueprint_name(app: Sanic, static_file_directory, file_name):
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
2018-12-30 11:18:06 +00:00
|
|
|
with open(current_file, "rb") as file:
|
2019-04-23 22:44:42 +01:00
|
|
|
file.read()
|
2018-11-25 19:56:34 +00:00
|
|
|
|
|
|
|
bp = Blueprint(name="static", url_prefix="/static", strict_slashes=False)
|
|
|
|
|
|
|
|
bp.static(
|
|
|
|
"/test.file/",
|
|
|
|
get_file_path(static_file_directory, file_name),
|
|
|
|
name="static.testing",
|
2018-12-30 11:18:06 +00:00
|
|
|
strict_slashes=True,
|
|
|
|
)
|
2018-11-25 19:56:34 +00:00
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
uri = app.url_for("static", name="static.testing")
|
2018-11-25 19:56:34 +00:00
|
|
|
assert uri == "/static/test.file"
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/static/test.file")
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/static/test.file/")
|
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_handler_add(app: Sanic):
|
|
|
|
view = CompositionView()
|
|
|
|
|
|
|
|
async def get_handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return json({"response": "OK"})
|
2018-11-25 19:56:34 +00:00
|
|
|
|
|
|
|
view.add(["GET"], get_handler, stream=False)
|
|
|
|
|
|
|
|
async def default_handler(request):
|
|
|
|
return text("OK")
|
|
|
|
|
|
|
|
bp = Blueprint(name="handler", url_prefix="/handler")
|
2018-12-30 11:18:06 +00:00
|
|
|
bp.add_route(default_handler, uri="/default/", strict_slashes=True)
|
2018-11-25 19:56:34 +00:00
|
|
|
|
|
|
|
bp.add_route(view, uri="/view", name="test")
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/handler/default/")
|
|
|
|
assert response.text == "OK"
|
|
|
|
|
|
|
|
_, response = app.test_client.get("/handler/view")
|
|
|
|
assert response.json["response"] == "OK"
|
|
|
|
|
|
|
|
|
|
|
|
def test_websocket_route(app: Sanic):
|
|
|
|
event = asyncio.Event()
|
|
|
|
|
|
|
|
async def websocket_handler(request, ws):
|
|
|
|
assert ws.subprotocol is None
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
bp = Blueprint(name="handler", url_prefix="/ws")
|
|
|
|
bp.add_websocket_route(websocket_handler, "/test", name="test")
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
_, response = app.test_client.websocket("/ws/test")
|
|
|
|
assert response.opened is True
|
2018-11-25 19:56:34 +00:00
|
|
|
assert event.is_set()
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_duplicate_blueprint(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp_name = "bp"
|
2018-12-13 17:50:50 +00:00
|
|
|
bp = Blueprint(bp_name)
|
|
|
|
bp1 = Blueprint(bp_name)
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError) as excinfo:
|
|
|
|
app.blueprint(bp1)
|
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
|
|
|
'A blueprint with the name "{}" is already registered. '
|
2018-12-30 11:18:06 +00:00
|
|
|
"Blueprint names must be unique."
|
2018-12-13 17:50:50 +00:00
|
|
|
).format(bp_name)
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("debug", [True, False, None])
|
2018-12-13 17:50:50 +00:00
|
|
|
def test_register_blueprint(app, debug):
|
2018-12-30 11:18:06 +00:00
|
|
|
bp = Blueprint("bp")
|
2018-12-13 17:50:50 +00:00
|
|
|
|
|
|
|
app.debug = debug
|
|
|
|
with pytest.warns(DeprecationWarning) as record:
|
|
|
|
app.register_blueprint(bp)
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2018-12-13 17:50:50 +00:00
|
|
|
assert record[0].message.args[0] == (
|
|
|
|
"Use of register_blueprint will be deprecated in "
|
|
|
|
"version 1.0. Please use the blueprint method"
|
|
|
|
" instead"
|
|
|
|
)
|
2019-06-06 13:21:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_strict_slashes_behavior_adoption(app):
|
|
|
|
app.strict_slashes = True
|
|
|
|
|
|
|
|
@app.get("/test")
|
|
|
|
def handler_test(request):
|
|
|
|
return text("Test")
|
|
|
|
|
|
|
|
assert app.test_client.get("/test")[1].status == 200
|
|
|
|
assert app.test_client.get("/test/")[1].status == 404
|
|
|
|
|
|
|
|
bp = Blueprint("bp")
|
|
|
|
|
|
|
|
@bp.get("/one", strict_slashes=False)
|
|
|
|
def one(request):
|
|
|
|
return text("one")
|
|
|
|
|
|
|
|
@bp.get("/second")
|
|
|
|
def second(request):
|
|
|
|
return text("second")
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
assert app.test_client.get("/one")[1].status == 200
|
|
|
|
assert app.test_client.get("/one/")[1].status == 200
|
|
|
|
|
|
|
|
assert app.test_client.get("/second")[1].status == 200
|
|
|
|
assert app.test_client.get("/second/")[1].status == 404
|
|
|
|
|
|
|
|
bp2 = Blueprint("bp2", strict_slashes=False)
|
|
|
|
|
|
|
|
@bp2.get("/third")
|
|
|
|
def third(request):
|
|
|
|
return text("third")
|
|
|
|
|
|
|
|
app.blueprint(bp2)
|
|
|
|
assert app.test_client.get("/third")[1].status == 200
|
|
|
|
assert app.test_client.get("/third/")[1].status == 200
|
|
|
|
|
|
|
|
@app.get("/f1", strict_slashes=False)
|
|
|
|
def f1(request):
|
|
|
|
return text("f1")
|
|
|
|
|
|
|
|
assert app.test_client.get("/f1")[1].status == 200
|
|
|
|
assert app.test_client.get("/f1/")[1].status == 200
|