Merge "rest: return 415 when content-type is invalid"
This commit is contained in:
commit
7784cc73b1
@ -233,7 +233,8 @@ def args_from_body(funcdef, body, mimetype):
|
|||||||
elif mimetype in restxml.accept_content_types:
|
elif mimetype in restxml.accept_content_types:
|
||||||
dataformat = restxml
|
dataformat = restxml
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unknown mimetype: %s" % mimetype)
|
raise ClientSideError("Unknown mimetype: %s" % mimetype,
|
||||||
|
status_code=415)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
kw = dataformat.parse(
|
kw = dataformat.parse(
|
||||||
|
@ -203,6 +203,7 @@ class WSRoot(object):
|
|||||||
infos = wsme.api.format_exception(sys.exc_info(), self._debug)
|
infos = wsme.api.format_exception(sys.exc_info(), self._debug)
|
||||||
if isinstance(e, ClientSideError):
|
if isinstance(e, ClientSideError):
|
||||||
request.client_errorcount += 1
|
request.client_errorcount += 1
|
||||||
|
request.client_last_status_code = e.code
|
||||||
else:
|
else:
|
||||||
request.server_errorcount += 1
|
request.server_errorcount += 1
|
||||||
return protocol.encode_error(context, infos)
|
return protocol.encode_error(context, infos)
|
||||||
@ -266,6 +267,7 @@ class WSRoot(object):
|
|||||||
|
|
||||||
request.calls = []
|
request.calls = []
|
||||||
request.client_errorcount = 0
|
request.client_errorcount = 0
|
||||||
|
request.client_last_status_code = None
|
||||||
request.server_errorcount = 0
|
request.server_errorcount = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -290,7 +292,9 @@ class WSRoot(object):
|
|||||||
if hasattr(protocol, 'get_response_status'):
|
if hasattr(protocol, 'get_response_status'):
|
||||||
res.status = protocol.get_response_status(request)
|
res.status = protocol.get_response_status(request)
|
||||||
else:
|
else:
|
||||||
if request.client_errorcount:
|
if request.client_errorcount == 1:
|
||||||
|
res.status = request.client_last_status_code
|
||||||
|
elif request.client_errorcount:
|
||||||
res.status = 400
|
res.status = 400
|
||||||
elif request.server_errorcount:
|
elif request.server_errorcount:
|
||||||
res.status = 500
|
res.status = 500
|
||||||
|
@ -254,6 +254,15 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
|
|||||||
print(r)
|
print(r)
|
||||||
assert json.loads(r.text) == 2
|
assert json.loads(r.text) == 2
|
||||||
|
|
||||||
|
def test_invalid_content_type_body(self):
|
||||||
|
r = self.app.post('/argtypes/setint.json', '{"value": 2}',
|
||||||
|
headers={"Content-Type": "application/invalid"},
|
||||||
|
expect_errors=True)
|
||||||
|
print(r)
|
||||||
|
assert r.status_int == 415
|
||||||
|
assert json.loads(r.text)['faultstring'] == \
|
||||||
|
"Unknown mimetype: application/invalid"
|
||||||
|
|
||||||
def test_invalid_json_body(self):
|
def test_invalid_json_body(self):
|
||||||
r = self.app.post('/argtypes/setint.json', '{"value": 2',
|
r = self.app.post('/argtypes/setint.json', '{"value": 2',
|
||||||
headers={"Content-Type": "application/json"},
|
headers={"Content-Type": "application/json"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user