mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching (#811)
* fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching
When multiple sibling {proxy+} resources exist (e.g. /auth/{proxy+}
and /products/{proxy+}), the first one registered always matched
every request regardless of the parent path. Now the proxy+ with
the longest parent prefix matching the request path is selected,
matching real AWS behaviour.
Closes #792
* test(api-gateway): add unit tests for proxy+ path matching
Add 7 focused tests for the matchResource method covering root proxy+,
sibling proxy+ routing, fallback behaviour, exact/template priority,
longest parent prefix, and no-match cases.
* fix(api-gateway): reject empty proxy+ segments and use cleaner path extraction
Adopt reviewer's suggestion: use substring before {proxy+} for parent
prefix extraction instead of lastIndexOf('/'), and require at least one
path segment after the prefix for non-root proxy+ resources. Root
/{proxy+} still matches / as a special case.
Adds tests for empty proxy segment edge case (/auth/ should not match
/auth/{proxy+}) and updates longestParentPrefixWins assertion for /api/
(should be null, not match).
This commit is contained in:
1 parent
e89777b8a2
commit
4b6b99c1e5
2 files changed
+149
-5
No files matched your search
+127
@@ -0,0 +1,127 @@
|
||||
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.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ApiGatewayProxyMatchTest {
|
||||
|
||||
@Mock ApiGatewayService apiGatewayService;
|
||||
@Mock ApiGatewayV2Service apiGatewayV2Service;
|
||||
@Mock LambdaService lambdaService;
|
||||
@Mock VtlTemplateEngine vtlEngine;
|
||||
@Mock AwsServiceRouter serviceRouter;
|
||||
@Mock WebSocketConnectionManager webSocketConnectionManager;
|
||||
|
||||
private ApiGatewayExecuteController ctrl;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ctrl = new ApiGatewayExecuteController(apiGatewayService, apiGatewayV2Service, lambdaService,
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager);
|
||||
}
|
||||
|
||||
private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
|
||||
ApiGatewayResource r = new ApiGatewayResource();
|
||||
r.setId(id);
|
||||
r.setParentId(parentId);
|
||||
r.setPathPart(pathPart);
|
||||
r.setPath(path);
|
||||
return r;
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootProxyMatchesEverything() {
|
||||
ApiGatewayResource rootProxy = resource("r1", "root", "{proxy+}", "/{proxy+}");
|
||||
|
||||
assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/anything"));
|
||||
assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/"));
|
||||
assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/a/b/c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void siblingProxyRoutesToCorrectParent() {
|
||||
ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
|
||||
ApiGatewayResource productsProxy = resource("r2", "root", "{proxy+}", "/products/{proxy+}");
|
||||
List<ApiGatewayResource> resources = List.of(authProxy, productsProxy);
|
||||
|
||||
assertSame(authProxy, ctrl.matchResource(resources, "/auth/login"));
|
||||
assertSame(productsProxy, ctrl.matchResource(resources, "/products/123"));
|
||||
assertSame(productsProxy, ctrl.matchResource(resources, "/products/abc/def"));
|
||||
assertNull(ctrl.matchResource(resources, "/other"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyProxySegmentDoesNotMatchNonRootProxy() {
|
||||
ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
|
||||
ApiGatewayResource productsProxy = resource("r2", "root", "{proxy+}", "/products/{proxy+}");
|
||||
|
||||
assertNull(ctrl.matchResource(List.of(authProxy), "/auth/"));
|
||||
assertNull(ctrl.matchResource(List.of(authProxy, productsProxy), "/auth/"));
|
||||
assertNull(ctrl.matchResource(List.of(authProxy, productsProxy), "/products/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootProxyFallbackWhenNoSpecificMatch() {
|
||||
ApiGatewayResource rootProxy = resource("r0", "root", "{proxy+}", "/{proxy+}");
|
||||
ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
|
||||
List<ApiGatewayResource> resources = List.of(rootProxy, authProxy);
|
||||
|
||||
assertSame(authProxy, ctrl.matchResource(resources, "/auth/login"));
|
||||
assertSame(rootProxy, ctrl.matchResource(resources, "/other"));
|
||||
assertSame(rootProxy, ctrl.matchResource(resources, "/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactMatchTakesPriorityOverProxy() {
|
||||
ApiGatewayResource exact = resource("r1", "root", "login", "/auth/login");
|
||||
ApiGatewayResource authProxy = resource("r2", "root", "{proxy+}", "/auth/{proxy+}");
|
||||
List<ApiGatewayResource> resources = List.of(authProxy, exact);
|
||||
|
||||
assertSame(exact, ctrl.matchResource(resources, "/auth/login"));
|
||||
assertSame(authProxy, ctrl.matchResource(resources, "/auth/register"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void templatePathTakesPriorityOverProxy() {
|
||||
ApiGatewayResource itemDetail = resource("r1", "root", "{id}", "/items/{id}");
|
||||
ApiGatewayResource itemsProxy = resource("r2", "root", "{proxy+}", "/items/{proxy+}");
|
||||
List<ApiGatewayResource> resources = List.of(itemsProxy, itemDetail);
|
||||
|
||||
assertSame(itemDetail, ctrl.matchResource(resources, "/items/123"));
|
||||
assertSame(itemsProxy, ctrl.matchResource(resources, "/items/123/sub"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void longestParentPrefixWins() {
|
||||
ApiGatewayResource apiProxy = resource("r1", "root", "{proxy+}", "/api/{proxy+}");
|
||||
ApiGatewayResource apiV1Proxy = resource("r2", "root", "{proxy+}", "/api/v1/{proxy+}");
|
||||
List<ApiGatewayResource> resources = List.of(apiV1Proxy, apiProxy);
|
||||
|
||||
assertSame(apiV1Proxy, ctrl.matchResource(resources, "/api/v1/users"));
|
||||
assertSame(apiProxy, ctrl.matchResource(resources, "/api/v2"));
|
||||
assertNull(ctrl.matchResource(resources, "/api/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noMatchReturnsNull() {
|
||||
ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
|
||||
|
||||
assertNull(ctrl.matchResource(List.of(authProxy), "/"));
|
||||
assertNull(ctrl.matchResource(List.of(authProxy), "/other/path"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user