Merge branch 'master' of github.com:chenjr0719/sanic

This commit is contained in:
jacob 2018-10-10 14:04:45 +08:00
commit 801258c46a
3 changed files with 20 additions and 21 deletions

View File

@ -150,10 +150,7 @@ class HttpProtocol(asyncio.Protocol):
self._request_stream_task.cancel() self._request_stream_task.cancel()
if self._request_handler_task: if self._request_handler_task:
self._request_handler_task.cancel() self._request_handler_task.cancel()
try: self.write_error(RequestTimeout('Request Timeout'))
raise RequestTimeout('Request Timeout')
except RequestTimeout as exception:
self.write_error(exception)
def response_timeout_callback(self): def response_timeout_callback(self):
# Check if elapsed time since response was initiated exceeds our # Check if elapsed time since response was initiated exceeds our
@ -170,10 +167,7 @@ class HttpProtocol(asyncio.Protocol):
self._request_stream_task.cancel() self._request_stream_task.cancel()
if self._request_handler_task: if self._request_handler_task:
self._request_handler_task.cancel() self._request_handler_task.cancel()
try: self.write_error(ServiceUnavailable('Response Timeout'))
raise ServiceUnavailable('Response Timeout')
except ServiceUnavailable as exception:
self.write_error(exception)
def keep_alive_timeout_callback(self): def keep_alive_timeout_callback(self):
# Check if elapsed time since last response exceeds our configured # Check if elapsed time since last response exceeds our configured
@ -199,8 +193,7 @@ class HttpProtocol(asyncio.Protocol):
# memory limits # memory limits
self._total_request_size += len(data) self._total_request_size += len(data)
if self._total_request_size > self.request_max_size: if self._total_request_size > self.request_max_size:
exception = PayloadTooLarge('Payload Too Large') self.write_error(PayloadTooLarge('Payload Too Large'))
self.write_error(exception)
# Create parser if this is the first time we're receiving data # Create parser if this is the first time we're receiving data
if self.parser is None: if self.parser is None:
@ -218,8 +211,7 @@ class HttpProtocol(asyncio.Protocol):
message = 'Bad Request' message = 'Bad Request'
if self._debug: if self._debug:
message += '\n' + traceback.format_exc() message += '\n' + traceback.format_exc()
exception = InvalidUsage(message) self.write_error(InvalidUsage(message))
self.write_error(exception)
def on_url(self, url): def on_url(self, url):
if not self.url: if not self.url:
@ -233,8 +225,7 @@ class HttpProtocol(asyncio.Protocol):
if value is not None: if value is not None:
if self._header_fragment == b'Content-Length' \ if self._header_fragment == b'Content-Length' \
and int(value) > self.request_max_size: and int(value) > self.request_max_size:
exception = PayloadTooLarge('Payload Too Large') self.write_error(PayloadTooLarge('Payload Too Large'))
self.write_error(exception)
try: try:
value = value.decode() value = value.decode()
except UnicodeDecodeError: except UnicodeDecodeError:
@ -433,7 +424,7 @@ class HttpProtocol(asyncio.Protocol):
self.log_response(response) self.log_response(response)
try: try:
self.transport.close() self.transport.close()
except AttributeError as e: except AttributeError:
logger.debug('Connection lost before server could close it.') logger.debug('Connection lost before server could close it.')
def bail_out(self, message, from_error=False): def bail_out(self, message, from_error=False):
@ -443,8 +434,7 @@ class HttpProtocol(asyncio.Protocol):
self.transport.get_extra_info('peername')) self.transport.get_extra_info('peername'))
logger.debug('Exception:\n%s', traceback.format_exc()) logger.debug('Exception:\n%s', traceback.format_exc())
else: else:
exception = ServerError(message) self.write_error(ServerError(message))
self.write_error(exception)
logger.error(message) logger.error(message)
def cleanup(self): def cleanup(self):

View File

@ -15,24 +15,28 @@ def test_load_from_object(app):
assert app.config.CONFIG_VALUE == 'should be used' assert app.config.CONFIG_VALUE == 'should be used'
assert 'not_for_config' not in app.config assert 'not_for_config' not in app.config
def test_auto_load_env(): def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic() app = Sanic()
assert app.config.TEST_ANSWER == 42 assert app.config.TEST_ANSWER == 42
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_dont_load_env(): def test_dont_load_env():
environ["SANIC_TEST_ANSWER"] = "42" environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic(load_env=False) app = Sanic(load_env=False)
assert getattr(app.config, 'TEST_ANSWER', None) == None assert getattr(app.config, 'TEST_ANSWER', None) is None
del environ["SANIC_TEST_ANSWER"] del environ["SANIC_TEST_ANSWER"]
def test_load_env_prefix(): def test_load_env_prefix():
environ["MYAPP_TEST_ANSWER"] = "42" environ["MYAPP_TEST_ANSWER"] = "42"
app = Sanic(load_env='MYAPP_') app = Sanic(load_env='MYAPP_')
assert app.config.TEST_ANSWER == 42 assert app.config.TEST_ANSWER == 42
del environ["MYAPP_TEST_ANSWER"] del environ["MYAPP_TEST_ANSWER"]
def test_load_from_file(app): def test_load_from_file(app):
config = b""" config = b"""
VALUE = 'some value' VALUE = 'some value'
@ -68,12 +72,16 @@ def test_load_from_envvar(app):
def test_load_from_missing_envvar(app): def test_load_from_missing_envvar(app):
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError) as e:
app.config.from_envvar('non-existent variable') app.config.from_envvar('non-existent variable')
assert str(e.value) == ("The environment variable 'non-existent "
"variable' is not set and thus configuration "
"could not be loaded.")
def test_overwrite_exisiting_config(app): def test_overwrite_exisiting_config(app):
app.config.DEFAULT = 1 app.config.DEFAULT = 1
class Config: class Config:
DEFAULT = 2 DEFAULT = 2
@ -82,5 +90,6 @@ def test_overwrite_exisiting_config(app):
def test_missing_config(app): def test_missing_config(app):
with pytest.raises(AttributeError): with pytest.raises(AttributeError) as e:
app.config.NON_EXISTENT app.config.NON_EXISTENT
assert str(e.value) == ("Config has no 'NON_EXISTENT'")