Merge pull request #1334 from chenjr0719/master

Fix TypeError when use Blueprint.group() to group blueprint with defa…
This commit is contained in:
Stephen Sadowski 2018-10-11 07:28:10 -05:00 committed by GitHub
commit 298d5cdf24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -54,6 +54,8 @@ class Blueprint:
yield i
bps = []
for bp in chain(blueprints):
if bp.url_prefix is None:
bp.url_prefix = ''
bp.url_prefix = url_prefix + bp.url_prefix
bps.append(bp)
return bps

View File

@ -499,3 +499,40 @@ def test_bp_group(app):
request, response = app.test_client.get('/mid/deep1/bottom')
assert response.text == 'D1B_OK'
def test_bp_group_with_default_url_prefix(app):
from sanic.response import json
bp_resources = Blueprint('bp_resources')
@bp_resources.get('/')
def list_resources_handler(request):
resource = {}
return json([resource])
bp_resource = Blueprint('bp_resource', url_prefix='/<resource_id>')
@bp_resource.get('/')
def get_resource_hander(request, resource_id):
resource = {'resource_id': resource_id}
return json(resource)
bp_resources_group = Blueprint.group(bp_resources, bp_resource, url_prefix='/resources')
bp_api_v1 = Blueprint('bp_api_v1')
@bp_api_v1.get('/info')
def api_v1_info(request):
return text('api_version: v1')
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')
app.blueprint(bp_api_group)
request, response = app.test_client.get('/api/v1/info')
assert response.text == 'api_version: v1'
request, response = app.test_client.get('/api/v1/resources')
assert response.json == [{}]
from uuid import uuid4
resource_id = str(uuid4())
request, response = app.test_client.get('/api/v1/resources/{0}'.format(resource_id))
assert response.json == {'resource_id': resource_id}