def test_errors(app, json_rpc, path, mocker): request_id = 1 params = (1, 2) method_name = 'test_method' def error_method(*args, **kwargs): raise exc.JsonRpcError(code=1, message='message') mock = mocker.Mock(name=method_name, side_effect=error_method) json_rpc.dispatcher.add(mock, method_name) with app.test_client() as cli: # method not found raw = cli.post(path, json=v20.Request(method='unknown_method', params=params, id=request_id).to_json()) assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) assert resp.id is request_id assert resp.is_error is True assert resp.error == exc.MethodNotFoundError( data="method 'unknown_method' not found") # customer error raw = cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.is_error is True assert resp.error == exc.JsonRpcError(code=1, message='message') # content type error raw = cli.post(path, data='') assert raw.status_code == 415 # malformed json raw = cli.post(path, headers={'Content-Type': 'application/json'}, data='') assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) assert resp.id is None assert resp.is_error is True assert resp.error == exc.ParseError(data=_) # decoding error raw = cli.post(path, headers={'Content-Type': 'application/json'}, data=b'\xff') assert raw.status_code == 400
def test_errors(json_rpc, path, mocker): request_id = 1 params = (1, 2) method_name = 'test_method' def error_method(*args, **kwargs): raise exc.JsonRpcError(code=1, message='message') mock = mocker.Mock(name=method_name, side_effect=error_method) json_rpc.dispatcher.add(mock, method_name) cli = werkzeug.test.Client(json_rpc) # method not found body_iter, code, header = cli.post( path, json=v20.Request(method='unknown_method', params=params, id=request_id).to_json(), ) body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) assert resp.id is request_id assert resp.is_error is True assert resp.error == exc.MethodNotFoundError( data="method 'unknown_method' not found") # customer error body_iter, code, header = cli.post( path, json=v20.Request(method=method_name, params=params, id=request_id).to_json(), ) body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.is_error is True assert resp.error == exc.JsonRpcError(code=1, message='message') # malformed json body_iter, code, header = cli.post( path, headers={'Content-Type': 'application/json'}, data='') body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) assert resp.id is None assert resp.is_error is True assert resp.error == exc.ParseError(data=_)
def error_method(*args, **kwargs): raise exc.JsonRpcError(code=1, message='message')