mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(apigateway): apply MOCK integration response headers (#1822)
* fix(apigateway): apply mock response headers for OPTIONS * refactor(apigateway): drop the OPTIONS route in favour of #1955 Both this PR and #1955 added an @OPTIONS method on the same @Path in ApiGatewayExecuteController and ApiGatewayUserRequestController, so whichever merged second would not have compiled. #1955's version is the more complete one: it covers ApiGatewayAwsExecuteController (the /_aws/execute-api path) as well, forwards the request body, and skips GlobalCorsFilter for deployed-API paths -- without which a real browser preflight is short-circuited before it ever reaches the route. This PR keeps only its own contribution: invokeMock returned early on the empty-response-template path and dropped static method.response.header.* mappings. ApiGatewayMockCorsHeadersTest now exercises that over GET, since the dropped-header bug is verb-agnostic, leaving the two PRs independently mergeable in either order. * test(dynamodb): retain Quarkus port for cleanup * chore: rerun CI after autoscaling test flake --------- Co-authored-by: abanna <abanna@users.noreply.github.com>
This commit is contained in:
1 parent
5d61e95239
commit
a49c3e57f6
3 files changed
+316
-31
No files matched your search
+61
-31
@@ -44,6 +44,7 @@ import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -1235,43 +1236,72 @@ public class ApiGatewayExecuteController {
|
||||
String template = ir.responseTemplates() != null
|
||||
? ir.responseTemplates().getOrDefault("application/json", "") : "";
|
||||
|
||||
if (template.isEmpty()) {
|
||||
return Response.status(Integer.parseInt(ir.statusCode()))
|
||||
.type(MediaType.APPLICATION_JSON)
|
||||
.build();
|
||||
int status = Integer.parseInt(ir.statusCode());
|
||||
String responseBody = null;
|
||||
Map<String, String> vtlHeaderOverrides = new HashMap<>();
|
||||
|
||||
if (!template.isEmpty()) {
|
||||
// Evaluate the response template through VTL (supports $context.responseOverride etc.)
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
String bodyStr = body != null && body.length > 0 ? new String(body) : null;
|
||||
|
||||
Map<String, String> headerMap = new HashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : headers.getRequestHeaders().entrySet()) {
|
||||
if (!e.getValue().isEmpty()) headerMap.put(e.getKey(), e.getValue().get(0));
|
||||
}
|
||||
Map<String, String> queryMap = new HashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
|
||||
if (!e.getValue().isEmpty()) queryMap.put(e.getKey(), e.getValue().get(0));
|
||||
}
|
||||
Map<String, String> pathMap = new HashMap<>(extractPathParams(resource.getPath(), path));
|
||||
|
||||
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
|
||||
bodyStr, headerMap, queryMap, pathMap, stageName, httpMethod,
|
||||
resource.getPath(), requestId, regionResolver.getAccountId(), null);
|
||||
|
||||
VtlTemplateEngine.EvaluateResult result = vtlEngine.evaluate(template, vtlCtx);
|
||||
if (result.statusOverride() != null) status = result.statusOverride();
|
||||
responseBody = result.body();
|
||||
vtlHeaderOverrides = result.headerOverrides();
|
||||
}
|
||||
|
||||
// Evaluate the response template through VTL (supports $context.responseOverride etc.)
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
String bodyStr = body != null && body.length > 0 ? new String(body) : null;
|
||||
|
||||
Map<String, String> headerMap = new HashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : headers.getRequestHeaders().entrySet()) {
|
||||
if (!e.getValue().isEmpty()) headerMap.put(e.getKey(), e.getValue().get(0));
|
||||
Response.ResponseBuilder rb = Response.status(status).type(MediaType.APPLICATION_JSON);
|
||||
if (responseBody != null) {
|
||||
rb.entity(responseBody);
|
||||
}
|
||||
Map<String, String> queryMap = new HashMap<>();
|
||||
for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
|
||||
if (!e.getValue().isEmpty()) queryMap.put(e.getKey(), e.getValue().get(0));
|
||||
}
|
||||
Map<String, String> pathMap = new HashMap<>(extractPathParams(resource.getPath(), path));
|
||||
|
||||
VtlTemplateEngine.VtlContext vtlCtx = new VtlTemplateEngine.VtlContext(
|
||||
bodyStr, headerMap, queryMap, pathMap, stageName, httpMethod,
|
||||
resource.getPath(), requestId, regionResolver.getAccountId(), null);
|
||||
|
||||
VtlTemplateEngine.EvaluateResult result = vtlEngine.evaluate(template, vtlCtx);
|
||||
|
||||
int status = result.statusOverride() != null
|
||||
? result.statusOverride()
|
||||
: Integer.parseInt(ir.statusCode());
|
||||
|
||||
Response.ResponseBuilder rb = Response.status(status)
|
||||
.entity(result.body())
|
||||
.type(MediaType.APPLICATION_JSON);
|
||||
|
||||
for (Map.Entry<String, String> hdr : result.headerOverrides().entrySet()) {
|
||||
// $context.responseOverride header assignments (VTL) take precedence.
|
||||
for (Map.Entry<String, String> hdr : vtlHeaderOverrides.entrySet()) {
|
||||
rb.header(hdr.getKey(), hdr.getValue());
|
||||
}
|
||||
// Header names a VTL $context.responseOverride already set (HTTP header names are
|
||||
// case-insensitive); these win, so skip a responseParameters entry for the same header
|
||||
// rather than adding a second value for it.
|
||||
Set<String> vtlOverriddenHeaders = new HashSet<>();
|
||||
for (String name : vtlHeaderOverrides.keySet()) {
|
||||
vtlOverriddenHeaders.add(name.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
// Apply static header mappings from the integration response's responseParameters.
|
||||
// This is what makes MOCK-integration CORS work (e.g. OPTIONS preflight returning
|
||||
// Access-Control-Allow-Origin/-Methods/-Headers). A MOCK has no backend, so only
|
||||
// static ('literal') and response-body-JSONPath sources resolve; header sources
|
||||
// (integration.response.header.*) yield null and are skipped.
|
||||
if (ir.responseParameters() != null) {
|
||||
for (Map.Entry<String, String> param : ir.responseParameters().entrySet()) {
|
||||
String dest = param.getKey(); // method.response.header.X-Foo
|
||||
if (!dest.startsWith("method.response.header.")) continue;
|
||||
String headerName = dest.substring("method.response.header.".length());
|
||||
if (vtlOverriddenHeaders.contains(headerName.toLowerCase(Locale.ROOT))) {
|
||||
continue; // a VTL $context.responseOverride for this header takes precedence
|
||||
}
|
||||
String headerValue = resolveResponseParameter(param.getValue(),
|
||||
new HashMap<>(), responseBody != null ? responseBody : "{}");
|
||||
if (headerValue != null) {
|
||||
rb.header(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rb.build();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user