diff --git a/moto/apigateway/exceptions.py b/moto/apigateway/exceptions.py index a3dd86083..5df362a20 100644 --- a/moto/apigateway/exceptions.py +++ b/moto/apigateway/exceptions.py @@ -215,5 +215,5 @@ class MethodNotFoundException(NotFoundException): def __init__(self): super(MethodNotFoundException, self).__init__( - "NotFoundException", "Invalid method properties specified" + "NotFoundException", "Invalid Method identifier specified" ) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 75541664c..cebf6df5a 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -343,6 +343,9 @@ class Resource(CloudFormationModel): raise MethodNotFoundException() return method + def delete_method(self, method_type): + self.resource_methods.pop(method_type) + def add_integration( self, method_type, @@ -1118,6 +1121,10 @@ class APIGatewayBackend(BaseBackend): method = resource.get_method(method_type) return method.apply_operations(patch_operations) + def delete_method(self, function_id, resource_id, method_type): + resource = self.get_resource(function_id, resource_id) + resource.delete_method(method_type) + def get_authorizer(self, restapi_id, authorizer_id): api = self.get_rest_api(restapi_id) authorizer = api.authorizers.get(authorizer_id) diff --git a/moto/apigateway/responses.py b/moto/apigateway/responses.py index e91a10c4f..ad24706e2 100644 --- a/moto/apigateway/responses.py +++ b/moto/apigateway/responses.py @@ -180,8 +180,11 @@ class APIGatewayResponse(BaseResponse): method_type = url_path_parts[6] if self.method == "GET": - method = self.backend.get_method(function_id, resource_id, method_type) - return 200, {}, json.dumps(method) + try: + method = self.backend.get_method(function_id, resource_id, method_type) + return 200, {}, json.dumps(method) + except NotFoundException as nfe: + return self.error("NotFoundException", nfe.message) elif self.method == "PUT": authorization_type = self._get_param("authorizationType") api_key_required = self._get_param("apiKeyRequired") diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 3d7c77a2b..c8e77cd0b 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -463,6 +463,32 @@ def test_create_method_response(): response.should.equal({"ResponseMetadata": {"HTTPStatusCode": 200}}) +@mock_apigateway +def test_delete_method(): + client = boto3.client("apigateway", region_name="us-west-2") + response = client.create_rest_api(name="my_api", description="this is my api") + api_id = response["id"] + + resources = client.get_resources(restApiId=api_id) + root_id = [resource for resource in resources["items"] if resource["path"] == "/"][ + 0 + ]["id"] + + client.put_method( + restApiId=api_id, resourceId=root_id, httpMethod="GET", authorizationType="none" + ) + + client.get_method(restApiId=api_id, resourceId=root_id, httpMethod="GET") + + client.delete_method(restApiId=api_id, resourceId=root_id, httpMethod="GET") + + with pytest.raises(ClientError) as ex: + client.get_method(restApiId=api_id, resourceId=root_id, httpMethod="GET") + err = ex.value.response["Error"] + err["Code"].should.equal("NotFoundException") + err["Message"].should.equal("Invalid Method identifier specified") + + @mock_apigateway def test_integrations(): client = boto3.client("apigateway", region_name="us-west-2")