mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
241806a4f3
commit
49d2d74f3f
6 files changed
+256
-19
No files matched your search
+135
@@ -0,0 +1,135 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
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 software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.services.apigatewayv2.ApiGatewayV2Client;
|
||||
import software.amazon.awssdk.services.apigatewayv2.model.IntegrationType;
|
||||
import software.amazon.awssdk.services.lambda.LambdaClient;
|
||||
import software.amazon.awssdk.services.lambda.model.Runtime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* How an HTTP API answers the Lambda proxy results it cannot read, in payload formats 1.0 and 2.0.
|
||||
* Measured on AWS (eu-west-1, 2026-10-19): a function that raises, a 1.0 result without a
|
||||
* statusCode, and a statusCode that is not a whole number from 100 to 599 are answered 500 with
|
||||
* {"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.
|
||||
*/
|
||||
@DisplayName("API Gateway V2 Lambda proxy responses")
|
||||
class ApiGatewayV2LambdaProxyResponseTest {
|
||||
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role";
|
||||
private static final String ERROR = "{\"message\":\"Internal Server Error\"}";
|
||||
private static final String STAGE = "test";
|
||||
|
||||
private static ApiGatewayV2Client apiGatewayV2;
|
||||
private static LambdaClient lambda;
|
||||
private static HttpClient http;
|
||||
private static String functionName;
|
||||
private static String apiId;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
Assumptions.assumeTrue(TestFixtures.isLambdaDispatchAvailable(), "Lambda dispatch is unavailable here");
|
||||
apiGatewayV2 = TestFixtures.apiGatewayV2Client();
|
||||
lambda = TestFixtures.lambdaClient();
|
||||
http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
|
||||
functionName = TestFixtures.uniqueName("http-api-proxy-fn");
|
||||
|
||||
String functionArn = lambda.createFunction(request -> request
|
||||
.functionName(functionName)
|
||||
.runtime(Runtime.NODEJS20_X)
|
||||
.role(ROLE)
|
||||
.handler("index.handler")
|
||||
.timeout(30)
|
||||
.code(code -> code.zipFile(SdkBytes.fromByteArray(LambdaUtils.proxyResponseZip())))).functionArn();
|
||||
apiId = apiGatewayV2.createApi(request -> request
|
||||
.name(TestFixtures.uniqueName("http-api-proxy"))
|
||||
.protocolType("HTTP")).apiId();
|
||||
for (String format : new String[] {"1.0", "2.0"}) {
|
||||
String integrationId = apiGatewayV2.createIntegration(request -> request
|
||||
.apiId(apiId)
|
||||
.integrationType(IntegrationType.AWS_PROXY)
|
||||
.integrationUri(functionArn)
|
||||
.payloadFormatVersion(format)).integrationId();
|
||||
apiGatewayV2.createRoute(request -> request
|
||||
.apiId(apiId)
|
||||
.routeKey("GET /v" + format.charAt(0) + "/{mode}")
|
||||
.target("integrations/" + integrationId));
|
||||
}
|
||||
apiGatewayV2.createStage(request -> request.apiId(apiId).stageName(STAGE).autoDeploy(true));
|
||||
|
||||
// As ApiGatewayV2ExecuteTest does: a result the gateway can read must come back through the
|
||||
// API before the cases run, or this environment cannot dispatch from API Gateway to Lambda.
|
||||
boolean dispatches;
|
||||
try {
|
||||
HttpResponse<String> warmup = get("2.0", "ok");
|
||||
dispatches = warmup.statusCode() == 200 && "ok".equals(warmup.body());
|
||||
} catch (IOException e) {
|
||||
dispatches = false;
|
||||
}
|
||||
Assumptions.assumeTrue(dispatches, "API Gateway does not dispatch to Lambda here");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (apiGatewayV2 != null) {
|
||||
if (apiId != null) {
|
||||
apiGatewayV2.deleteApi(request -> request.apiId(apiId));
|
||||
}
|
||||
apiGatewayV2.close();
|
||||
}
|
||||
if (lambda != null) {
|
||||
lambda.deleteFunction(request -> request.functionName(functionName));
|
||||
lambda.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{1} in payload {0}")
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"1.0|raise|500|" + ERROR,
|
||||
"2.0|raise|500|" + ERROR,
|
||||
"1.0|nostatus|500|" + ERROR,
|
||||
"1.0|badstatus|500|" + ERROR,
|
||||
"2.0|badstatus|500|" + ERROR,
|
||||
"1.0|highstatus|500|" + ERROR,
|
||||
"2.0|highstatus|500|" + ERROR,
|
||||
"1.0|textstatus|201|x",
|
||||
"2.0|textstatus|201|x",
|
||||
"2.0|nostatus|200|{\"body\":\"x\",\"headers\":{\"x-probe\":\"1\"}}"
|
||||
})
|
||||
void aResultIsAnsweredAsAwsReadsIt(String format, String mode, int status, String body) throws Exception {
|
||||
HttpResponse<String> response = get(format, mode);
|
||||
|
||||
assertThat(response.statusCode()).isEqualTo(status);
|
||||
assertThat(response.body()).isEqualTo(body);
|
||||
}
|
||||
|
||||
private static HttpResponse<String> get(String format, String mode) throws IOException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(TestFixtures.endpoint() + "/execute-api/" + apiId + "/" + STAGE + "/v"
|
||||
+ format.charAt(0) + "/" + mode))
|
||||
.timeout(Duration.ofSeconds(30))
|
||||
.GET()
|
||||
.build();
|
||||
try {
|
||||
return http.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("interrupted waiting for " + request.uri(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -334,6 +334,26 @@ public final class LambdaUtils {
|
||||
return createZip("lambda_function.py", code);
|
||||
}
|
||||
|
||||
/**
|
||||
* ZIP containing a Node.js handler that returns the Lambda proxy result its {@code mode} path
|
||||
* parameter names: one that raises, one without a statusCode, one whose statusCode is not a
|
||||
* number, one whose statusCode is text that spells one, and one outside 100 to 599.
|
||||
*/
|
||||
public static byte[] proxyResponseZip() {
|
||||
String code = """
|
||||
exports.handler = async (event) => {
|
||||
const mode = (event.pathParameters || {}).mode;
|
||||
if (mode === 'raise') throw new Error('boom');
|
||||
if (mode === 'nostatus') return { body: 'x', headers: { 'x-probe': '1' } };
|
||||
if (mode === 'badstatus') return { statusCode: 'abc', body: 'x' };
|
||||
if (mode === 'textstatus') return { statusCode: '201', body: 'x' };
|
||||
if (mode === 'highstatus') return { statusCode: 600, body: 'x' };
|
||||
return { statusCode: 200, body: 'ok' };
|
||||
};
|
||||
""";
|
||||
return createZip("index.js", code);
|
||||
}
|
||||
|
||||
private static byte[] createZip(String filename, String content) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
Reference in new issue
Block a user