def test_must_raise_if_no_api_defined(self, local_api_service_mock, invoke_context_mock): # Mock the __enter__ method to return a object inside a context manager context_mock = Mock() invoke_context_mock.return_value.__enter__.return_value = context_mock service_mock = Mock() local_api_service_mock.return_value = service_mock service_mock.start.side_effect = NoApisDefined("no apis") with self.assertRaises(UserException) as context: self.call_cli() msg = str(context.exception) expected = "Template does not have any APIs connected to Lambda functions" self.assertEqual(msg, expected)
def start(self): """ Creates and starts the local API Gateway service. This method will block until the service is stopped manually using an interrupt. After the service is started, callers can make HTTP requests to the endpoint to invoke the Lambda function and receive a response. NOTE: This is a blocking call that will not return until the thread is interrupted with SIGINT/SIGTERM """ if not self.api_provider.api.routes: raise NoApisDefined("No APIs available in template") static_dir_path = self._make_static_dir_path(self.cwd, self.static_dir) # We care about passing only stderr to the Service and not stdout because stdout from Docker container # contains the response to the API which is sent out as HTTP response. Only stderr needs to be printed # to the console or a log file. stderr from Docker container contains runtime logs and output of print # statements from the Lambda function service = LocalApigwService( api=self.api_provider.api, lambda_runner=self.lambda_runner, static_dir=static_dir_path, port=self.port, host=self.host, stderr=self.stderr_stream, ) service.create() # Print out the list of routes that will be mounted self._print_routes(self.api_provider.api.routes, self.host, self.port) LOG.info( "You can now browse to the above endpoints to invoke your functions. " "You do not need to restart/reload SAM CLI while working on your functions, " "changes will be reflected instantly/automatically. You only need to restart " "SAM CLI if you update your AWS SAM template" ) service.run()