def test_runtime_error_raised_when_app_not_created(self): is_debugging = False service = BaseLocalService(is_debugging=is_debugging, port=3000, host="127.0.0.1") with self.assertRaises(RuntimeError): service.run()
def test_create_returns_not_implemented(self): is_debugging = False service = BaseLocalService(is_debugging=is_debugging, port=3000, host="127.0.0.1") with self.assertRaises(NotImplementedError): service.create()
def test_run_starts_service_multithreaded(self): is_debugging = False # multithreaded service = BaseLocalService(is_debugging=is_debugging, port=3000, host='127.0.0.1') service._app = Mock() app_run_mock = Mock() service._app.run = app_run_mock service.run() app_run_mock.assert_called_once_with(threaded=True, host='127.0.0.1', port=3000)
def test_run_starts_service_singlethreaded(self): is_debugging = True # singlethreaded service = BaseLocalService(is_debugging=is_debugging, port=3000, host="127.0.0.1") service._app = Mock() app_run_mock = Mock() service._app.run = app_run_mock service.run() app_run_mock.assert_called_once_with(threaded=False, host="127.0.0.1", port=3000)
def test_service_response(self, flask_response_patch): flask_response_mock = Mock() flask_response_patch.return_value = flask_response_mock body = "this is the body" status_code = 200 headers = {"Content-Type": "application/json"} actual_response = BaseLocalService.service_response( body, headers, status_code) flask_response_patch.assert_called_once_with("this is the body") self.assertEqual(actual_response.status_code, 200) self.assertEqual(actual_response.headers, {"Content-Type": "application/json"})
def not_implemented_locally(message): """ Creates a Lambda Service NotImplementedLocally Response Parameters ---------- message str Message to be added to the body of the response Returns ------- Flask.Response A response object representing the NotImplementedLocally Error """ exception_tuple = LambdaErrorResponses.NotImplementedException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.LOCAL_SERVICE_ERROR, message), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1], )
def invalid_request_content(message): """ Creates a Lambda Service InvalidRequestContent Response Parameters ---------- message str Message to be added to the body of the response Returns ------- Flask.Response A response object representing the InvalidRequestContent Error """ exception_tuple = LambdaErrorResponses.InvalidRequestContentException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, message), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1], )
def invalid_request_content(message): """ Creates a Lambda Service InvalidRequestContent Response Parameters ---------- message str Message to be added to the body of the response Returns ------- Flask.Response A response object representing the InvalidRequestContent Error """ exception_tuple = LambdaErrorResponses.InvalidRequestContentException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, message), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1] )
def generic_service_exception(*args): """ Creates a Lambda Service Generic ServiceException Response Parameters ---------- args list List of arguments Flask passes to the method Returns ------- Flask.Response A response object representing the GenericServiceException Error """ exception_tuple = LambdaErrorResponses.ServiceException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.SERVICE_ERROR, "ServiceException"), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1], )
def not_implemented_locally(message): """ Creates a Lambda Service NotImplementedLocally Response Parameters ---------- message str Message to be added to the body of the response Returns ------- Flask.Response A response object representing the NotImplementedLocally Error """ exception_tuple = LambdaErrorResponses.NotImplementedException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.LOCAL_SERVICE_ERROR, message), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1] )
def generic_service_exception(*args): """ Creates a Lambda Service Generic ServiceException Response Parameters ---------- args list List of arguments Flask passes to the method Returns ------- Flask.Response A response object representing the GenericServiceException Error """ exception_tuple = LambdaErrorResponses.ServiceException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.SERVICE_ERROR, "ServiceException"), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1] )
def unsupported_media_type(content_type): """ Creates a Lambda Service UnsupportedMediaType Response Parameters ---------- content_type str Content Type of the request that was made Returns ------- Flask.Response A response object representing the UnsupportedMediaType Error """ exception_tuple = LambdaErrorResponses.UnsupportedMediaTypeException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body( LambdaErrorResponses.USER_ERROR, "Unsupported content type: {}".format(content_type)), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1])
def generic_method_not_allowed(*args): """ Creates a Lambda Service Generic MethodNotAllowed Response Parameters ---------- args list List of arguments Flask passes to the method Returns ------- Flask.Response A response object representing the GenericMethodNotAllowed Error """ exception_tuple = LambdaErrorResponses.MethodNotAllowedException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body( LambdaErrorResponses.LOCAL_SERVICE_ERROR, "MethodNotAllowedException"), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1])
def unsupported_media_type(content_type): """ Creates a Lambda Service UnsupportedMediaType Response Parameters ---------- content_type str Content Type of the request that was made Returns ------- Flask.Response A response object representing the UnsupportedMediaType Error """ exception_tuple = LambdaErrorResponses.UnsupportedMediaTypeException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body(LambdaErrorResponses.USER_ERROR, "Unsupported content type: {}".format(content_type)), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1] )
def resource_not_found(function_name): """ Creates a Lambda Service ResourceNotFound Response Parameters ---------- function_name str Name of the function that was requested to invoke Returns ------- Flask.Response A response object representing the ResourceNotFound Error """ exception_tuple = LambdaErrorResponses.ResourceNotFoundException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body( LambdaErrorResponses.USER_ERROR, "Function not found: arn:aws:lambda:us-west-2:012345678901:function:{}" .format(function_name)), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1])
def resource_not_found(function_name): """ Creates a Lambda Service ResourceNotFound Response Parameters ---------- function_name str Name of the function that was requested to invoke Returns ------- Flask.Response A response object representing the ResourceNotFound Error """ exception_tuple = LambdaErrorResponses.ResourceNotFoundException return BaseLocalService.service_response( LambdaErrorResponses._construct_error_response_body( LambdaErrorResponses.USER_ERROR, "Function not found: arn:aws:lambda:us-west-2:012345678901:function:{}".format(function_name) ), LambdaErrorResponses._construct_headers(exception_tuple[0]), exception_tuple[1] )