From 1eb892f4b25baedf0cd2b0e456a2d15aeb2d3f87 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 9 Jul 2018 14:55:27 +0200 Subject: [PATCH] GH-186 ensured FluxReturnValueConfiguration is used Ensured that regardless of the version of Spring and presense of webflux, the FluxReturnValueConfiguration is used Resolves #186 --- .../web/flux/ReactorAutoConfiguration.java | 17 ++--- .../function/web/HeadersToMessageTests.java | 73 +++++++++++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/HeadersToMessageTests.java diff --git a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/flux/ReactorAutoConfiguration.java b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/flux/ReactorAutoConfiguration.java index de3ddcd01..0a9a913a2 100644 --- a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/flux/ReactorAutoConfiguration.java +++ b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/flux/ReactorAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; @@ -41,7 +40,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.util.ClassUtils; import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; @@ -52,6 +50,7 @@ import reactor.core.publisher.Flux; /** * @author Dave Syer * @author Mark Fisher + * @author Oleg Zhurakousky */ @Configuration @ConditionalOnWebApplication @@ -77,7 +76,6 @@ public class ReactorAutoConfiguration { } @Configuration - @ConditionalOnMissingClass("org.springframework.core.ReactiveAdapter") protected static class FluxReturnValueConfiguration { @Bean public FluxReturnValueHandler fluxReturnValueHandler(FunctionInspector inspector, @@ -107,13 +105,10 @@ public class ReactorAutoConfiguration { adapter.getArgumentResolvers()); resolvers.add(0, resolver); adapter.setArgumentResolvers(resolvers); - if (!ClassUtils.isPresent("org.springframework.core.ReactiveAdapter", - null)) { - List handlers = new ArrayList<>( - adapter.getReturnValueHandlers()); - handlers.add(0, context.getBean(FluxReturnValueHandler.class)); - adapter.setReturnValueHandlers(handlers); - } + List handlers = new ArrayList<>( + adapter.getReturnValueHandlers()); + handlers.add(0, context.getBean(FluxReturnValueHandler.class)); + adapter.setReturnValueHandlers(handlers); } }; diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/HeadersToMessageTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/HeadersToMessageTests.java new file mode 100644 index 000000000..ac718ee7c --- /dev/null +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/HeadersToMessageTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.function.web; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.net.URI; +import java.util.function.Function; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.http.ResponseEntity; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * + * @author Oleg Zhurakousky + * + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"spring.cloud.function.web.path=/functions" }) +public class HeadersToMessageTests { + + @Autowired + private TestRestTemplate rest; + + @Test + public void testBodyAndCustomHeaderFromMessagePropagation() throws Exception { + ResponseEntity postForEntity = rest.postForEntity(new URI("/functions/employee"), "{\"name\":\"Bob\",\"age\":25}", String.class); + assertEquals("{\"name\":\"Bob\",\"age\":25}", postForEntity.getBody()); + assertTrue(postForEntity.getHeaders().containsKey("x-content-type")); + assertEquals("application/xml", postForEntity.getHeaders().get("x-content-type").get(0)); + assertEquals("bar", postForEntity.getHeaders().get("foo").get(0)); + } + + @EnableAutoConfiguration + @org.springframework.boot.test.context.TestConfiguration + protected static class TestConfiguration { + @Bean({ "employee"}) + public Function, Message> function() { + return request -> { + Message message = MessageBuilder.withPayload(request.getPayload()) + .setHeader("X-Content-Type", "application/xml") + .setHeader("foo", "bar") + .build(); + return message; + }; + } + } +} +