Merge pull request #27 from huge-success/master

Merge upstream master branch
This commit is contained in:
7 2018-10-11 22:34:09 -07:00 committed by GitHub
commit 4cb107aedc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 6 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

@ -1,6 +1,8 @@
import os
import types
from sanic.exceptions import PyFileError
SANIC_PREFIX = 'SANIC_'
@ -83,6 +85,9 @@ class Config(dict):
except IOError as e:
e.strerror = 'Unable to load configuration file (%s)' % e.strerror
raise
except Exception as e:
raise PyFileError(filename) from e
self.from_object(module)
return True

View File

@ -1,4 +1,4 @@
from sanic.http import STATUS_CODES
from sanic.helpers import STATUS_CODES
TRACEBACK_STYLE = '''
<style>
@ -223,6 +223,11 @@ class InvalidRangeType(ContentRangeError):
pass
class PyFileError(Exception):
def __init__(self, file):
super().__init__('could not execute config file %s', file)
@add_status_code(401)
class Unauthorized(SanicException):
"""

View File

@ -10,7 +10,7 @@ except BaseException:
from aiofiles import open as open_async
from multidict import CIMultiDict
from sanic import http
from sanic.helpers import STATUS_CODES, has_message_body, remove_entity_headers
from sanic.cookies import CookieJar
@ -103,7 +103,7 @@ class StreamingHTTPResponse(BaseHTTPResponse):
if self.status is 200:
status = b'OK'
else:
status = http.STATUS_CODES.get(self.status)
status = STATUS_CODES.get(self.status)
return (b'HTTP/%b %d %b\r\n'
b'%b'
@ -141,7 +141,7 @@ class HTTPResponse(BaseHTTPResponse):
timeout_header = b'Keep-Alive: %d\r\n' % keep_alive_timeout
body = b''
if http.has_message_body(self.status):
if has_message_body(self.status):
body = self.body
self.headers['Content-Length'] = self.headers.get(
'Content-Length', len(self.body))
@ -150,14 +150,14 @@ class HTTPResponse(BaseHTTPResponse):
'Content-Type', self.content_type)
if self.status in (304, 412):
self.headers = http.remove_entity_headers(self.headers)
self.headers = remove_entity_headers(self.headers)
headers = self._parse_headers()
if self.status is 200:
status = b'OK'
else:
status = http.STATUS_CODES.get(self.status, b'UNKNOWN RESPONSE')
status = STATUS_CODES.get(self.status, b'UNKNOWN RESPONSE')
return (b'HTTP/%b %d %b\r\n'
b'Connection: %b\r\n'

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}

View File

@ -6,6 +6,7 @@ from textwrap import dedent
import pytest
from sanic import Sanic
from sanic.exceptions import PyFileError
@contextmanager
@ -87,6 +88,15 @@ def test_load_from_missing_envvar(app):
"could not be loaded.")
def test_load_config_from_file_invalid_syntax(app):
config = "VALUE = some value"
with temp_path() as config_path:
config_path.write_text(config)
with pytest.raises(PyFileError):
app.config.from_pyfile(config_path)
def test_overwrite_exisiting_config(app):
app.config.DEFAULT = 1