Browse Source

Complete initialization with bindToRouterFunction

Issue: SPR-17239
pull/1985/merge
Rossen Stoyanchev 6 years ago
parent
commit
ecd0d7f0f6
  1. 7
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java
  2. 75
      spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpecTests.java

7
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpec.java

@ -1,5 +1,5 @@ @@ -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<WebTestClient.Rou @@ -50,7 +50,10 @@ class DefaultRouterFunctionSpec extends AbstractMockServerSpec<WebTestClient.Rou
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies);
return WebHttpHandlerBuilder.webHandler(webHandler);
return WebHttpHandlerBuilder.webHandler(webHandler)
.filters(filters -> filters.addAll(this.handlerStrategies.webFilters()))
.exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers()))
.localeContextResolver(this.handlerStrategies.localeContextResolver());
}
}

75
spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultRouterFunctionSpecTests.java

@ -0,0 +1,75 @@ @@ -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<ServerResponse> 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<ServerResponse> 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();
}
}
Loading…
Cancel
Save