fix(apigatewayv2): store and apply integration ResponseParameters (#9090)

This commit is contained in:
Ignas authored and GitHub committed 2026-11-20 20:19:51 -04:00
1 parent 7b88a31956
commit d427b31e07
15 files changed
+693 -32

No files matched your search

@@ -693,4 +693,53 @@ class ApiGatewayV2OpenApiImportTest {
assertEquals(1, authorizers.size());
assertEquals(300, authorizers.get(0).get("authorizerResultTtlInSeconds").asInt());
}
@Test
@Order(20)
void importedResponseParametersAreStoredAndAnInvalidOneLeavesThePreviousDefinitionIntact() throws Exception {
String spec = """
{
"openapi": "3.0.1",
"info": {"title": "ResponseParametersImport", "version": "1.0"},
"paths": {
"/orders": {
"get": {
"x-amazon-apigateway-integration": {
"type": "http_proxy",
"httpMethod": "ANY",
"uri": "https://api.example.com/orders",
"payloadFormatVersion": "1.0",
"responseParameters": {
"500": {"overwrite:statuscode": "403", "append:header.header1": "$context.requestId"}
}
}
}
}
}
}
""";
String created = given().contentType(ContentType.JSON)
.body(envelope(spec))
.when().put("/v2/apis")
.then().statusCode(201).extract().asString();
String apiId = mapper.readTree(created).get("apiId").asText();
JsonNode responseParameters = get("/v2/apis/" + apiId + "/integrations")
.get("items").get(0).get("responseParameters");
assertEquals("403", responseParameters.path("500").path("overwrite:statuscode").asText());
assertEquals("$context.requestId", responseParameters.path("500").path("append:header.header1").asText());
given().contentType(ContentType.JSON)
.body(envelope(spec.replace("\"500\":", "\"700\":")))
.when().put("/v2/apis/" + apiId)
.then().statusCode(400);
given().contentType(ContentType.JSON)
.body(envelope(spec.replace("\"responseParameters\": {",
"\"responseParameters\": [\"500\"], \"unused\": {")))
.when().put("/v2/apis/" + apiId)
.then().statusCode(400);
assertEquals("403", get("/v2/apis/" + apiId + "/integrations").get("items").get(0)
.path("responseParameters").path("500").path("overwrite:statuscode").asText());
}
}
@@ -0,0 +1,268 @@
package io.floci.aws.services.apigatewayv2;
import com.sun.net.httpserver.HttpServer;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@QuarkusTest
class HttpProxyResponseParametersIntegrationTest {
private HttpServer backend;
private String apiId;
private String integrationId;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void startBackend() throws Exception {
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
backend.createContext("/", exchange -> {
String query = exchange.getRequestURI().getQuery();
int status = query != null && query.startsWith("status=") ? Integer.parseInt(query.substring(7)) : 200;
exchange.getResponseHeaders().add("X-Backend", "one");
exchange.getResponseHeaders().add("X-Drop", "gone");
exchange.getResponseHeaders().add("Content-Type", "application/json");
String body = "pretty=1".equals(query) ? "{\n \"error\": {\"code\": \"E-7\"}\n}" : "{\"error\":{\"code\":\"E-7\"}}";
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.close();
});
backend.start();
apiId = HttpProxyApiFixtures.createProxyApi("response-parameters", "", "test",
backend.getAddress().getPort(), "/orders");
integrationId = given().get("/v2/apis/" + apiId + "/integrations").then().statusCode(200)
.extract().path("items[0].integrationId");
}
@AfterEach
void cleanUp() {
try {
given().delete("/v2/apis/" + apiId).then().statusCode(204);
} finally {
backend.stop(0);
}
}
@Test
void createReturnsAndGetReadsResponseParameters() {
Map<String, Object> responseParameters = Map.of(
"500", Map.of("overwrite:statuscode", "403", "append:header.header1", "$context.requestId"),
"404", Map.of("append:header.error", "$stageVariables.environmentId"));
String createdId = given().contentType(ContentType.JSON)
.body(Map.of("integrationType", "HTTP_PROXY", "integrationUri", "https://api.example.com",
"integrationMethod", "ANY", "payloadFormatVersion", "1.0",
"responseParameters", responseParameters))
.post("/v2/apis/" + apiId + "/integrations")
.then().statusCode(201)
.body("responseParameters", equalTo(responseParameters))
.extract().path("integrationId");
given().get("/v2/apis/" + apiId + "/integrations/" + createdId)
.then().statusCode(200)
.body("responseParameters", equalTo(responseParameters));
given().get("/v2/apis/" + apiId + "/integrations")
.then().statusCode(200)
.body("items.find { it.integrationId == '" + createdId + "' }.responseParameters",
equalTo(responseParameters));
}
@Test
void updateReplacesAStatusCodesMappingsAndAnEmptyMapRemovesIt() {
patchResponseParameters(Map.of(
"500", Map.of("overwrite:statuscode", "403"),
"502", Map.of("overwrite:statuscode", "200")));
patchResponseParameters(Map.of(
"500", Map.of(),
"502", Map.of("append:header.x-retry", "later"),
"404", Map.of("remove:header.x-drop", "")));
given().get("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(200)
.body("responseParameters", equalTo(Map.of(
"502", Map.of("append:header.x-retry", "later"),
"404", Map.of("remove:header.x-drop", ""))));
patchResponseParameters(Map.of("502", Map.of(), "404", Map.of()));
assertNull(given().get("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(200).extract().path("responseParameters"));
}
@Test
void json11CreateAndGetKeepResponseParameters() {
String target = "AmazonApiGatewayV2.";
String json11 = "application/x-amz-json-1.1";
String auth = "AWS4-HMAC-SHA256 Credential=test/20260413/us-east-1/apigatewayv2/aws4_request";
String createdId = given().contentType(json11).header("X-Amz-Target", target + "CreateIntegration")
.header("Authorization", auth)
.body("""
{"ApiId":"%s","IntegrationType":"HTTP_PROXY","IntegrationUri":"http://example.com",
"PayloadFormatVersion":"1.0","ResponseParameters":{"500":{"overwrite:statuscode":"200"}}}
""".formatted(apiId))
.post("/")
.then().statusCode(201)
.body("ResponseParameters.'500'.'overwrite:statuscode'", equalTo("200"))
.extract().path("IntegrationId");
given().contentType(json11).header("X-Amz-Target", target + "GetIntegration")
.header("Authorization", auth)
.body("{\"ApiId\":\"%s\",\"IntegrationId\":\"%s\"}".formatted(apiId, createdId))
.post("/")
.then().statusCode(200)
.body("ResponseParameters.'500'.'overwrite:statuscode'", equalTo("200"));
}
@Test
void statusCodeOutsideTwoHundredToFiveNinetyNineIsRejected() {
given().contentType(ContentType.JSON)
.body(Map.of("responseParameters", Map.of("600", Map.of("overwrite:statuscode", "200"))))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("The selection key must be a valid HTTP status code within the range of 200-599."));
}
@Test
void unknownMappingIsRejectedAndLeavesTheIntegrationUnchanged() {
patchResponseParameters(Map.of("500", Map.of("overwrite:statuscode", "403")));
given().contentType(ContentType.JSON)
.body(Map.of("responseParameters", Map.of(
"500", Map.of("overwrite:statuscode", "200"),
"404", Map.of("overwrite:querystring.page", "1"))))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", containsString("ResponseParameters key overwrite:querystring.page"));
given().get("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(200)
.body("responseParameters", equalTo(Map.of("500", Map.of("overwrite:statuscode", "403"))));
}
@Test
void staticStatusCodeOverwriteThatIsNotAStatusIsRejected() {
patchResponseParameters(Map.of("500", Map.of("overwrite:statuscode", "$response.header.x-status")));
given().contentType(ContentType.JSON)
.body(Map.of("responseParameters", Map.of("500", Map.of("overwrite:statuscode", "abc"))))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", containsString("overwrite:statuscode value for status code 500"));
given().get("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(200)
.body("responseParameters",
equalTo(Map.of("500", Map.of("overwrite:statuscode", "$response.header.x-status"))));
}
@Test
void headerNameThatIsNotATokenIsRejected() {
given().contentType(ContentType.JSON)
.body(Map.of("responseParameters", Map.of("200", Map.of("append:header.x bad", "v"))))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", containsString("ResponseParameters key append:header.x bad"));
}
@Test
void responseParametersThatIsNotAnObjectIsRejected() {
given().contentType(ContentType.JSON)
.body(Map.of("integrationType", "HTTP_PROXY", "integrationUri", "https://api.example.com",
"payloadFormatVersion", "1.0", "responseParameters", List.of("500")))
.post("/v2/apis/" + apiId + "/integrations")
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("ResponseParameters must be a map."));
given().contentType(ContentType.JSON)
.body(Map.of("responseParameters", "500"))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(400)
.body("message", equalTo("ResponseParameters must be a map."));
given().contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "AmazonApiGatewayV2.UpdateIntegration")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260413/us-east-1/apigatewayv2/aws4_request")
.body("{\"ApiId\":\"%s\",\"IntegrationId\":\"%s\",\"ResponseParameters\":[\"500\"]}"
.formatted(apiId, integrationId))
.post("/")
.then().statusCode(400)
.body("message", equalTo("ResponseParameters must be a map."));
}
@Test
void headerValueWithALineBreakIsSkippedAndTheResponseIsRelayed() {
patchResponseParameters(Map.of("200", Map.of(
"append:header.x-body", "$response.body",
"append:header.x-code", "$response.body.error.code")));
Response response = given().get("/execute-api/" + apiId + "/test/orders?pretty=1");
assertEquals(200, response.statusCode());
assertEquals("E-7", response.jsonPath().getString("error.code"));
assertNull(response.header("x-body"));
assertEquals("E-7", response.header("x-code"));
}
@Test
void statusCodeOverwriteAppliesOnlyToTheMatchingBackendStatus() {
patchResponseParameters(Map.of("500", Map.of("overwrite:statuscode", "200")));
given().get("/execute-api/" + apiId + "/test/orders?status=500")
.then().statusCode(200)
.body("error.code", equalTo("E-7"));
given().get("/execute-api/" + apiId + "/test/orders?status=502")
.then().statusCode(502);
}
@Test
void headerActionsReadTheBackendResponseAndTheRequestContext() {
patchResponseParameters(Map.of("200", Map.of(
"append:header.x-copy", "$response.header.x-backend",
"overwrite:header.X-Backend", "two",
"remove:header.x-drop", "''",
"append:header.x-code", "$response.body.error.code",
"append:header.x-stage", "$context.stage",
"append:header.x-missing", "$response.header.x-absent")));
Response response = given().get("/execute-api/" + apiId + "/test/orders");
assertEquals(200, response.statusCode());
assertEquals("one", response.header("x-copy"));
assertEquals("two", response.header("x-backend"));
assertEquals(1, response.headers().getValues("x-backend").size());
assertNull(response.header("x-drop"));
assertEquals("E-7", response.header("x-code"));
assertEquals("test", response.header("x-stage"));
assertNull(response.header("x-missing"));
}
private void patchResponseParameters(Map<String, Object> responseParameters) {
given().contentType(ContentType.JSON).body(Map.of("responseParameters", responseParameters))
.patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
.then().statusCode(200);
}
}
@@ -941,7 +941,7 @@ class ApiGatewayV2CfnProvisionerTest {
""");
assertTrue(integration.getStatus().endsWith("_COMPLETE"), integration.getStatus());
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null);
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null, null);
}
@Test
@@ -952,10 +952,29 @@ class ApiGatewayV2CfnProvisionerTest {
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", "ANY",
"${request.method}", 15000, Map.of("append:header.x-request-id", "$context.requestId",
"overwrite:header.x-example", "static-value"),
"overwrite:header.x-example", "static-value"), null,
Map.of("application/json", "{\"a\":1}"));
}
@Test
void integrationResponseParametersBecomeAMapOfDestinationToSource() throws Exception {
Integration integration = new Integration();
integration.setIntegrationId("int-1");
when(apiGatewayV2Service.createIntegration(eq(REGION), eq(API_ID), anyMap())).thenReturn(integration);
provisioner.provision("Integration", "AWS::ApiGatewayV2::Integration", mapper.readTree("""
{"ApiId":"api-123","IntegrationType":"HTTP_PROXY","IntegrationUri":"http://backend",
"ResponseParameters":{"500":{"ResponseParameters":[
{"Destination":"overwrite:statuscode","Source":"403"},
{"Destination":"append:header.x-stack","Source":{"Ref":"AWS::StackName"}}]}}}
"""),
engine(), REGION, "000000000000", "test-stack", null, Map.of());
verify(apiGatewayV2Service).createIntegration(eq(REGION), eq(API_ID), argThat(req ->
Map.of("500", Map.of("overwrite:statuscode", "403", "append:header.x-stack", "test-stack"))
.equals(req.get("responseParameters"))));
}
@Test
void integrationMappingEntryWithNoValueIsOmitted() throws Exception {
Integration integration = new Integration();
@@ -980,22 +999,26 @@ class ApiGatewayV2CfnProvisionerTest {
prior.setTemplateSelectionExpression("${request.method}");
prior.setTimeoutInMillis(15000);
prior.setRequestParameters(Map.of("overwrite:header.x-example", "old"));
prior.setResponseParameters(Map.of("500", Map.of("overwrite:statuscode", "200")));
prior.setRequestTemplates(Map.of("application/json", "{}"));
stubIntegrationUpdate(prior);
StackResource integration = provisionIntegration("""
{"ApiId":"api-123","IntegrationType":"HTTP_PROXY","IntegrationUri":"http://backend",
"ConnectionType":"VPC_LINK","ConnectionId":"vl-1","IntegrationMethod":"POST",
"TimeoutInMillis":"3000","RequestParameters":{"overwrite:header.x-example":"new"}}
"TimeoutInMillis":"3000","RequestParameters":{"overwrite:header.x-example":"new"},
"ResponseParameters":{"404":{"ResponseParameters":[
{"Destination":"overwrite:statuscode","Source":"200"}]}}}
""");
assertTrue(provisioner.rollbackUpdate(integration));
InOrder order = inOrder(apiGatewayV2Service);
order.verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", "POST", null, 3000,
Map.of("overwrite:header.x-example", "new"), null);
Map.of("overwrite:header.x-example", "new"), Map.of("404", Map.of("overwrite:statuscode", "200")),
null);
order.verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", "GET",
"${request.method}", 15000, Map.of("overwrite:header.x-example", "old"),
Map.of("application/json", "{}"));
Map.of("500", Map.of("overwrite:statuscode", "200")), Map.of("application/json", "{}"));
}
@Test
@@ -1012,9 +1035,9 @@ class ApiGatewayV2CfnProvisionerTest {
""");
assertTrue(integration.getStatus().endsWith("_FAILED"), integration.getStatus());
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null);
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null, null);
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0,
Map.of("overwrite:header.x-example", "old"), null);
Map.of("overwrite:header.x-example", "old"), null, null);
}
private static final String NO_VALUE_SETTINGS = """
@@ -1049,7 +1072,7 @@ class ApiGatewayV2CfnProvisionerTest {
"test-stack", "int-1", Map.of("IntegrationId", "int-1"));
assertTrue(resource.getStatus().endsWith("_COMPLETE"), resource.getStatus());
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null);
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", null, null, 0, null, null, null);
}
@Test
@@ -1061,7 +1084,7 @@ class ApiGatewayV2CfnProvisionerTest {
"test-stack", "int-1", Map.of("IntegrationId", "int-1"));
verify(apiGatewayV2Service).replaceIntegrationSettings(REGION, API_ID, "int-1", "ANY", null, 15000,
null, null);
null, null, null);
}
@Test