diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-08-07 09:28:25 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-08-07 09:28:25 +0000 |
commit | 7784cc73b1905f8038249b071d3c766ef6180b1f (patch) | |
tree | 53b75250084b034e943f6b1662d9cd312ae7319c | |
parent | 52f756a9deb925744e6c3ec293ac6d3e94600eb1 (diff) | |
parent | 078cd723655b4cf52835a66f19bd9a2b85d0b73a (diff) | |
download | wsme-7784cc73b1905f8038249b071d3c766ef6180b1f.tar.gz |
Merge "rest: return 415 when content-type is invalid"
-rw-r--r-- | wsme/rest/args.py | 3 | ||||
-rw-r--r-- | wsme/root.py | 6 | ||||
-rw-r--r-- | wsme/tests/test_restjson.py | 9 |
3 files changed, 16 insertions, 2 deletions
diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 55fb4eb..5268e86 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -233,7 +233,8 @@ def args_from_body(funcdef, body, mimetype): elif mimetype in restxml.accept_content_types: dataformat = restxml else: - raise ValueError("Unknown mimetype: %s" % mimetype) + raise ClientSideError("Unknown mimetype: %s" % mimetype, + status_code=415) try: kw = dataformat.parse( diff --git a/wsme/root.py b/wsme/root.py index 1ccfbdf..c31ad96 100644 --- a/wsme/root.py +++ b/wsme/root.py @@ -203,6 +203,7 @@ class WSRoot(object): infos = wsme.api.format_exception(sys.exc_info(), self._debug) if isinstance(e, ClientSideError): request.client_errorcount += 1 + request.client_last_status_code = e.code else: request.server_errorcount += 1 return protocol.encode_error(context, infos) @@ -266,6 +267,7 @@ class WSRoot(object): request.calls = [] request.client_errorcount = 0 + request.client_last_status_code = None request.server_errorcount = 0 try: @@ -290,7 +292,9 @@ class WSRoot(object): if hasattr(protocol, 'get_response_status'): res.status = protocol.get_response_status(request) else: - if request.client_errorcount: + if request.client_errorcount == 1: + res.status = request.client_last_status_code + elif request.client_errorcount: res.status = 400 elif request.server_errorcount: res.status = 500 diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 4ed6423..3368703 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -254,6 +254,15 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase): print(r) 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): r = self.app.post('/argtypes/setint.json', '{"value": 2', headers={"Content-Type": "application/json"}, |