diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java index 0e0ec6dd51..b98ff40432 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.apigatewayv2.ApiGatewayV2Client; import software.amazon.awssdk.services.apigatewayv2.model.IntegrationType; @@ -28,7 +29,8 @@ import static org.assertj.core.api.Assertions.assertThat; * {"message":"Internal Server Error"}; a statusCode given as text that spells one is taken; format * 2.0 takes a result without a statusCode as a 200 whose body is the result itself. A body that is * an object, one said to be base64 that is not, and an isBase64Encoded of "yes" are answered 500 - * too, while "True" is read as true. + * too, while "True" is read as true. A result with a body and no Content-Type header of its own is + * answered text/plain; charset=utf-8. */ @DisplayName("API Gateway V2 Lambda proxy responses") class ApiGatewayV2LambdaProxyResponseTest { @@ -128,6 +130,21 @@ class ApiGatewayV2LambdaProxyResponseTest { assertThat(response.body()).isEqualTo(body); } + @ParameterizedTest(name = "payload {0}") + @ValueSource(strings = {"1.0", "2.0"}) + void aResultWithoutAContentTypeIsAnsweredAsTextPlain(String format) throws Exception { + // Measured on AWS (eu-west-1, 2026-10-19): a result with a statusCode and a body but no + // Content-Type header of its own is answered text/plain; charset=utf-8, in both formats. + HttpResponse response = get(format, "ok"); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.headers().firstValue("Content-Type")) + .hasValueSatisfying(contentType -> { + assertThat(contentType).startsWith("text/plain"); + assertThat(contentType.toLowerCase()).contains("charset=utf-8"); + }); + } + private static HttpResponse get(String format, String mode) throws IOException { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(TestFixtures.endpoint() + "/execute-api/" + apiId + "/" + STAGE + "/v" diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 2eca69c32a..3ddb135f1c 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -695,6 +695,10 @@ number from 100 to 599 (text that spells one, such as `"201"`, is taken) are ans boolean `body` is its text. Format 2.0 takes a result without a `statusCode` as a `200` whose body is the result itself. +A result that carries a `statusCode` and a `body` and no `Content-Type` header of its own is +answered `text/plain; charset=utf-8`, in both payload formats (measured). A REST (v1) API keeps +answering `application/json`. + An API with a `corsConfiguration` applies it as AWS does (measured). It ignores the CORS headers an integration returns, and sets its own on whatever answers a route: the integration, or API Gateway itself refusing the request (a JWT or Lambda authorizer's `401` or `403`, an unsigned call to an 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 9e3777d72d..92a2127dd4 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 @@ -1608,6 +1608,11 @@ public class ApiGatewayExecuteController { return buildProxyResponse(result, httpApiV2, httpApiV2); } + // The Content-Type an HTTP API answers with when the function's result carries a body and no + // Content-Type header of its own (measured on AWS, eu-west-1, 2026-10-19, in both payload + // formats). A REST API keeps answering application/json. + private static final String HTTP_API_DEFAULT_CONTENT_TYPE = "text/plain; charset=utf-8"; + // httpApi selects the HTTP API error response; payloadV2 selects the integration's format 2.0 // response rules (inference and cookies). An HTTP API integration can still use format 1.0. Response buildProxyResponse(InvokeResult result, boolean httpApi, boolean payloadV2) { @@ -1670,7 +1675,7 @@ public class ApiGatewayExecuteController { byte[] bytes = base64 ? Base64.getDecoder().decode(bodyStr) : bodyStr.getBytes(); String ct = findHeaderIgnoreCase(multiHeaders, "Content-Type") .or(() -> findHeaderIgnoreCase(respHeaders, "Content-Type")) - .orElse(MediaType.APPLICATION_JSON); + .orElse(httpApi ? HTTP_API_DEFAULT_CONTENT_TYPE : MediaType.APPLICATION_JSON); builder.entity(bytes).type(ct); } return builder.build(); 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 5c791f38bd..0f30855102 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 @@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.apigatewayv2.model.Api; import io.github.hectorvent.floci.services.lambda.model.InvokeResult; import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; @@ -18,6 +19,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import java.net.URI; import java.nio.charset.StandardCharsets; @@ -372,6 +374,19 @@ class ApiGatewayExecuteControllerTest { assertEquals("application/json", response.getMediaType().toString()); } + @ParameterizedTest(name = "payload format {0}") + @ValueSource(strings = {"1.0", "2.0"}) + void buildProxyResponseAnswersTextPlainWhenAnHttpApiResultHasNoContentType(String format) { + // Measured on HTTP APIs (eu-west-1, 2026-10-19): a result with a statusCode and a body but + // no Content-Type header of its own is answered text/plain; charset=utf-8, in both payload + // formats. A REST API keeps answering application/json. + ApiGatewayExecuteController controller = controller(new ObjectMapper()); + Response response = controller.buildProxyResponse(proxyPayload(""" + {"statusCode":200,"headers":{"X-Other":"value"},"body":"hello"} + """), true, "2.0".equals(format)); + assertEquals(MediaType.valueOf("text/plain; charset=utf-8"), response.getMediaType()); + } + @Test void buildProxyResponseDefaultsToJsonWhenContentTypeIsJsonNull() { // A JSON-null Content-Type must fall back to the default, the same as a missing header — diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/HttpApiLambdaProxyResponseInferenceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/HttpApiLambdaProxyResponseInferenceIntegrationTest.java index 978e064be7..1b78f6cee2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/HttpApiLambdaProxyResponseInferenceIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/HttpApiLambdaProxyResponseInferenceIntegrationTest.java @@ -8,16 +8,23 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsStringIgnoringCase; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; /** - * End to end check of how an HTTP API answers a payload format 2.0 Lambda proxy integration whose - * function returns no {@code statusCode} or throws. AWS infers a 200 JSON response whose body is - * the function's result, and answers a function error with {@code {"message":"Internal Server - * Error"}}. An integration configured with format 1.0 keeps the 1.0 response rules. + * End to end check of how an HTTP API answers a Lambda proxy integration whose function returns + * no {@code statusCode} or throws. AWS infers a 200 JSON response whose body is the function's + * result, and answers a function error with {@code {"message":"Internal Server Error"}}. An + * integration configured with format 1.0 keeps the 1.0 response rules. + * + *

A result that carries a {@code statusCode} and a {@code body} and leaves the + * {@code Content-Type} header out is answered {@code text/plain; charset=utf-8}, in both payload + * formats (measured on AWS, eu-west-1, 2026-10-19). A result that names a {@code Content-Type} + * keeps it. */ @QuarkusTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -27,6 +34,9 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest { private static final String STRING_FUNCTION = "httpv2-inference-string-fn"; private static final String THROW_FUNCTION = "httpv2-inference-throw-fn"; private static final String V1_FUNCTION = "httpv2-inference-v1-fn"; + private static final String PLAIN_FUNCTION = "httpv2-inference-plain-fn"; + private static final String PLAIN_V1_FUNCTION = "httpv2-inference-plain-v1-fn"; + private static final String JSON_FUNCTION = "httpv2-inference-json-fn"; private static String apiId; @@ -60,6 +70,12 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest { "exports.handler = async () => { throw new Error('boom'); };"); createProxyRoute(V1_FUNCTION, "/v1", "1.0", "exports.handler = async () => ({ headers: { 'X-Trace': 'value' }, body: 'inner' });"); + createProxyRoute(PLAIN_FUNCTION, "/plain", "2.0", + "exports.handler = async () => ({ statusCode: 200, body: 'hello' });"); + createProxyRoute(PLAIN_V1_FUNCTION, "/plain-v1", "1.0", + "exports.handler = async () => ({ statusCode: 200, body: 'hello' });"); + createProxyRoute(JSON_FUNCTION, "/json", "2.0", + "exports.handler = async () => ({ statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: '{\"a\":1}' });"); } private static void createProxyRoute(String functionName, String path, String handlerCode) throws Exception { @@ -145,12 +161,46 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest { .body("message", equalTo("Internal Server Error")); } + @Test + @Order(14) + void aResultWithoutAContentTypeIsAnsweredAsTextPlain() { + given() + .when().get("/execute-api/" + apiId + "/test/plain") + .then() + .statusCode(200) + .contentType(allOf(startsWith("text/plain"), containsStringIgnoringCase("charset=utf-8"))) + .body(equalTo("hello")); + } + + @Test + @Order(15) + void aResultWithoutAContentTypeIsAnsweredAsTextPlainInPayloadFormatOne() { + given() + .when().get("/execute-api/" + apiId + "/test/plain-v1") + .then() + .statusCode(200) + .contentType(allOf(startsWith("text/plain"), containsStringIgnoringCase("charset=utf-8"))) + .body(equalTo("hello")); + } + + @Test + @Order(16) + void aResultThatNamesAContentTypeKeepsIt() { + given() + .when().get("/execute-api/" + apiId + "/test/json") + .then() + .statusCode(200) + .contentType(startsWith("application/json")) + .body(equalTo("{\"a\":1}")); + } + @Test @Order(999) void cleanup() throws Exception { if (apiId != null) { given().when().delete("/v2/apis/" + apiId); } - WebSocketTestSupport.deleteFunctions(BARE_FUNCTION, STRING_FUNCTION, THROW_FUNCTION, V1_FUNCTION); + WebSocketTestSupport.deleteFunctions(BARE_FUNCTION, STRING_FUNCTION, THROW_FUNCTION, V1_FUNCTION, + PLAIN_FUNCTION, PLAIN_V1_FUNCTION, JSON_FUNCTION); } }