diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java index 4949f70649..23b3e69124 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java @@ -44,6 +44,7 @@ import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; @@ -766,7 +767,7 @@ public class ApiGatewayExecuteController { } } - private Response buildProxyResponse(InvokeResult result) { + Response buildProxyResponse(InvokeResult result) { if (result.getPayload() == null || result.getPayload().length == 0) { return Response.status(result.getFunctionError() != null ? 502 : result.getStatusCode()).build(); } @@ -793,9 +794,9 @@ public class ApiGatewayExecuteController { String bodyStr = bodyNode.asText(); boolean isBase64 = node.path("isBase64Encoded").asBoolean(false); byte[] bytes = isBase64 ? Base64.getDecoder().decode(bodyStr) : bodyStr.getBytes(); - String ct = MediaType.APPLICATION_JSON; - JsonNode ctNode = node.path("headers").path("Content-Type"); - if (!ctNode.isMissingNode() && !ctNode.isNull()) ct = ctNode.asText(); + String ct = findHeaderIgnoreCase(multiHeaders, "Content-Type") + .or(() -> findHeaderIgnoreCase(respHeaders, "Content-Type")) + .orElse(MediaType.APPLICATION_JSON); builder.entity(bytes).type(ct); } return builder.build(); @@ -805,6 +806,29 @@ public class ApiGatewayExecuteController { } } + /** + * HTTP header names are case-insensitive on the wire (RFC 7230 §3.2), and Lambda proxy + * integrations commonly return lowercased names (e.g. the AWS Lambda Web Adapter emits + * "content-type", not "Content-Type"). A plain JsonNode#path lookup is exact-case and + * silently misses those, so Content-Type detection needs to scan case-insensitively. + * Handles both the "headers" shape (single string value) and the "multiValueHeaders" + * shape (array value, first element wins). + */ + private static Optional findHeaderIgnoreCase(JsonNode headersNode, String name) { + if (headersNode == null || !headersNode.isObject()) { + return Optional.empty(); + } + var it = headersNode.fields(); + while (it.hasNext()) { + var e = it.next(); + if (e.getKey().equalsIgnoreCase(name)) { + JsonNode value = e.getValue().isArray() ? e.getValue().get(0) : e.getValue(); + return value == null || value.isNull() ? Optional.empty() : Optional.of(value.asText()); + } + } + return Optional.empty(); + } + // ──────────────────────────── AWS (non-proxy) ──────────────────────────── private MultivaluedMap parseFormEncodedBody(String body) { diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java index 6bf58f1686..5d2ef61158 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java @@ -3,11 +3,14 @@ package io.github.hectorvent.floci.services.apigateway; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.services.lambda.model.InvokeResult; import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; +import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.Test; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -109,4 +112,85 @@ class ApiGatewayExecuteControllerTest { objectMapper.valueToTree(List.of("first", "second", "third")), event.path("multiValueHeaders").path("X-Dup")); } + + // ── Lambda proxy response Content-Type header matching ────── + + private static InvokeResult proxyPayload(String payloadJson) { + return new InvokeResult(200, null, payloadJson.getBytes(StandardCharsets.UTF_8), null, "req-1"); + } + + @Test + void buildProxyResponseMatchesLowercaseContentType() { + // The AWS Lambda Web Adapter sidecar (and many handlers) emit lowercase header names. + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":404,"headers":{"content-type":"application/problem+json"},"body":"{}"} + """)); + assertEquals("application/problem+json", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseMatchesExactCaseContentType() { + // The pre-existing exact-case path must keep working — this is a regression guard, + // not a new behavior. + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"headers":{"Content-Type":"text/plain"},"body":"hi"} + """)); + assertEquals("text/plain", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseMatchesMixedCaseContentType() { + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"headers":{"CONTENT-TYPE":"application/xml"},"body":""} + """)); + assertEquals("application/xml", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseDefaultsToJsonWhenNoContentTypeHeaderPresent() { + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"headers":{"X-Other":"value"},"body":"{}"} + """)); + assertEquals("application/json", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseDefaultsToJsonWhenContentTypeIsJsonNull() { + // A JSON-null Content-Type must fall back to the default, the same as a missing header — + // not be coerced to the literal string "null" (an invalid media type). + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"headers":{"content-type":null},"body":"{}"} + """)); + assertEquals("application/json", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseMatchesContentTypeInMultiValueHeaders() { + // multiValueHeaders is a fully valid way to return any header, including Content-Type — + // not just repeated ones — and must be scanned the same as headers. + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"multiValueHeaders":{"content-type":["application/xml"]},"body":""} + """)); + assertEquals("application/xml", response.getMediaType().toString()); + } + + @Test + void buildProxyResponseMultiValueHeadersTakesPrecedenceOverHeaders() { + // API Gateway merges headers and multiValueHeaders, with multiValueHeaders winning + // on conflicts. + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,\ + "headers":{"Content-Type":"text/plain"},\ + "multiValueHeaders":{"Content-Type":["application/xml"]},\ + "body":""} + """)); + assertEquals("application/xml", response.getMediaType().toString()); + } }