2017-02-21 06:41:53 +00:00
|
|
|
import asyncio
|
2016-10-24 09:21:06 +01:00
|
|
|
import inspect
|
|
|
|
|
2016-10-15 20:38:12 +01:00
|
|
|
from sanic import Sanic
|
2016-10-16 09:48:51 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
2016-10-15 20:38:12 +01:00
|
|
|
from sanic.response import json, text
|
2016-10-16 09:48:51 +01:00
|
|
|
from sanic.exceptions import NotFound, ServerError, InvalidUsage
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
def test_bp():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
bp = Blueprint('test_text')
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
assert response.text == 'Hello'
|
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def test_bp_strict_slash():
|
|
|
|
app = Sanic('test_route_strict_slash')
|
|
|
|
bp = Blueprint('test_text')
|
|
|
|
|
|
|
|
@bp.get('/get', strict_slashes=True)
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
@bp.post('/post/', strict_slashes=True)
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/get')
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/get/')
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/post/')
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
|
|
|
request, response = app.test_client.post('/post')
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
2016-10-15 20:38:12 +01:00
|
|
|
def test_bp_with_url_prefix():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
bp = Blueprint('test_text', url_prefix='/test1')
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test1/')
|
2016-10-15 20:38:12 +01:00
|
|
|
|
|
|
|
assert response.text == 'Hello'
|
|
|
|
|
|
|
|
|
|
|
|
def test_several_bp_with_url_prefix():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
bp = Blueprint('test_text', url_prefix='/test1')
|
|
|
|
bp2 = Blueprint('test_text2', url_prefix='/test2')
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
|
|
|
|
|
|
|
@bp2.route('/')
|
|
|
|
def handler2(request):
|
|
|
|
return text('Hello2')
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(bp)
|
|
|
|
app.blueprint(bp2)
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test1/')
|
2016-10-15 20:38:12 +01:00
|
|
|
assert response.text == 'Hello'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test2/')
|
2016-10-15 20:38:12 +01:00
|
|
|
assert response.text == 'Hello2'
|
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
def test_bp_with_host():
|
|
|
|
app = Sanic('test_bp_host')
|
|
|
|
bp = Blueprint('test_bp_host', url_prefix='/test1', host="example.com")
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
|
|
|
|
2017-01-11 06:08:15 +00:00
|
|
|
@bp.route('/', host="sub.example.com")
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello subdomain!')
|
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
app.blueprint(bp)
|
|
|
|
headers = {"Host": "example.com"}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/test1/',
|
|
|
|
headers=headers)
|
2017-01-11 02:07:58 +00:00
|
|
|
assert response.text == 'Hello'
|
|
|
|
|
2017-01-11 06:08:15 +00:00
|
|
|
headers = {"Host": "sub.example.com"}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/test1/',
|
|
|
|
headers=headers)
|
2017-01-11 06:08:15 +00:00
|
|
|
|
|
|
|
assert response.text == 'Hello subdomain!'
|
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
|
|
|
|
def test_several_bp_with_host():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
bp = Blueprint('test_text',
|
|
|
|
url_prefix='/test',
|
|
|
|
host="example.com")
|
|
|
|
bp2 = Blueprint('test_text2',
|
|
|
|
url_prefix='/test',
|
|
|
|
host="sub.example.com")
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
|
|
|
|
|
|
|
@bp2.route('/')
|
|
|
|
def handler2(request):
|
|
|
|
return text('Hello2')
|
|
|
|
|
|
|
|
@bp2.route('/other/')
|
|
|
|
def handler2(request):
|
|
|
|
return text('Hello3')
|
|
|
|
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
app.blueprint(bp2)
|
|
|
|
|
|
|
|
assert bp.host == "example.com"
|
|
|
|
headers = {"Host": "example.com"}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/test/',
|
|
|
|
headers=headers)
|
2017-01-11 02:07:58 +00:00
|
|
|
assert response.text == 'Hello'
|
|
|
|
|
|
|
|
assert bp2.host == "sub.example.com"
|
|
|
|
headers = {"Host": "sub.example.com"}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/test/',
|
|
|
|
headers=headers)
|
2017-01-11 02:07:58 +00:00
|
|
|
|
|
|
|
assert response.text == 'Hello2'
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get(
|
|
|
|
'/test/other/',
|
|
|
|
headers=headers)
|
2017-01-11 02:07:58 +00:00
|
|
|
assert response.text == 'Hello3'
|
2016-10-16 09:48:51 +01:00
|
|
|
|
|
|
|
def test_bp_middleware():
|
|
|
|
app = Sanic('test_middleware')
|
|
|
|
blueprint = Blueprint('test_middleware')
|
|
|
|
|
|
|
|
@blueprint.middleware('response')
|
|
|
|
async def process_response(request, response):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('FAIL')
|
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
app.blueprint(blueprint)
|
2016-10-16 09:48:51 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-16 09:48:51 +01:00
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
|
|
|
def test_bp_exception_handler():
|
|
|
|
app = Sanic('test_middleware')
|
|
|
|
blueprint = Blueprint('test_middleware')
|
|
|
|
|
|
|
|
@blueprint.route('/1')
|
|
|
|
def handler_1(request):
|
|
|
|
raise InvalidUsage("OK")
|
|
|
|
|
|
|
|
@blueprint.route('/2')
|
|
|
|
def handler_2(request):
|
|
|
|
raise ServerError("OK")
|
|
|
|
|
|
|
|
@blueprint.route('/3')
|
|
|
|
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
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/1')
|
2016-10-16 09:48:51 +01:00
|
|
|
assert response.status == 400
|
|
|
|
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/2')
|
2016-10-16 09:48:51 +01:00
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/3')
|
2016-10-21 12:11:18 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
def test_bp_listeners():
|
|
|
|
app = Sanic('test_middleware')
|
|
|
|
blueprint = Blueprint('test_middleware')
|
|
|
|
|
|
|
|
order = []
|
|
|
|
|
|
|
|
@blueprint.listener('before_server_start')
|
|
|
|
def handler_1(sanic, loop):
|
|
|
|
order.append(1)
|
|
|
|
|
|
|
|
@blueprint.listener('after_server_start')
|
|
|
|
def handler_2(sanic, loop):
|
|
|
|
order.append(2)
|
|
|
|
|
|
|
|
@blueprint.listener('after_server_start')
|
|
|
|
def handler_3(sanic, loop):
|
|
|
|
order.append(3)
|
|
|
|
|
|
|
|
@blueprint.listener('before_server_stop')
|
|
|
|
def handler_4(sanic, loop):
|
|
|
|
order.append(5)
|
|
|
|
|
|
|
|
@blueprint.listener('before_server_stop')
|
|
|
|
def handler_5(sanic, loop):
|
|
|
|
order.append(4)
|
|
|
|
|
|
|
|
@blueprint.listener('after_server_stop')
|
|
|
|
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
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
assert order == [1,2,3,4,5,6]
|
|
|
|
|
|
|
|
def test_bp_static():
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
with open(current_file, 'rb') as file:
|
|
|
|
current_file_contents = file.read()
|
|
|
|
|
|
|
|
app = Sanic('test_static')
|
|
|
|
blueprint = Blueprint('test_static')
|
|
|
|
|
2016-10-25 10:45:28 +01:00
|
|
|
blueprint.static('/testing.file', current_file)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
app.blueprint(blueprint)
|
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
|
|
|
def test_bp_shorthand():
|
|
|
|
app = Sanic('test_shorhand_routes')
|
|
|
|
blueprint = Blueprint('test_shorhand_routes')
|
2017-02-21 06:41:53 +00:00
|
|
|
ev = asyncio.Event()
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.get('/get')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.put('/put')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.post('/post')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.head('/head')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.options('/options')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.patch('/patch')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-01-30 07:21:00 +00:00
|
|
|
@blueprint.delete('/delete')
|
2017-01-30 07:20:38 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-21 06:41:53 +00:00
|
|
|
@blueprint.websocket('/ws')
|
|
|
|
async def handler(request, ws):
|
|
|
|
ev.set()
|
|
|
|
|
2017-01-30 07:20:38 +00:00
|
|
|
app.blueprint(blueprint)
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/get')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/get')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.put('/put')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/post')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/post')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/post')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/head')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.options('/options')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/options')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.patch('/patch')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/patch')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.delete('/delete')
|
2017-01-30 07:20:38 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
|
|
|
request, response = app.test_client.get('/ws', headers={
|
|
|
|
'Upgrade': 'websocket',
|
|
|
|
'Connection': 'upgrade',
|
|
|
|
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==',
|
|
|
|
'Sec-WebSocket-Version': '13'})
|
|
|
|
assert response.status == 101
|
|
|
|
assert ev.is_set()
|