Merge pull request #186 from r0fls/master
return 400 on invalid json post data
This commit is contained in:
commit
6abaa78f9e
|
@ -4,6 +4,7 @@ from http.cookies import SimpleCookie
|
|||
from httptools import parse_url
|
||||
from urllib.parse import parse_qs
|
||||
from ujson import loads as json_loads
|
||||
from sanic.exceptions import InvalidUsage
|
||||
|
||||
from .log import log
|
||||
|
||||
|
@ -67,7 +68,7 @@ class Request(dict):
|
|||
try:
|
||||
self.parsed_json = json_loads(self.body)
|
||||
except Exception:
|
||||
log.exception("Failed when parsing body as json")
|
||||
raise InvalidUsage("Failed when parsing body as json")
|
||||
|
||||
return self.parsed_json
|
||||
|
||||
|
|
|
@ -49,6 +49,19 @@ def test_json():
|
|||
assert results.get('test') == True
|
||||
|
||||
|
||||
def test_invalid_json():
|
||||
app = Sanic('test_json')
|
||||
|
||||
@app.route('/')
|
||||
async def handler(request):
|
||||
return json(request.json())
|
||||
|
||||
data = "I am not json"
|
||||
request, response = sanic_endpoint_test(app, data=data)
|
||||
|
||||
assert response.status == 400
|
||||
|
||||
|
||||
def test_query_string():
|
||||
app = Sanic('test_query_string')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user