def test_JsonHandler_render_response(app):
    h = api.JsonHandler()
    with app.test_request_context():
        g.request_id = 'RQID'
        eq_(json.loads(h.render_response([1, 2, 3], 200, {}).data), {
            'result': [1, 2, 3],
            'request_id': 'RQID'
        })
def test_JsonHandler_handle_exception_httpexception(app):
    """JsonHandler handles HTTP exceptions with an error response containing
    information about the status code"""
    h = api.JsonHandler()
    with app.test_request_context():
        g.request_id = 'RQID'
        try:
            raise BadRequest()
        except Exception:
            resp = h.handle_exception(*sys.exc_info())
            eq_(resp.status_code, 400)
            eq_(json.loads(resp.data)['error']['code'], 400)
def test_JsonHandler_handle_exception(app):
    """JsonHandler handles regular exceptions with an error response"""
    h = api.JsonHandler()
    with app.test_request_context():
        g.request_id = 'RQID'
        app.debug = True
        # mock out log_exception, since it prints to stdout
        with mock.patch("flask.current_app.log_exception"):
            try:
                raise RuntimeError('oh noes')
            except Exception:
                resp = h.handle_exception(*sys.exc_info())
                eq_(resp.status_code, 500)
                data = json.loads(resp.data)
                eq_(data['error']['code'], 500)
                assert 'oh noes' in data['error']['description']