|
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;
|
@@ -989,6 +990,40 @@ public void testApiGatewayAsSupplier() throws Exception {
|
989 | 990 | assertThat(result.get("body")).isEqualTo("\"boom\"");
|
990 | 991 | }
|
991 | 992 |
|
| 993 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 994 | + @Test |
| 995 | + public void testApiGatewayInAndOutInputStream() throws Exception { |
| 996 | + System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName()); |
| 997 | + System.setProperty("spring.cloud.function.definition", "echoInputStreamToString"); |
| 998 | + FunctionInvoker invoker = new FunctionInvoker(); |
| 999 | + |
| 1000 | + InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes()); |
| 1001 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 1002 | + invoker.handleRequest(targetStream, output, null); |
| 1003 | + |
| 1004 | + Map result = mapper.readValue(output.toByteArray(), Map.class); |
| 1005 | + assertThat(result.get("body")).isEqualTo("hello"); |
| 1006 | + Map headers = (Map) result.get("headers"); |
| 1007 | + assertThat(headers).isNotEmpty(); |
| 1008 | + } |
| 1009 | + |
| 1010 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 1011 | + @Test |
| 1012 | + public void testApiGatewayInAndOutInputStreamMsg() throws Exception { |
| 1013 | + System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName()); |
| 1014 | + System.setProperty("spring.cloud.function.definition", "echoInputStreamMsgToString"); |
| 1015 | + FunctionInvoker invoker = new FunctionInvoker(); |
| 1016 | + |
| 1017 | + InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes()); |
| 1018 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 1019 | + invoker.handleRequest(targetStream, output, null); |
| 1020 | + |
| 1021 | + Map result = mapper.readValue(output.toByteArray(), Map.class); |
| 1022 | + assertThat(result.get("body")).isEqualTo("hello"); |
| 1023 | + Map headers = (Map) result.get("headers"); |
| 1024 | + assertThat(headers).isNotEmpty(); |
| 1025 | + } |
| 1026 | + |
992 | 1027 | @SuppressWarnings("rawtypes")
|
993 | 1028 | @Test
|
994 | 1029 | public void testApiGatewayInAndOut() throws Exception {
|
@@ -1426,6 +1461,34 @@ public Function<APIGatewayProxyRequestEvent, String> inputApiEvent() {
|
1426 | 1461 | };
|
1427 | 1462 | }
|
1428 | 1463 |
|
| 1464 | + @Bean |
| 1465 | + |
| 1466 | + public Function<InputStream, String> echoInputStreamToString() { |
| 1467 | + return is -> { |
| 1468 | + try { |
| 1469 | + String result = StreamUtils.copyToString(is, StandardCharsets.UTF_8); |
| 1470 | + return result; |
| 1471 | + } |
| 1472 | + catch (Exception e) { |
| 1473 | + throw new RuntimeException(e); |
| 1474 | + } |
| 1475 | + }; |
| 1476 | + } |
| 1477 | + |
| 1478 | + @Bean |
| 1479 | + |
| 1480 | + public Function<Message<InputStream>, String> echoInputStreamMsgToString() { |
| 1481 | + return msg -> { |
| 1482 | + try { |
| 1483 | + String result = StreamUtils.copyToString(msg.getPayload(), StandardCharsets.UTF_8); |
| 1484 | + return result; |
| 1485 | + } |
| 1486 | + catch (Exception e) { |
| 1487 | + throw new RuntimeException(e); |
| 1488 | + } |
| 1489 | + }; |
| 1490 | + } |
| 1491 | + |
1429 | 1492 | @Bean
|
1430 | 1493 | public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> inputOutputApiEvent() {
|
1431 | 1494 | return v -> {
|
|
0 commit comments