def test_initalize_with_values(self): lambda_runner = Mock() local_service = LocalApigwService(Api(), lambda_runner, static_dir="dir/static", port=5000, host="129.0.0.0") self.assertEqual(local_service.port, 5000) self.assertEqual(local_service.host, "129.0.0.0") self.assertEqual(local_service.api.routes, []) self.assertEqual(local_service.static_dir, "dir/static") self.assertEqual(local_service.lambda_runner, lambda_runner)
def test_provider_with_valid_template(self, extract_api_mock): extract_api_mock.return_value = Api(routes={"set", "of", "values"}) template = {"Resources": {"a": "b"}} stack_mock = Mock(template_dict=template, resources=template["Resources"]) provider = ApiProvider([stack_mock]) self.assertEqual(len(provider.routes), 3) self.assertEqual(provider.routes, set(["set", "of", "values"]))
def setUp(self): self.function_name = Mock() self.api_gateway_route = Route(methods=["GET"], function_name=self.function_name, path="/") self.list_of_routes = [self.api_gateway_route] self.lambda_runner = Mock() self.lambda_runner.is_debugging.return_value = False self.stderr = Mock() self.api = Api(routes=self.list_of_routes) self.service = LocalApigwService(self.api, self.lambda_runner, port=3000, host="127.0.0.1", stderr=self.stderr)
def test_provider_with_valid_template(self, SamBaseProviderMock, extract_api_mock): extract_api_mock.return_value = Api(routes={"set", "of", "values"}) template = {"Resources": {"a": "b"}} SamBaseProviderMock.get_template.return_value = template provider = ApiProvider(template) self.assertEqual(len(provider.routes), 3) self.assertEqual(provider.routes, set(["set", "of", "values"])) self.assertEqual(provider.template_dict, {"Resources": {"a": "b"}}) self.assertEqual(provider.resources, {"a": "b"})
def test_must_raise_if_route_not_available( self, extract_api, log_routes_mock, make_static_dir_mock, SamApiProviderMock, ApiGwServiceMock ): routing_list = [] # Empty api = Api() extract_api.return_value = api SamApiProviderMock.extract_api.return_value = api SamApiProviderMock.return_value = self.api_provider_mock ApiGwServiceMock.return_value = self.apigw_service # Now start the service local_service = LocalApiService(self.lambda_invoke_context_mock, self.port, self.host, self.static_dir) local_service.api_provider.api.routes = routing_list with self.assertRaises(NoApisDefined): local_service.start()
def test_create_creates_dict_of_routes(self): function_name_1 = Mock() function_name_2 = Mock() api_gateway_route_1 = Route(methods=["GET"], function_name=function_name_1, path="/") api_gateway_route_2 = Route(methods=["POST"], function_name=function_name_2, path="/") list_of_routes = [api_gateway_route_1, api_gateway_route_2] lambda_runner = Mock() api = Api(routes=list_of_routes) service = LocalApigwService(api, lambda_runner) service.create() self.assertEqual(service._dict_of_routes, {"/:GET": api_gateway_route_1, "/:POST": api_gateway_route_2})
def get_api(self): """ Creates the api using the parts from the ApiCollector. The routes are also deduped so that there is no duplicate routes with the same function name, path, but different method. The normalised_routes are the routes that have been processed. By default, this will get all the routes. However, it can be changed to override the default value of normalised routes such as in SamApiProvider Return ------- An Api object with all the properties """ api = Api() routes = self.dedupe_function_routes(self.routes) routes = self.normalize_cors_methods(routes, self.cors) api.routes = routes api.binary_media_types_set = self.binary_media_types_set api.stage_name = self.stage_name api.stage_variables = self.stage_variables api.cors = self.cors return api