diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java index b99d6a9e53..4251643430 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java @@ -90,15 +90,17 @@ public class RuntimeApiServer { return; } inFlight.put(invocation.getRequestId(), invocation); + byte[] payload = invocation.getPayload(); + String body = (payload != null && payload.length > 0) + ? new String(payload) + : "{}"; ctx.response() .setStatusCode(200) .putHeader("Content-Type", "application/json") .putHeader("Lambda-Runtime-Aws-Request-Id", invocation.getRequestId()) .putHeader("Lambda-Runtime-Invoked-Function-Arn", invocation.getFunctionArn()) .putHeader("Lambda-Runtime-Deadline-Ms", String.valueOf(invocation.getDeadlineMs())) - .end(invocation.getPayload() != null - ? new String(invocation.getPayload()) - : "{}"); + .end(body); } catch (InterruptedException e) { Thread.currentThread().interrupt(); ctx.response().setStatusCode(204).end(); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java index 8adb924689..5b034bcc6f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java @@ -76,6 +76,35 @@ class RuntimeApiServerTest { assertTrue(response.body().contains("key")); } + /** + * Regression: an Invoke with no body (e.g. {@code aws lambda invoke} without + * {@code --payload}) reaches the /next handler as a {@code byte[0]}, not + * {@code null}. The server must still write a valid JSON body ({@code {}}) + * so the managed Node.js runtime's {@code JSON.parse(event)} doesn't throw + * "Unexpected end of JSON input" before the handler runs. + */ + @Test + @Timeout(15) + void nextEndpoint_emptyPayload_isDeliveredAsEmptyJsonObject() throws Exception { + PendingInvocation invocation = new PendingInvocation( + "req-empty", new byte[0], System.currentTimeMillis() + 60_000, + "arn:aws:lambda:us-east-1:000000000000:function:test", + new CompletableFuture<>()); + server.enqueue(invocation); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next")) + .GET() + .build(); + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + + assertEquals(200, response.statusCode()); + assertEquals("req-empty", + response.headers().firstValue("Lambda-Runtime-Aws-Request-Id").orElse("")); + assertEquals("{}", response.body(), + "empty Invoke payload must be normalised to '{}' so JSON.parse() in the runtime succeeds"); + } + @Test @Timeout(45) void nextEndpoint_returns204OnTimeout_thenReturns200OnRepoll() throws Exception {