APIGateway: fix rest API status codes (#5397)

This commit is contained in:
Cesar Alvernaz 2022-08-23 23:46:01 +01:00 committed by GitHub
parent cde5537b85
commit 318a3071fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 39 deletions

View File

@ -163,12 +163,14 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
resource = self.backend.get_resource(function_id, resource_id)
return 200, {}, json.dumps(resource.to_dict())
elif self.method == "POST":
path_part = self._get_param("pathPart")
resource = self.backend.create_resource(function_id, resource_id, path_part)
return 201, {}, json.dumps(resource.to_dict())
elif self.method == "DELETE":
resource = self.backend.delete_resource(function_id, resource_id)
return 200, {}, json.dumps(resource.to_dict())
return 202, {}, json.dumps(resource.to_dict())
def resource_methods(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -200,11 +202,11 @@ class APIGatewayResponse(BaseResponse):
authorization_scopes=authorization_scopes,
request_validator_id=request_validator_id,
)
return 200, {}, json.dumps(method)
return 201, {}, json.dumps(method)
elif self.method == "DELETE":
self.backend.delete_method(function_id, resource_id, method_type)
return 200, {}, ""
return 204, {}, ""
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
@ -226,6 +228,7 @@ class APIGatewayResponse(BaseResponse):
method_response = self.backend.get_method_response(
function_id, resource_id, method_type, response_code
)
return 200, {}, json.dumps(method_response)
elif self.method == "PUT":
response_models = self._get_param("responseModels")
response_parameters = self._get_param("responseParameters")
@ -237,18 +240,19 @@ class APIGatewayResponse(BaseResponse):
response_models,
response_parameters,
)
return 201, {}, json.dumps(method_response)
elif self.method == "DELETE":
method_response = self.backend.delete_method_response(
function_id, resource_id, method_type, response_code
)
return 204, {}, json.dumps(method_response)
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
method_response = self.backend.update_method_response(
function_id, resource_id, method_type, response_code, patch_operations
)
else:
return 201, {}, json.dumps(method_response)
raise Exception('Unexpected HTTP method "%s"' % self.method)
return 200, {}, json.dumps(method_response)
def restapis_authorizers(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -295,12 +299,11 @@ class APIGatewayResponse(BaseResponse):
identiy_validation_expression=identiy_validation_expression,
authorizer_result_ttl=authorizer_result_ttl,
)
return 201, {}, json.dumps(authorizer_response)
elif self.method == "GET":
authorizers = self.backend.get_authorizers(restapi_id)
return 200, {}, json.dumps({"item": authorizers})
return 200, {}, json.dumps(authorizer_response)
def request_validators(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
url_path_parts = self.path.split("/")
@ -319,7 +322,7 @@ class APIGatewayResponse(BaseResponse):
validator = self.backend.create_request_validator(
restapi_id, name, body, params
)
return 200, {}, json.dumps(validator)
return 201, {}, json.dumps(validator)
def request_validator_individual(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -348,15 +351,16 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
authorizer_response = self.backend.get_authorizer(restapi_id, authorizer_id)
return 200, {}, json.dumps(authorizer_response)
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
authorizer_response = self.backend.update_authorizer(
restapi_id, authorizer_id, patch_operations
)
return 200, {}, json.dumps(authorizer_response)
elif self.method == "DELETE":
self.backend.delete_authorizer(restapi_id, authorizer_id)
return 202, {}, "{}"
return 200, {}, json.dumps(authorizer_response)
def restapis_stages(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -384,12 +388,11 @@ class APIGatewayResponse(BaseResponse):
tags=tags,
tracing_enabled=tracing_enabled,
)
return 201, {}, json.dumps(stage_response)
elif self.method == "GET":
stages = self.backend.get_stages(function_id)
return 200, {}, json.dumps({"item": stages})
return 200, {}, json.dumps(stage_response)
def restapis_stages_tags(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
url_path_parts = self.path.split("/")
@ -416,16 +419,16 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
stage_response = self.backend.get_stage(function_id, stage_name)
return 200, {}, json.dumps(stage_response)
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
stage_response = self.backend.update_stage(
function_id, stage_name, patch_operations
)
return 200, {}, json.dumps(stage_response)
elif self.method == "DELETE":
self.backend.delete_stage(function_id, stage_name)
return 202, {}, "{}"
return 200, {}, json.dumps(stage_response)
def integrations(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -440,6 +443,7 @@ class APIGatewayResponse(BaseResponse):
integration_response = self.backend.get_integration(
function_id, resource_id, method_type
)
return 200, {}, json.dumps(integration_response)
elif self.method == "PUT":
integration_type = self._get_param("type")
uri = self._get_param("uri")
@ -471,12 +475,12 @@ class APIGatewayResponse(BaseResponse):
timeout_in_millis=timeout_in_millis,
request_parameters=request_parameters,
)
return 201, {}, json.dumps(integration_response)
elif self.method == "DELETE":
integration_response = self.backend.delete_integration(
function_id, resource_id, method_type
)
return 200, {}, json.dumps(integration_response)
return 204, {}, json.dumps(integration_response)
def integration_responses(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -490,6 +494,7 @@ class APIGatewayResponse(BaseResponse):
integration_response = self.backend.get_integration_response(
function_id, resource_id, method_type, status_code
)
return 200, {}, json.dumps(integration_response)
elif self.method == "PUT":
if not self.body:
raise InvalidRequestInput()
@ -506,11 +511,12 @@ class APIGatewayResponse(BaseResponse):
response_templates,
content_handling,
)
return 201, {}, json.dumps(integration_response)
elif self.method == "DELETE":
integration_response = self.backend.delete_integration_response(
function_id, resource_id, method_type, status_code
)
return 200, {}, json.dumps(integration_response)
return 204, {}, json.dumps(integration_response)
def deployments(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -526,7 +532,7 @@ class APIGatewayResponse(BaseResponse):
deployment = self.backend.create_deployment(
function_id, name, description, stage_variables
)
return 200, {}, json.dumps(deployment)
return 201, {}, json.dumps(deployment)
def individual_deployment(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -578,11 +584,11 @@ class APIGatewayResponse(BaseResponse):
self.setup_class(request, full_url, headers)
if self.method == "POST":
usage_plan_response = self.backend.create_usage_plan(json.loads(self.body))
return 201, {}, json.dumps(usage_plan_response)
elif self.method == "GET":
api_key_id = self.querystring.get("keyId", [None])[0]
usage_plans_response = self.backend.get_usage_plans(api_key_id=api_key_id)
return 200, {}, json.dumps({"item": usage_plans_response})
return 200, {}, json.dumps(usage_plan_response)
def usage_plan_individual(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -592,8 +598,10 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
usage_plan_response = self.backend.get_usage_plan(usage_plan)
return 200, {}, json.dumps(usage_plan_response)
elif self.method == "DELETE":
usage_plan_response = self.backend.delete_usage_plan(usage_plan)
return 202, {}, json.dumps(usage_plan_response)
elif self.method == "PATCH":
patch_operations = self._get_param("patchOperations")
usage_plan_response = self.backend.update_usage_plan(
@ -625,11 +633,12 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
usage_plan_response = self.backend.get_usage_plan_key(usage_plan_id, key_id)
return 200, {}, json.dumps(usage_plan_response)
elif self.method == "DELETE":
usage_plan_response = self.backend.delete_usage_plan_key(
usage_plan_id, key_id
)
return 200, {}, json.dumps(usage_plan_response)
return 202, {}, json.dumps(usage_plan_response)
def domain_names(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -665,7 +674,7 @@ class APIGatewayResponse(BaseResponse):
security_policy,
generate_cli_skeleton,
)
return 200, {}, json.dumps(domain_name_resp)
return 201, {}, json.dumps(domain_name_resp)
def domain_name_induvidual(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -677,17 +686,19 @@ class APIGatewayResponse(BaseResponse):
if self.method == "GET":
if domain_name is not None:
domain_names = self.backend.get_domain_name(domain_name)
return 200, {}, json.dumps(domain_names)
elif self.method == "DELETE":
if domain_name is not None:
self.backend.delete_domain_name(domain_name)
return 202, {}, json.dumps({})
elif self.method == "PATCH":
if domain_name is not None:
patch_operations = self._get_param("patchOperations")
self.backend.update_domain_name(domain_name, patch_operations)
return 200, {}, json.dumps(domain_name)
else:
msg = 'Method "%s" for API GW domain names not implemented' % self.method
return 404, {}, json.dumps({"error": msg})
return 200, {}, json.dumps(domain_names)
def models(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -713,8 +724,7 @@ class APIGatewayResponse(BaseResponse):
cli_input_json,
generate_cli_skeleton,
)
return 200, {}, json.dumps(model)
return 201, {}, json.dumps(model)
def model_induvidual(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -775,7 +785,7 @@ class APIGatewayResponse(BaseResponse):
if self.method == "DELETE":
self.backend.delete_vpc_link(vpc_link_id=vpc_link_id)
return 200, {}, "{}"
return 202, {}, "{}"
if self.method == "GET":
vpc_link = self.backend.get_vpc_link(vpc_link_id=vpc_link_id)
return 200, {}, json.dumps(vpc_link)
@ -794,7 +804,7 @@ class APIGatewayResponse(BaseResponse):
vpc_link = self.backend.create_vpc_link(
name=name, description=description, target_arns=target_arns, tags=tags
)
return 200, {}, json.dumps(vpc_link)
return 202, {}, json.dumps(vpc_link)
def put_gateway_response(self):
rest_api_id = self.path.split("/")[-3]
@ -810,7 +820,7 @@ class APIGatewayResponse(BaseResponse):
response_parameters=response_parameters,
response_templates=response_templates,
)
return 200, {}, json.dumps(response)
return 201, {}, json.dumps(response)
def get_gateway_response(self):
rest_api_id = self.path.split("/")[-3]

View File

@ -432,7 +432,7 @@ def test_create_method_response():
response["ResponseMetadata"].pop("HTTPHeaders", None)
response["ResponseMetadata"].pop("RetryAttempts", None)
response.should.equal(
{"ResponseMetadata": {"HTTPStatusCode": 200}, "statusCode": "200"}
{"ResponseMetadata": {"HTTPStatusCode": 201}, "statusCode": "200"}
)
response = client.get_method_response(
@ -451,7 +451,7 @@ def test_create_method_response():
# this is hard to match against, so remove it
response["ResponseMetadata"].pop("HTTPHeaders", None)
response["ResponseMetadata"].pop("RetryAttempts", None)
response.should.equal({"ResponseMetadata": {"HTTPStatusCode": 200}})
response.should.equal({"ResponseMetadata": {"HTTPStatusCode": 204}})
@mock_apigateway
@ -527,7 +527,7 @@ def test_integrations():
response["ResponseMetadata"].pop("RetryAttempts", None)
response.should.equal(
{
"ResponseMetadata": {"HTTPStatusCode": 200},
"ResponseMetadata": {"HTTPStatusCode": 201},
"httpMethod": "POST",
"type": "HTTP",
"uri": "http://httpbin.org/robots.txt",
@ -657,7 +657,7 @@ def test_integration_response():
{
"statusCode": "200",
"selectionPattern": "foobar",
"ResponseMetadata": {"HTTPStatusCode": 200},
"ResponseMetadata": {"HTTPStatusCode": 201},
"responseTemplates": {}, # Note: TF compatibility
}
)
@ -733,7 +733,7 @@ def test_integration_response():
{
"statusCode": "200",
"selectionPattern": "foobar",
"ResponseMetadata": {"HTTPStatusCode": 200},
"ResponseMetadata": {"HTTPStatusCode": 201},
"responseTemplates": {}, # Note: TF compatibility
"contentHandling": "CONVERT_TO_BINARY",
}
@ -927,7 +927,7 @@ def test_create_authorizer():
"providerARNs": [user_pool_arn],
"identitySource": "method.request.header.Authorization",
"authorizerResultTtlInSeconds": 300,
"ResponseMetadata": {"HTTPStatusCode": 200},
"ResponseMetadata": {"HTTPStatusCode": 201},
}
)

View File

@ -30,7 +30,7 @@ def test_create_stage_minimal():
response.should.have.key("variables").equals({})
response.should.have.key("ResponseMetadata").should.have.key(
"HTTPStatusCode"
).equals(200)
).equals(201)
response.should.have.key("description").equals("")
response.shouldnt.have.key("cacheClusterStatus")
response.should.have.key("cacheClusterEnabled").equals(False)
@ -65,7 +65,7 @@ def test_create_stage_with_env_vars():
response.should.have.key("variables").equals({"env": "dev"})
response.should.have.key("ResponseMetadata").should.have.key(
"HTTPStatusCode"
).equals(200)
).equals(201)
response.should.have.key("description").equals("")
response.shouldnt.have.key("cacheClusterStatus")
response.should.have.key("cacheClusterEnabled").equals(False)
@ -103,7 +103,7 @@ def test_create_stage_with_vars_and_cache():
response.should.have.key("variables").equals({"env": "dev"})
response.should.have.key("ResponseMetadata").should.have.key(
"HTTPStatusCode"
).equals(200)
).equals(201)
response.should.have.key("description").equals("hello moto")
response.should.have.key("cacheClusterStatus").equals("AVAILABLE")
response.should.have.key("cacheClusterEnabled").equals(True)
@ -143,7 +143,7 @@ def test_create_stage_with_cache_settings():
response.should.have.key("variables").equals({"env": "dev"})
response.should.have.key("ResponseMetadata").should.have.key(
"HTTPStatusCode"
).equals(200)
).equals(201)
response.should.have.key("description").equals("hello moto")
response.should.have.key("cacheClusterStatus").equals("AVAILABLE")
response.should.have.key("cacheClusterEnabled").equals(True)