diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java index 85235bb1cb..deba044296 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-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. @@ -50,7 +50,10 @@ class DefaultRouterFunctionSpec extends AbstractMockServerSpec filters.addAll(this.handlerStrategies.webFilters())) + .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) + .localeContextResolver(this.handlerStrategies.localeContextResolver()); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpecTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpecTests.java new file mode 100644 index 0000000000..84f9aec052 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpecTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-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.test.web.reactive.server; + +import org.junit.Test; +import reactor.core.publisher.Mono; + +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.server.HandlerStrategies; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.RouterFunctions; +import org.springframework.web.reactive.function.server.ServerResponse; + +/** + * Unit tests for {@link DefaultRouterFunctionSpec}. + * @author Rossen Stoyanchev + */ +public class DefaultRouterFunctionSpecTests { + + @Test + public void webFilter() { + + RouterFunction routerFunction = RouterFunctions.route() + .GET("/", request -> ServerResponse.ok().build()) + .build(); + + new DefaultRouterFunctionSpec(routerFunction) + .handlerStrategies(HandlerStrategies.builder() + .webFilter((exchange, chain) -> { + exchange.getResponse().getHeaders().set("foo", "123"); + return chain.filter(exchange); + }) + .build()) + .build() + .get() + .uri("/") + .exchange() + .expectStatus().isOk() + .expectHeader().valueEquals("foo", "123"); + } + + @Test + public void exceptionHandler() { + + RouterFunction routerFunction = RouterFunctions.route() + .GET("/error", request -> Mono.error(new IllegalStateException("boo"))) + .build(); + + new DefaultRouterFunctionSpec(routerFunction) + .handlerStrategies(HandlerStrategies.builder() + .exceptionHandler((exchange, ex) -> { + exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); + return Mono.empty(); + }) + .build()) + .build() + .get() + .uri("/error") + .exchange() + .expectStatus().isBadRequest(); + } +}