From dda3149302b397ac2e6920c0a5c33441781b8082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 25 Nov 2015 14:06:28 +0100 Subject: [PATCH] api: always return errors are returned as json (#9007) --- tests/test_api.py | 5 +++++ wcs/api.py | 4 ++++ wcs/qommon/http_request.py | 3 +++ 3 files changed, 12 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index d9e9f09..11681e3 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -77,6 +77,11 @@ def test_user_page_redirect(pub): output = get_app(pub).get('/user') assert output.headers.get('location') == 'http://example.net/myspace/' +def test_user_page_error(pub): + # check we get json as output for errors + output = get_app(pub).get('/api/user/', status=403) + assert output.json['err_desc'] == 'no user specified' + def test_user_page_error_when_json_and_no_user(pub): output = get_app(pub).get('/api/user/?format=json', status=403) assert output.json['err_desc'] == 'no user specified' diff --git a/wcs/api.py b/wcs/api.py index a9b19b0..318ffae 100644 --- a/wcs/api.py +++ b/wcs/api.py @@ -588,3 +588,7 @@ class ApiDirectory(Directory): list_roles.append(role.get_json_export_dict()) get_response().set_content_type('application/json') return json.dumps({'data': list_roles}) + + def _q_traverse(self, path): + get_request().is_json_marker = True + return super(ApiDirectory, self)._q_traverse(path) diff --git a/wcs/qommon/http_request.py b/wcs/qommon/http_request.py index 1934b9c..d4dc578 100644 --- a/wcs/qommon/http_request.py +++ b/wcs/qommon/http_request.py @@ -29,6 +29,7 @@ class HTTPRequest(quixote.http_request.HTTPRequest): quixote.http_request.HTTPRequest.__init__(self, *args, **kwargs) self.response = HTTPResponse() self.charset = get_publisher().site_charset + self.is_json_marker = None _user = () # use empty tuple instead of None as None is a "valid" user value def get_user(self): @@ -119,6 +120,8 @@ class HTTPRequest(quixote.http_request.HTTPRequest): self.form[k] = v.encode(self.charset) def is_json(self): + if self.is_json_marker: + return True if self.get_header('Content-Type', '').strip() == 'application/json': return True if self.get_header('Accept', '').strip() == 'application/json': -- 2.6.2