mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(apigatewayv2): forward HTTP API requests through ALB listener (#941)
* feat(apigatewayv2): forward HTTP API requests through ALB listener * implemented copilot feedbacks * refactor(apigatewayv2): apply maintainer feedback
This commit is contained in:
1 parent
0a8732b349
commit
5f4dd43b5a
10 files changed
+906
-4
No files matched your search
+64
-1
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.websocket.WebSocketConnectionManager;
|
||||
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -13,6 +14,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -25,6 +27,7 @@ class ApiGatewayProxyMatchTest {
|
||||
@Mock VtlTemplateEngine vtlEngine;
|
||||
@Mock AwsServiceRouter serviceRouter;
|
||||
@Mock WebSocketConnectionManager webSocketConnectionManager;
|
||||
@Mock ElbV2Service elbV2Service;
|
||||
|
||||
private ApiGatewayExecuteController ctrl;
|
||||
|
||||
@@ -32,7 +35,7 @@ class ApiGatewayProxyMatchTest {
|
||||
void setUp() {
|
||||
ctrl = new ApiGatewayExecuteController(apiGatewayService, apiGatewayV2Service, lambdaService,
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager);
|
||||
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service);
|
||||
}
|
||||
|
||||
private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
|
||||
@@ -124,4 +127,64 @@ class ApiGatewayProxyMatchTest {
|
||||
assertNull(ctrl.matchResource(List.of(authProxy), "/"));
|
||||
assertNull(ctrl.matchResource(List.of(authProxy), "/other/path"));
|
||||
}
|
||||
|
||||
// ──────────────────────────── ELB_LISTENER_ARN ────────────────────────────
|
||||
|
||||
@Test
|
||||
void elbListenerArnMatchesAlbAndExtractsRegion() {
|
||||
Matcher m = ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:000000000000:listener/app/my-lb/abcdef0123456789/0011223344556677");
|
||||
assertTrue(m.matches());
|
||||
assertEquals("us-east-1", m.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void elbListenerArnMatchesNlb() {
|
||||
Matcher m = ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-west-2:000000000000:listener/net/my-nlb/abcdef0123456789/0011223344556677");
|
||||
assertTrue(m.matches());
|
||||
assertEquals("us-west-2", m.group(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void elbListenerArnMatchesGovAndChinaPartitions() {
|
||||
assertTrue(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws-us-gov:elasticloadbalancing:us-gov-west-1:0:listener/app/lb/a/b").matches());
|
||||
assertTrue(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws-cn:elasticloadbalancing:cn-north-1:0:listener/net/lb/a/b").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
void elbListenerArnRejectsNonListenerElbArns() {
|
||||
// LoadBalancer ARN, not a listener
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:0:loadbalancer/app/my-lb/abc").matches());
|
||||
// Target group ARN
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:0:targetgroup/my-tg/abc").matches());
|
||||
// Listener rule ARN
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:0:listener-rule/app/lb/a/b/c").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
void elbListenerArnRejectsUnsupportedListenerSubtypes() {
|
||||
// gateway / unknown lb subtypes — not yet supported by HttpAlbIntegration in CDK
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:0:listener/gwy/lb/a/b").matches());
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:elasticloadbalancing:us-east-1:0:listener/unknown/lb/a/b").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
void elbListenerArnRejectsOtherServiceArns() {
|
||||
// Lambda function ARN — what AWS_PROXY integrations use; must NOT trigger ELB resolution
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"arn:aws:lambda:us-east-1:0:function:my-fn").matches());
|
||||
// Plain http URL — what regular HTTP_PROXY integrations use
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher(
|
||||
"http://example.com/path").matches());
|
||||
// Empty / blank
|
||||
assertFalse(ApiGatewayExecuteController.ELB_LISTENER_ARN.matcher("").matches());
|
||||
}
|
||||
}
|
||||
+353
@@ -0,0 +1,353 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/** End-to-end: HTTP API → ALB listener ARN → ELBv2 data plane → registered stub target. */
|
||||
@QuarkusTest
|
||||
@TestProfile(ApiGatewayV2AlbIntegrationTest.RealElbV2Profile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayV2AlbIntegrationTest {
|
||||
|
||||
/** Overrides the default {@code elbv2.mock=true} so the listener actually binds. */
|
||||
public static class RealElbV2Profile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.elbv2.mock", "false");
|
||||
}
|
||||
}
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260520/us-east-1/elasticloadbalancing/aws4_request";
|
||||
|
||||
/** Fixed listener port. Distinct from {@code ElbV2LambdaTargetIntegrationTest} (7780). */
|
||||
private static final int LISTENER_PORT = 7792;
|
||||
|
||||
private static HttpServer stub;
|
||||
private static int stubPort;
|
||||
|
||||
private static String lbArn;
|
||||
private static String tgArn;
|
||||
private static String listenerArn;
|
||||
private static String vpcLinkId;
|
||||
private static String apiId;
|
||||
private static String integrationId;
|
||||
|
||||
@BeforeAll
|
||||
static void startStubBackend() throws IOException {
|
||||
// Ephemeral-port HTTP stub that echoes the requested path. Acts as the
|
||||
// "ECS container" sitting behind the ALB.
|
||||
stub = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
stub.createContext("/", exchange -> {
|
||||
String reqPath = exchange.getRequestURI().getPath();
|
||||
byte[] body = ("{\"status\":\"ok\",\"path\":\"" + reqPath + "\"}").getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(body);
|
||||
}
|
||||
});
|
||||
stub.start();
|
||||
stubPort = stub.getAddress().getPort();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopStubBackend() {
|
||||
if (stub != null) {
|
||||
stub.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createLoadBalancer() {
|
||||
lbArn = given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "apigw-alb-test")
|
||||
.formParam("Type", "application")
|
||||
.formParam("Scheme", "internet-facing")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createTargetGroup() {
|
||||
tgArn = given()
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
.formParam("Name", "apigw-alb-tg")
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", String.valueOf(stubPort))
|
||||
.formParam("TargetType", "ip")
|
||||
.formParam("HealthCheckPath", "/")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreateTargetGroupResponse.CreateTargetGroupResult.TargetGroups.member.TargetGroupArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void registerStubAsTarget() {
|
||||
given()
|
||||
.formParam("Action", "RegisterTargets")
|
||||
.formParam("TargetGroupArn", tgArn)
|
||||
.formParam("Targets.member.1.Id", "127.0.0.1")
|
||||
.formParam("Targets.member.1.Port", String.valueOf(stubPort))
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void createListenerOnFixedPort() {
|
||||
listenerArn = given()
|
||||
.formParam("Action", "CreateListener")
|
||||
.formParam("LoadBalancerArn", lbArn)
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", String.valueOf(LISTENER_PORT))
|
||||
.formParam("DefaultActions.member.1.Type", "forward")
|
||||
.formParam("DefaultActions.member.1.TargetGroupArn", tgArn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreateListenerResponse.CreateListenerResult.Listeners.member.ListenerArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void createVpcLink() {
|
||||
vpcLinkId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"alb-vpc-link","subnetIds":["subnet-1"],"securityGroupIds":["sg-1"]}
|
||||
""")
|
||||
.when().post("/v2/vpclinks")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("vpcLinkId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void createHttpApi() {
|
||||
apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"alb-test-api","protocolType":"HTTP"}
|
||||
""")
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void createAlbIntegration() {
|
||||
integrationId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationType":"HTTP_PROXY",
|
||||
"integrationUri":"%s",
|
||||
"connectionType":"VPC_LINK",
|
||||
"connectionId":"%s",
|
||||
"payloadFormatVersion":"1.0",
|
||||
"integrationMethod":"ANY"}
|
||||
""".formatted(listenerArn, vpcLinkId))
|
||||
.when().post("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("integrationUri", equalTo(listenerArn))
|
||||
.body("connectionType", equalTo("VPC_LINK"))
|
||||
.extract().path("integrationId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void createHealthRoute() {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"routeKey":"GET /health","target":"integrations/%s"}
|
||||
""".formatted(integrationId))
|
||||
.when().post("/v2/apis/" + apiId + "/routes")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void httpProxyToStubDirectlyWorksThroughDispatch() {
|
||||
// Baseline: bypass the ALB chain and prove the v2 HTTP_PROXY dispatch path
|
||||
// works when integrationUri is a plain http://127.0.0.1 URL. If this hangs,
|
||||
// it's a pre-existing pipeline issue independent of the ALB resolver.
|
||||
// PATCH the integration to bypass the listener, GET execute-api, then PATCH it back.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationUri":"http://127.0.0.1:%d/probe"}
|
||||
""".formatted(stubPort))
|
||||
.when().patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when().get("/execute-api/" + apiId + "/$default/health")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("status", equalTo("ok"));
|
||||
|
||||
// Restore the listener-ARN integrationUri for the ALB tests that follow.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationUri":"%s"}
|
||||
""".formatted(listenerArn))
|
||||
.when().patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void listenerForwardsDirectlyToStub() throws Exception {
|
||||
// Verifies the ALB data plane independently of the API Gateway path: hit the
|
||||
// listener directly. Polls because server.listen() is async — if the listener
|
||||
// never accepts a connection we get a fast ConnectException and retry.
|
||||
HttpClient client = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(2))
|
||||
.build();
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://127.0.0.1:" + LISTENER_PORT + "/health"))
|
||||
.timeout(Duration.ofSeconds(3))
|
||||
.GET()
|
||||
.build();
|
||||
long deadline = System.currentTimeMillis() + 15_000;
|
||||
Exception lastErr = null;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
try {
|
||||
HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
|
||||
if (resp.statusCode() == 200 && resp.body().contains("\"status\":\"ok\"")) {
|
||||
return;
|
||||
}
|
||||
lastErr = new IllegalStateException("status=" + resp.statusCode() + " body=" + resp.body());
|
||||
} catch (Exception e) {
|
||||
lastErr = e;
|
||||
}
|
||||
Thread.sleep(250);
|
||||
}
|
||||
Assertions.fail("listener direct probe never succeeded: " + lastErr);
|
||||
}
|
||||
|
||||
/** Full HttpAlbIntegration happy path: execute-api → ALB resolver → listener → stub returns 200. */
|
||||
@Test
|
||||
@Order(11)
|
||||
void executeApiReachesStubViaAlb() {
|
||||
// Now go through the actual HttpAlbIntegration path: dispatchHttpProxyV2 must
|
||||
// detect the listener ARN, resolve it to the bound port, and forward to it.
|
||||
Response last = null;
|
||||
long deadline = System.currentTimeMillis() + 15_000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
last = given().when().get("/execute-api/" + apiId + "/$default/health");
|
||||
if (last.statusCode() == 200) {
|
||||
last.then()
|
||||
.body("status", equalTo("ok"))
|
||||
.body("path", equalTo("/health"));
|
||||
return;
|
||||
}
|
||||
try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; }
|
||||
}
|
||||
Assertions.fail("execute-api → ALB → stub never returned 200; last status="
|
||||
+ (last != null ? last.statusCode() : "n/a")
|
||||
+ " body=" + (last != null ? last.body().asString() : "n/a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void unresolvableListenerArnReturns502() {
|
||||
// Replace the integrationUri with a listener ARN that doesn't exist.
|
||||
// The ALB resolver must return a clean 502, not crash.
|
||||
String ghostArn = "arn:aws:elasticloadbalancing:us-east-1:000000000000:listener/app/ghost-lb/0000000000000000/0000000000000000";
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationUri":"%s"}
|
||||
""".formatted(ghostArn))
|
||||
.when().patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when().get("/execute-api/" + apiId + "/$default/health")
|
||||
.then()
|
||||
.statusCode(502);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(90)
|
||||
void cleanup() {
|
||||
if (integrationId != null) {
|
||||
given().when().delete("/v2/apis/" + apiId + "/integrations/" + integrationId);
|
||||
}
|
||||
if (apiId != null) {
|
||||
given().when().delete("/v2/apis/" + apiId);
|
||||
}
|
||||
if (vpcLinkId != null) {
|
||||
given().when().delete("/v2/vpclinks/" + vpcLinkId);
|
||||
}
|
||||
if (listenerArn != null) {
|
||||
given()
|
||||
.formParam("Action", "DeleteListener")
|
||||
.formParam("ListenerArn", listenerArn)
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/");
|
||||
}
|
||||
if (lbArn != null) {
|
||||
given()
|
||||
.formParam("Action", "DeleteLoadBalancer")
|
||||
.formParam("LoadBalancerArn", lbArn)
|
||||
.header("Authorization", AUTH)
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/** REST tests for {@code /v2/vpclinks} CRUD + Integration {@code connectionId} round-trip. */
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayV2VpcLinkRestTest {
|
||||
|
||||
private static String vpcLinkId;
|
||||
private static String apiId;
|
||||
private static String integrationId;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
// Register encoder for application/x-amz-json-1.1 so the JSON 1.1 round-trip
|
||||
// below can post a raw body string.
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createVpcLink() {
|
||||
vpcLinkId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"my-vpc-link",
|
||||
"subnetIds":["subnet-aaaa1111","subnet-bbbb2222"],
|
||||
"securityGroupIds":["sg-cccc3333"],
|
||||
"tags":{"Env":"test"}}
|
||||
""")
|
||||
.when().post("/v2/vpclinks")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("vpcLinkId", notNullValue())
|
||||
.body("name", equalTo("my-vpc-link"))
|
||||
.body("vpcLinkStatus", equalTo("AVAILABLE"))
|
||||
.body("vpcLinkVersion", equalTo("V2"))
|
||||
.body("subnetIds", hasItems("subnet-aaaa1111", "subnet-bbbb2222"))
|
||||
.body("securityGroupIds", hasItem("sg-cccc3333"))
|
||||
.body("tags.Env", equalTo("test"))
|
||||
.extract().path("vpcLinkId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void getVpcLink() {
|
||||
given()
|
||||
.when().get("/v2/vpclinks/" + vpcLinkId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("vpcLinkId", equalTo(vpcLinkId))
|
||||
.body("name", equalTo("my-vpc-link"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void listVpcLinksIncludesCreated() {
|
||||
given()
|
||||
.when().get("/v2/vpclinks")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items.vpcLinkId", hasItem(vpcLinkId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getNonexistentVpcLinkReturns404() {
|
||||
given()
|
||||
.when().get("/v2/vpclinks/does-not-exist")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void createApi() {
|
||||
apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"vpc-link-api","protocolType":"HTTP"}
|
||||
""")
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void createIntegrationWithVpcLinkEchoesConnectionFields() {
|
||||
// VPC_LINK + connectionId pointing at our VpcLink + listener-ARN integrationUri:
|
||||
// CreateIntegration must persist + echo connectionType, connectionId, integrationUri.
|
||||
integrationId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationType":"HTTP_PROXY",
|
||||
"integrationUri":"arn:aws:elasticloadbalancing:us-east-1:000000000000:listener/app/my-lb/abcdef0123456789/0011223344556677",
|
||||
"connectionType":"VPC_LINK",
|
||||
"connectionId":"%s",
|
||||
"payloadFormatVersion":"1.0",
|
||||
"integrationMethod":"ANY"}
|
||||
""".formatted(vpcLinkId))
|
||||
.when().post("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("integrationId", notNullValue())
|
||||
.body("integrationType", equalTo("HTTP_PROXY"))
|
||||
.body("connectionType", equalTo("VPC_LINK"))
|
||||
.body("connectionId", equalTo(vpcLinkId))
|
||||
.body("integrationUri", containsString("listener/app/my-lb"))
|
||||
.extract().path("integrationId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void getIntegrationStillEchoesConnectionId() {
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("connectionType", equalTo("VPC_LINK"))
|
||||
.body("connectionId", equalTo(vpcLinkId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void updateIntegrationPreservesConnectionId() {
|
||||
// PATCH unrelated field; connectionType + connectionId must survive merge-patch.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"integrationMethod":"GET"}
|
||||
""")
|
||||
.when().patch("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("integrationMethod", equalTo("GET"))
|
||||
.body("connectionType", equalTo("VPC_LINK"))
|
||||
.body("connectionId", equalTo(vpcLinkId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void connectionIdRoundTripsViaJson11() {
|
||||
// Mirrors IntegrationConnectionTypeAndAuthorizerSimpleResponsesTest#connectionTypeReturnedViaJson11
|
||||
// to make sure the JSON 1.1 (X-Amz-Target) response path also emits ConnectionId.
|
||||
String amzJson = "application/x-amz-json-1.1";
|
||||
String authHeader = "AWS4-HMAC-SHA256 Credential=test/20260520/us-east-1/apigateway/aws4_request";
|
||||
String targetPrefix = "AmazonApiGatewayV2.";
|
||||
|
||||
String localApiId = given()
|
||||
.contentType(amzJson)
|
||||
.header("X-Amz-Target", targetPrefix + "CreateApi")
|
||||
.header("Authorization", authHeader)
|
||||
.body("""
|
||||
{"Name":"conn-id-json11","ProtocolType":"HTTP"}
|
||||
""")
|
||||
.when().post("/")
|
||||
.then().statusCode(201)
|
||||
.extract().path("ApiId");
|
||||
|
||||
String localIntegrationId = given()
|
||||
.contentType(amzJson)
|
||||
.header("X-Amz-Target", targetPrefix + "CreateIntegration")
|
||||
.header("Authorization", authHeader)
|
||||
.body("""
|
||||
{"ApiId":"%s","IntegrationType":"HTTP_PROXY","IntegrationUri":"https://example.com","IntegrationMethod":"GET","ConnectionType":"VPC_LINK","ConnectionId":"%s","PayloadFormatVersion":"1.0"}
|
||||
""".formatted(localApiId, vpcLinkId))
|
||||
.when().post("/")
|
||||
.then().statusCode(201)
|
||||
.body("ConnectionType", equalTo("VPC_LINK"))
|
||||
.body("ConnectionId", equalTo(vpcLinkId))
|
||||
.extract().path("IntegrationId");
|
||||
|
||||
given()
|
||||
.contentType(amzJson)
|
||||
.header("X-Amz-Target", targetPrefix + "GetIntegration")
|
||||
.header("Authorization", authHeader)
|
||||
.body("""
|
||||
{"ApiId":"%s","IntegrationId":"%s"}
|
||||
""".formatted(localApiId, localIntegrationId))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("ConnectionId", equalTo(vpcLinkId));
|
||||
|
||||
// cleanup the locally-scoped api so it doesn't leak into the next test
|
||||
given().when().delete("/v2/apis/" + localApiId).then().statusCode(anyOf(equalTo(204), equalTo(404)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void deleteVpcLink() {
|
||||
given()
|
||||
.when().delete("/v2/vpclinks/" + vpcLinkId)
|
||||
.then()
|
||||
.statusCode(202);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void getDeletedVpcLinkReturns404() {
|
||||
given()
|
||||
.when().get("/v2/vpclinks/" + vpcLinkId)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(99)
|
||||
void cleanup() {
|
||||
if (integrationId != null) {
|
||||
given().when().delete("/v2/apis/" + apiId + "/integrations/" + integrationId)
|
||||
.then().statusCode(anyOf(equalTo(204), equalTo(404)));
|
||||
}
|
||||
if (apiId != null) {
|
||||
given().when().delete("/v2/apis/" + apiId)
|
||||
.then().statusCode(anyOf(equalTo(204), equalTo(404)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user