Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if response is malformed. #223

Merged
merged 1 commit into from
Dec 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sanic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from time import time
from httptools import HttpRequestParser
from httptools.parser.errors import HttpParserError
from .exceptions import ServerError

try:
import uvloop as async_loop
Expand Down Expand Up @@ -173,8 +174,9 @@ def write_error(self, exception):
"Writing error failed, connection closed {}".format(e))

def bail_out(self, message):
log.debug(message)
self.transport.close()
exception = ServerError(message)
self.write_error(exception)
log.error(message)

def cleanup(self):
self.parser = None
Expand Down
17 changes: 17 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sanic import Sanic
from sanic.response import json, text
from sanic.utils import sanic_endpoint_test
from sanic.exceptions import ServerError


# ------------------------------------------------------------ #
Expand Down Expand Up @@ -32,6 +33,22 @@ async def handler(request):
assert response.text == 'Hello'


def test_invalid_response():
app = Sanic('test_invalid_response')

@app.exception(ServerError)
def handler_exception(request, exception):
return text('Internal Server Error.', 500)

@app.route('/')
async def handler(request):
return 'This should fail'

request, response = sanic_endpoint_test(app)
assert response.status == 500
assert response.text == "Internal Server Error."


def test_json():
app = Sanic('test_json')

Expand Down