Add the missing newline of AWS.
This commit is contained in:
@ -19,6 +19,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
// AwsFunctionRequest contains the java files that needs to be executed.
|
// AwsFunctionRequest contains the java files that needs to be executed.
|
||||||
@ -153,15 +154,14 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
|
|||||||
private void scanForOutput(Process p, InputStream stream, WebSocketMessageType type) {
|
private void scanForOutput(Process p, InputStream stream, WebSocketMessageType type) {
|
||||||
Scanner outputScanner = new Scanner(stream);
|
Scanner outputScanner = new Scanner(stream);
|
||||||
while (p.isAlive() || outputScanner.hasNextLine()) {
|
while (p.isAlive() || outputScanner.hasNextLine()) {
|
||||||
this.sendMessage(type, outputScanner.nextLine(), null);
|
try {
|
||||||
|
this.sendMessage(type, outputScanner.nextLine() + "\n", null);
|
||||||
|
} catch (NoSuchElementException ignored) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendMessage sends WebSocketMessage objects back to the requester of this Lambda function.
|
// sendMessage sends WebSocketMessage objects back to the requester of this Lambda function.
|
||||||
private void sendMessage(WebSocketMessageType type, String data, Integer exitCode) {
|
private void sendMessage(WebSocketMessageType type, String data, Integer exitCode) {
|
||||||
if (this.disableOutput) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
JsonObject msg = new JsonObject();
|
JsonObject msg = new JsonObject();
|
||||||
msg.addProperty("type", type.toString());
|
msg.addProperty("type", type.toString());
|
||||||
if (type == WebSocketMessageType.WebSocketExit) {
|
if (type == WebSocketMessageType.WebSocketExit) {
|
||||||
@ -170,8 +170,12 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
|
|||||||
msg.addProperty("data", data);
|
msg.addProperty("data", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.gwClient.postToConnection(new PostToConnectionRequest()
|
if (this.disableOutput) {
|
||||||
.withConnectionId(this.connectionID)
|
System.out.println(gson.toJson(msg));
|
||||||
.withData(ByteBuffer.wrap(gson.toJson(msg).getBytes(StandardCharsets.UTF_8))));
|
} else {
|
||||||
|
this.gwClient.postToConnection(new PostToConnectionRequest()
|
||||||
|
.withConnectionId(this.connectionID)
|
||||||
|
.withData(ByteBuffer.wrap(gson.toJson(msg).getBytes(StandardCharsets.UTF_8))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
|
|||||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2WebSocketEvent;
|
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2WebSocketEvent;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -28,8 +30,51 @@ public class AppTest {
|
|||||||
" }\n" +
|
" }\n" +
|
||||||
"}").getBytes(StandardCharsets.UTF_8));
|
"}").getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
static final String MultilineMathContent = Base64.getEncoder().encodeToString(
|
||||||
|
("package org.example;\n" +
|
||||||
|
"\n" +
|
||||||
|
"public class RecursiveMath {\n" +
|
||||||
|
"\n" +
|
||||||
|
" public static void main(String[] args) {\n" +
|
||||||
|
" System.out.println(\"Mein Text\");\n" +
|
||||||
|
" System.out.println(\"Mein Text\");\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}").getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void successfulResponse() {
|
public void successfulResponse() {
|
||||||
|
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(RecursiveMathContent);
|
||||||
|
assertEquals(200, result.getStatusCode().intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scanner.nextLine() consumes the new line. Therefore, we need to add it again.
|
||||||
|
@Test
|
||||||
|
public void successfulMultilineResponse() {
|
||||||
|
ByteArrayOutputStream out = setupStdOutLogs();
|
||||||
|
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(MultilineMathContent);
|
||||||
|
restoreStdOutLogs();
|
||||||
|
|
||||||
|
assertEquals(200, result.getStatusCode().intValue());
|
||||||
|
String expectedOutput =
|
||||||
|
"{\"type\":\"stdout\",\"data\":\"Mein Text\\n\"}\n" +
|
||||||
|
"{\"type\":\"stdout\",\"data\":\"Mein Text\\n\"}\n" +
|
||||||
|
"{\"type\":\"exit\",\"data\":0}\n";
|
||||||
|
assertEquals(expectedOutput, out.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private PrintStream originalOut;
|
||||||
|
private ByteArrayOutputStream setupStdOutLogs() {
|
||||||
|
originalOut = System.out;
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(out));
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreStdOutLogs() {
|
||||||
|
System.setOut(originalOut);
|
||||||
|
}
|
||||||
|
|
||||||
|
private APIGatewayProxyResponseEvent getApiGatewayProxyResponse(String content) {
|
||||||
App app = new App();
|
App app = new App();
|
||||||
APIGatewayV2WebSocketEvent input = new APIGatewayV2WebSocketEvent();
|
APIGatewayV2WebSocketEvent input = new APIGatewayV2WebSocketEvent();
|
||||||
APIGatewayV2WebSocketEvent.RequestContext ctx = new APIGatewayV2WebSocketEvent.RequestContext();
|
APIGatewayV2WebSocketEvent.RequestContext ctx = new APIGatewayV2WebSocketEvent.RequestContext();
|
||||||
@ -40,8 +85,7 @@ public class AppTest {
|
|||||||
headers.put(App.disableOutputHeaderKey, "True");
|
headers.put(App.disableOutputHeaderKey, "True");
|
||||||
input.setHeaders(headers);
|
input.setHeaders(headers);
|
||||||
input.setBody("{\"action\":\"java11Exec\",\"cmd\":[\"sh\",\"-c\",\"javac org/example/RecursiveMath.java && java org/example/RecursiveMath\"]," +
|
input.setBody("{\"action\":\"java11Exec\",\"cmd\":[\"sh\",\"-c\",\"javac org/example/RecursiveMath.java && java org/example/RecursiveMath\"]," +
|
||||||
"\"files\":{\"org/example/RecursiveMath.java\":\"" + RecursiveMathContent + "\"}}");
|
"\"files\":{\"org/example/RecursiveMath.java\":\"" + content + "\"}}");
|
||||||
APIGatewayProxyResponseEvent result = app.handleRequest(input, null);
|
return app.handleRequest(input, null);
|
||||||
assertEquals(200, result.getStatusCode().intValue());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user