fix(apigatewayv2): answer the Lambda proxy results an HTTP API cannot read with 500 (#5985)

* fix(apigatewayv2): answer the Lambda proxy results an HTTP API cannot read with 500

Measured on HTTP APIs (eu-west-1), with payload formats 1.0 and 2.0:
a function that raises is answered 500 with {"message":"Internal Server
Error"}, not the 502 Floci sent. The same 500 answers what API Gateway
cannot read as a response: in format 1.0 a result that is not an object
with a statusCode (a result without one, a string, a number, null), and
in either format a statusCode that is not a whole number from 100 to 599
(99, 600, 201.5, "abc"); a statusCode given as text that spells one,
"201", is taken, and headers that are not an object are left out.
Format 2.0 keeps inferring a 200 for a result without a statusCode.

Floci answered a format 1.0 result without a statusCode with 200 and
its headers and body, as HttpApiLambdaProxyResponseInferenceIntegrationTest
pinned since #5153; AWS answers 500, and that test and the unit tests
that pinned 502 now expect what AWS answers. A REST API's Lambda proxy
keeps its own 502, untouched.

The HTTP API's generic error responses come from one helper, which the
invocation failures from #5935 (500, or 503 when Lambda throttles) use
too.

* test(compat): skip the HTTP API proxy cases unless the API reaches Lambda

Review of #5985: the test checked only that Lambda can be invoked, not
that API Gateway dispatches to it. It now sends a readable result through
the API first, as ApiGatewayV2ExecuteTest does, and skips the cases when
that does not come back.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-19 09:18:20 -04:00
1 parent 241806a4f3
commit 49d2d74f3f
6 files changed
+256 -19

No files matched your search

@@ -14,6 +14,9 @@ import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
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 java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -27,6 +30,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -247,6 +251,47 @@ class ApiGatewayExecuteControllerTest {
assertNull(ApiGatewayExecuteController.vtlAuthorizerContext(null, Map.of()));
}
// ── HTTP API Lambda proxy responses AWS cannot read ──────
/**
* Measured on HTTP APIs (eu-west-1, 2026-10-19), for payload formats 1.0 and 2.0: a 1.0 result
* that is not an object with a statusCode, and a statusCode that is not a whole number from 100
* to 599, are answered with 500 and the generic message; a statusCode given as text that spells
* one is taken, and headers that are not an object are left out. A function that raises is
* covered below.
*/
@ParameterizedTest(name = "{0} payload {1}")
@MethodSource("httpApiProxyResponses")
void httpApiAnswersAProxyResponseAsAwsReadsIt(String result, String format, int status, String body) {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
try (Response response = controller.buildProxyResponse(proxyPayload(result), true, "2.0".equals(format))) {
assertEquals(status, response.getStatus());
assertEquals(body, bodyOf(response));
}
}
static Stream<Arguments> httpApiProxyResponses() {
String error = "{\"message\":\"Internal Server Error\"}";
return Stream.of(
Arguments.of("\"hello\"", "1.0", 500, error),
Arguments.of("null", "1.0", 500, error),
Arguments.of("5", "1.0", 500, error),
Arguments.of("{\"statusCode\":\"abc\",\"body\":\"x\"}", "1.0", 500, error),
Arguments.of("{\"statusCode\":\"abc\",\"body\":\"x\"}", "2.0", 500, error),
Arguments.of("{\"statusCode\":99,\"body\":\"x\"}", "1.0", 500, error),
Arguments.of("{\"statusCode\":99,\"body\":\"x\"}", "2.0", 500, error),
Arguments.of("{\"statusCode\":600,\"body\":\"x\"}", "1.0", 500, error),
Arguments.of("{\"statusCode\":600,\"body\":\"x\"}", "2.0", 500, error),
Arguments.of("{\"statusCode\":201.5,\"body\":\"x\"}", "1.0", 500, error),
Arguments.of("{\"statusCode\":201.5,\"body\":\"x\"}", "2.0", 500, error),
Arguments.of("{\"statusCode\":\"201\",\"body\":\"x\"}", "1.0", 201, "x"),
Arguments.of("{\"statusCode\":\"201\",\"body\":\"x\"}", "2.0", 201, "x"),
Arguments.of("{\"statusCode\":201,\"headers\":\"x\",\"body\":\"x\"}", "1.0", 201, "x"),
Arguments.of("{\"statusCode\":201,\"headers\":\"x\",\"body\":\"x\"}", "2.0", 201, "x"),
Arguments.of("null", "2.0", 200, "null"),
Arguments.of("5", "2.0", 200, "5"));
}
// ── Lambda proxy response Content-Type header matching ──────
private static InvokeResult proxyPayload(String payloadJson) {
@@ -552,21 +597,21 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
try (Response response = controller.buildProxyResponse(functionError(
"{\"errorType\":\"Error\",\"errorMessage\":\"boom\",\"trace\":[]}"), true)) {
assertEquals(502, response.getStatus());
assertEquals(500, response.getStatus());
assertEquals("application/json", String.valueOf(response.getMediaType()));
assertEquals("{\"message\":\"Internal Server Error\"}", bodyOf(response));
}
}
@Test
void httpApiPayloadV1ResponseWithoutStatusCodeIsNotInferred() {
// An HTTP API integration configured with payloadFormatVersion 1.0 keeps the 1.0 response rules.
void httpApiPayloadV1ResponseWithoutStatusCodeAnswersInternalServerErrorMessage() {
// Format 1.0 infers nothing, so a result without a statusCode is one the HTTP API cannot read.
String payload = "{\"headers\":{\"X-Trace\":\"value\"},\"body\":\"inner\"}";
ApiGatewayExecuteController controller = controller(new ObjectMapper());
try (Response response = controller.buildProxyResponse(proxyPayload(payload), true, false)) {
assertEquals(200, response.getStatus());
assertEquals("inner", bodyOf(response));
assertEquals("value", response.getHeaderString("X-Trace"));
assertEquals(500, response.getStatus());
assertEquals("{\"message\":\"Internal Server Error\"}", bodyOf(response));
assertNull(response.getHeaderString("X-Trace"));
}
}
@@ -575,7 +620,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
try (Response response = controller.buildProxyResponse(functionError(
"{\"errorType\":\"Error\",\"errorMessage\":\"boom\",\"trace\":[]}"), true, false)) {
assertEquals(502, response.getStatus());
assertEquals(500, response.getStatus());
assertEquals("{\"message\":\"Internal Server Error\"}", bodyOf(response));
}
}
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/**
@@ -121,23 +122,27 @@ class HttpApiLambdaProxyResponseInferenceIntegrationTest {
@Test
@Order(12)
void functionErrorAnswersInternalServerErrorMessage() {
// Measured on HTTP APIs (eu-west-1, 2026-10-19): 500, in both payload formats.
given()
.when().get("/execute-api/" + apiId + "/test/throw")
.then()
.statusCode(502)
.statusCode(500)
.contentType(startsWith("application/json"))
.body("message", equalTo("Internal Server Error"));
}
@Test
@Order(13)
void payloadV1ResponseFieldsAreNotInferred() {
void payloadV1ResponseWithoutStatusCodeAnswersInternalServerErrorMessage() {
// Format 1.0 infers nothing: a result with headers and a body but no statusCode is one
// the HTTP API cannot read, and it answers 500 (measured, eu-west-1, 2026-10-19).
given()
.when().get("/execute-api/" + apiId + "/test/v1")
.then()
.statusCode(200)
.header("X-Trace", "value")
.body(equalTo("inner"));
.statusCode(500)
.contentType(startsWith("application/json"))
.header("X-Trace", nullValue())
.body("message", equalTo("Internal Server Error"));
}
@Test