def test_error_response_case(self): def _error_response_func1(): raise WebApiError('fail567', status=500, result=None) decorated1 = json_endpoint(_error_response_func1) app1 = Bottle() app1.route('/', ['GET'], decorated1) wt1 = TestApp(app1) resp1 = wt1.get('/', status=500) eq_(resp1.status_int, 500) eq_(resp1.content_type, 'application/json') eq_(resp1.body, '{"status": "error", "message": "fail567", "result": null}') eq_(resp1.headers['Pragma'], 'no-cache') eq_(resp1.headers['Cache-Control'], 'no-cache') # test JSONP resp2 = wt1.get('/', params=dict(j='j345'), status=500) eq_(resp2.status_int, 500) eq_(resp2.content_type, 'application/javascript') eq_(resp2.body, 'j345({"status": "error", "message": "fail567", "result": null});') eq_(resp2.headers['Pragma'], 'no-cache') eq_(resp2.headers['Cache-Control'], 'no-cache')
def test_success_response_case(self): def _success_response_func1(): return dict(value=123) decorated1 = json_endpoint(_success_response_func1) app1 = Bottle() app1.route('/', ['GET'], decorated1) wt1 = TestApp(app1) resp1 = wt1.get('/', status=200) eq_(resp1.status_int, 200) eq_(resp1.content_type, 'application/json') eq_(resp1.body, '{"status": "ok", "result": {"value": 123}}') eq_(resp1.headers['Pragma'], 'no-cache') eq_(resp1.headers['Cache-Control'], 'no-cache') # test JSONP resp2 = wt1.get('/', params=dict(j='j345'), status=200) eq_(resp2.status_int, 200) eq_(resp2.content_type, 'application/javascript') eq_(resp2.body, 'j345({"status": "ok", "result": {"value": 123}});') eq_(resp2.headers['Pragma'], 'no-cache') eq_(resp2.headers['Cache-Control'], 'no-cache')