Projet

Général

Profil

0001-api-always-return-errors-are-returned-as-json-9007.patch

Frédéric Péters, 25 novembre 2015 14:07

Télécharger (2,44 ko)

Voir les différences:

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(+)
tests/test_api.py
77 77
    output = get_app(pub).get('/user')
78 78
    assert output.headers.get('location') == 'http://example.net/myspace/'
79 79

  
80
def test_user_page_error(pub):
81
    # check we get json as output for errors
82
    output = get_app(pub).get('/api/user/', status=403)
83
    assert output.json['err_desc'] == 'no user specified'
84

  
80 85
def test_user_page_error_when_json_and_no_user(pub):
81 86
    output = get_app(pub).get('/api/user/?format=json', status=403)
82 87
    assert output.json['err_desc'] == 'no user specified'
wcs/api.py
588 588
            list_roles.append(role.get_json_export_dict())
589 589
        get_response().set_content_type('application/json')
590 590
        return json.dumps({'data': list_roles})
591

  
592
    def _q_traverse(self, path):
593
        get_request().is_json_marker = True
594
        return super(ApiDirectory, self)._q_traverse(path)
wcs/qommon/http_request.py
29 29
        quixote.http_request.HTTPRequest.__init__(self, *args, **kwargs)
30 30
        self.response = HTTPResponse()
31 31
        self.charset = get_publisher().site_charset
32
        self.is_json_marker = None
32 33

  
33 34
    _user = () # use empty tuple instead of None as None is a "valid" user value
34 35
    def get_user(self):
......
119 120
                self.form[k] = v.encode(self.charset)
120 121

  
121 122
    def is_json(self):
123
        if self.is_json_marker:
124
            return True
122 125
        if self.get_header('Content-Type', '').strip() == 'application/json':
123 126
            return True
124 127
        if self.get_header('Accept', '').strip() == 'application/json':
125
-