mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(apigatewayv2): select the most specific HTTP API route (#6999)
This commit is contained in:
1 parent
57c4a20dde
commit
4aaa5cc466
2 files changed
+143
-3
No files matched your search
+44
-3
@@ -631,7 +631,7 @@ public class ApiGatewayV2Service {
|
||||
|
||||
/**
|
||||
* Finds the best matching route for the given HTTP method and path.
|
||||
* Priority: exact match > path-template match > $default.
|
||||
* Priority: exact method and path > non-greedy path > greedy path > $default.
|
||||
*/
|
||||
public Route findMatchingRoute(String region, String apiId, String httpMethod, String path) {
|
||||
List<Route> routes = getRoutes(region, apiId);
|
||||
@@ -642,10 +642,18 @@ public class ApiGatewayV2Service {
|
||||
if (candidate.equals(r.getRouteKey())) return r;
|
||||
}
|
||||
|
||||
// 2. Path-template match (e.g. "GET /users/{id}")
|
||||
// 2. Most-specific path-template match
|
||||
Route bestMatch = null;
|
||||
for (Route r : routes) {
|
||||
if (r.getRouteKey() == null || r.getRouteKey().equals("$default")) continue;
|
||||
if (routeKeyMatchesPath(r.getRouteKey(), httpMethod, path)) return r;
|
||||
if (routeKeyMatchesPath(r.getRouteKey(), httpMethod, path)
|
||||
&& (bestMatch == null || compareRouteSpecificity(r.getRouteKey(),
|
||||
bestMatch.getRouteKey()) > 0)) {
|
||||
bestMatch = r;
|
||||
}
|
||||
}
|
||||
if (bestMatch != null) {
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
// 3. $default catch-all
|
||||
@@ -656,6 +664,39 @@ public class ApiGatewayV2Service {
|
||||
return null;
|
||||
}
|
||||
|
||||
private int compareRouteSpecificity(String firstKey, String secondKey) {
|
||||
boolean firstGreedy = firstKey.contains("+}");
|
||||
boolean secondGreedy = secondKey.contains("+}");
|
||||
if (firstGreedy != secondGreedy) {
|
||||
return firstGreedy ? -1 : 1;
|
||||
}
|
||||
|
||||
String[] firstSegments = firstKey.substring(firstKey.indexOf(' ') + 1).split("/", -1);
|
||||
String[] secondSegments = secondKey.substring(secondKey.indexOf(' ') + 1).split("/", -1);
|
||||
for (int i = 0; i < Math.min(firstSegments.length, secondSegments.length); i++) {
|
||||
int comparison = Integer.compare(segmentSpecificity(firstSegments[i]),
|
||||
segmentSpecificity(secondSegments[i]));
|
||||
if (comparison != 0) {
|
||||
return comparison;
|
||||
}
|
||||
}
|
||||
int lengthComparison = Integer.compare(firstSegments.length, secondSegments.length);
|
||||
if (lengthComparison != 0) {
|
||||
return lengthComparison;
|
||||
}
|
||||
return Boolean.compare(!firstKey.startsWith("ANY "), !secondKey.startsWith("ANY "));
|
||||
}
|
||||
|
||||
private int segmentSpecificity(String segment) {
|
||||
if (segment.startsWith("{") && segment.endsWith("+}")) {
|
||||
return 0;
|
||||
}
|
||||
if (segment.startsWith("{") && segment.endsWith("}")) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a route by its exact routeKey (e.g. "$connect", "$disconnect", "$default").
|
||||
* Returns null if no route with the given key exists on the API.
|
||||
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2;
|
||||
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Route;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class ApiGatewayV2RouteSelectionServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String API_ID = "api-id";
|
||||
|
||||
@Test
|
||||
void narrowerGreedyRouteWinsRegardlessOfStorageOrder() {
|
||||
Route broad = route("GET /api/{proxy+}", "JWT");
|
||||
Route narrow = route("GET /api/v1/customerid/{proxy+}", "NONE");
|
||||
|
||||
assertSame(narrow, select(List.of(broad, narrow), "GET", "/api/v1/customerid/items"));
|
||||
assertSame(narrow, select(List.of(narrow, broad), "GET", "/api/v1/customerid/items"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parameterRouteWinsOverGreedyRoute() {
|
||||
Route greedy = route("GET /pets/{proxy+}", "JWT");
|
||||
Route parameter = route("GET /pets/dog/{id}", "NONE");
|
||||
|
||||
assertSame(parameter, select(List.of(greedy, parameter), "GET", "/pets/dog/42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void literalPathWinsOverParameterRoute() {
|
||||
Route parameter = route("GET /pets/{id}", "JWT");
|
||||
Route literal = route("GET /pets/dog", "NONE");
|
||||
|
||||
assertSame(literal, select(List.of(parameter, literal), "GET", "/pets/dog"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void methodSpecificRouteWinsForSameGreedyPath() {
|
||||
Route any = route("ANY /open/{proxy+}", "JWT");
|
||||
Route get = route("GET /open/{proxy+}", "NONE");
|
||||
|
||||
assertSame(get, select(List.of(any, get), "GET", "/open/items"));
|
||||
assertSame(any, select(List.of(any, get), "POST", "/open/items"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void petsGreedyRouteWinsOverCatchAllAnyRoute() {
|
||||
Route catchAll = route("ANY /{proxy+}", "JWT");
|
||||
Route pets = route("GET /pets/{proxy+}", "NONE");
|
||||
|
||||
assertSame(pets, select(List.of(catchAll, pets), "GET", "/pets/cat/1"));
|
||||
assertSame(pets, select(List.of(pets, catchAll), "GET", "/pets/cat/1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactAnyRouteWinsOverBroaderGreedyRoute() {
|
||||
Route greedy = route("GET /pets/{proxy+}", "JWT");
|
||||
Route exactAny = route("ANY /pets/cat", "NONE");
|
||||
|
||||
assertSame(exactAny, select(List.of(greedy, exactAny), "GET", "/pets/cat"));
|
||||
assertSame(exactAny, select(List.of(exactAny, greedy), "GET", "/pets/cat"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultRouteIsUsedOnlyWhenNoPathMatches() {
|
||||
Route fallback = route("$default", "JWT");
|
||||
Route specific = route("GET /open/{proxy+}", "NONE");
|
||||
|
||||
assertSame(specific, select(List.of(fallback, specific), "GET", "/open/items"));
|
||||
assertSame(fallback, select(List.of(fallback, specific), "GET", "/closed/items"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noMatchingRouteReturnsNull() {
|
||||
Route get = route("GET /open/{proxy+}", "NONE");
|
||||
|
||||
assertNull(select(List.of(get), "POST", "/open/items"));
|
||||
}
|
||||
|
||||
private static Route select(List<Route> routes, String method, String path) {
|
||||
ApiGatewayV2Service service = mock(ApiGatewayV2Service.class, CALLS_REAL_METHODS);
|
||||
doReturn(routes).when(service).getRoutes(REGION, API_ID);
|
||||
return service.findMatchingRoute(REGION, API_ID, method, path);
|
||||
}
|
||||
|
||||
private static Route route(String key, String authorizationType) {
|
||||
Route route = new Route();
|
||||
route.setRouteKey(key);
|
||||
route.setAuthorizationType(authorizationType);
|
||||
return route;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user