Skip to content

Commit ecd0d7f

Browse files
committed
Complete initialization with bindToRouterFunction
Issue: SPR-17239
1 parent 309e70a commit ecd0d7f

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,10 @@ public WebTestClient.RouterFunctionSpec handlerStrategies(HandlerStrategies hand
5050
@Override
5151
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
5252
WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies);
53-
return WebHttpHandlerBuilder.webHandler(webHandler);
53+
return WebHttpHandlerBuilder.webHandler(webHandler)
54+
.filters(filters -> filters.addAll(this.handlerStrategies.webFilters()))
55+
.exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers()))
56+
.localeContextResolver(this.handlerStrategies.localeContextResolver());
5457
}
5558

5659
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.test.web.reactive.server;
17+
18+
import org.junit.Test;
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.web.reactive.function.server.HandlerStrategies;
23+
import org.springframework.web.reactive.function.server.RouterFunction;
24+
import org.springframework.web.reactive.function.server.RouterFunctions;
25+
import org.springframework.web.reactive.function.server.ServerResponse;
26+
27+
/**
28+
* Unit tests for {@link DefaultRouterFunctionSpec}.
29+
* @author Rossen Stoyanchev
30+
*/
31+
public class DefaultRouterFunctionSpecTests {
32+
33+
@Test
34+
public void webFilter() {
35+
36+
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route()
37+
.GET("/", request -> ServerResponse.ok().build())
38+
.build();
39+
40+
new DefaultRouterFunctionSpec(routerFunction)
41+
.handlerStrategies(HandlerStrategies.builder()
42+
.webFilter((exchange, chain) -> {
43+
exchange.getResponse().getHeaders().set("foo", "123");
44+
return chain.filter(exchange);
45+
})
46+
.build())
47+
.build()
48+
.get()
49+
.uri("/")
50+
.exchange()
51+
.expectStatus().isOk()
52+
.expectHeader().valueEquals("foo", "123");
53+
}
54+
55+
@Test
56+
public void exceptionHandler() {
57+
58+
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route()
59+
.GET("/error", request -> Mono.error(new IllegalStateException("boo")))
60+
.build();
61+
62+
new DefaultRouterFunctionSpec(routerFunction)
63+
.handlerStrategies(HandlerStrategies.builder()
64+
.exceptionHandler((exchange, ex) -> {
65+
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
66+
return Mono.empty();
67+
})
68+
.build())
69+
.build()
70+
.get()
71+
.uri("/error")
72+
.exchange()
73+
.expectStatus().isBadRequest();
74+
}
75+
}

0 commit comments

Comments
 (0)