|
57 | 57 | import org.springframework.messaging.converter.AbstractMessageConverter;
|
58 | 58 | import org.springframework.messaging.support.MessageBuilder;
|
59 | 59 | import org.springframework.util.MimeType;
|
| 60 | +import org.springframework.util.StreamUtils; |
60 | 61 |
|
61 | 62 | import static org.assertj.core.api.Assertions.assertThat;
|
62 | 63 | import static org.junit.jupiter.api.Assertions.fail;
|
@@ -971,6 +972,40 @@ public void testApiGatewayAsSupplier() throws Exception {
|
971 | 972 | assertThat(result.get("body")).isEqualTo("\"boom\"");
|
972 | 973 | }
|
973 | 974 |
|
| 975 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 976 | + @Test |
| 977 | + public void testApiGatewayInAndOutInputStream() throws Exception { |
| 978 | + System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName()); |
| 979 | + System.setProperty("spring.cloud.function.definition", "echoInputStreamToString"); |
| 980 | + FunctionInvoker invoker = new FunctionInvoker(); |
| 981 | + |
| 982 | + InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes()); |
| 983 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 984 | + invoker.handleRequest(targetStream, output, null); |
| 985 | + |
| 986 | + Map result = mapper.readValue(output.toByteArray(), Map.class); |
| 987 | + assertThat(result.get("body")).isEqualTo("hello"); |
| 988 | + Map headers = (Map) result.get("headers"); |
| 989 | + assertThat(headers).isNotEmpty(); |
| 990 | + } |
| 991 | + |
| 992 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 993 | + @Test |
| 994 | + public void testApiGatewayInAndOutInputStreamMsg() throws Exception { |
| 995 | + System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName()); |
| 996 | + System.setProperty("spring.cloud.function.definition", "echoInputStreamMsgToString"); |
| 997 | + FunctionInvoker invoker = new FunctionInvoker(); |
| 998 | + |
| 999 | + InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes()); |
| 1000 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 1001 | + invoker.handleRequest(targetStream, output, null); |
| 1002 | + |
| 1003 | + Map result = mapper.readValue(output.toByteArray(), Map.class); |
| 1004 | + assertThat(result.get("body")).isEqualTo("hello"); |
| 1005 | + Map headers = (Map) result.get("headers"); |
| 1006 | + assertThat(headers).isNotEmpty(); |
| 1007 | + } |
| 1008 | + |
974 | 1009 | @SuppressWarnings("rawtypes")
|
975 | 1010 | @Test
|
976 | 1011 | public void testApiGatewayInAndOut() throws Exception {
|
@@ -1400,6 +1435,34 @@ public Function<APIGatewayProxyRequestEvent, String> inputApiEvent() {
|
1400 | 1435 | };
|
1401 | 1436 | }
|
1402 | 1437 |
|
| 1438 | + @Bean |
| 1439 | + |
| 1440 | + public Function<InputStream, String> echoInputStreamToString() { |
| 1441 | + return is -> { |
| 1442 | + try { |
| 1443 | + String result = StreamUtils.copyToString(is, StandardCharsets.UTF_8); |
| 1444 | + return result; |
| 1445 | + } |
| 1446 | + catch (Exception e) { |
| 1447 | + throw new RuntimeException(e); |
| 1448 | + } |
| 1449 | + }; |
| 1450 | + } |
| 1451 | + |
| 1452 | + @Bean |
| 1453 | + |
| 1454 | + public Function<Message<InputStream>, String> echoInputStreamMsgToString() { |
| 1455 | + return msg -> { |
| 1456 | + try { |
| 1457 | + String result = StreamUtils.copyToString(msg.getPayload(), StandardCharsets.UTF_8); |
| 1458 | + return result; |
| 1459 | + } |
| 1460 | + catch (Exception e) { |
| 1461 | + throw new RuntimeException(e); |
| 1462 | + } |
| 1463 | + }; |
| 1464 | + } |
| 1465 | + |
1403 | 1466 | @Bean
|
1404 | 1467 | public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> inputOutputApiEvent() {
|
1405 | 1468 | return v -> {
|
|
0 commit comments