Skip to content

Commit

Permalink
Complete initialization with bindToRouterFunction
Browse files Browse the repository at this point in the history
Issue: SPR-17239
  • Loading branch information
rstoyanchev committed Oct 12, 2018
1 parent 309e70a commit ecd0d7f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -50,7 +50,10 @@ public WebTestClient.RouterFunctionSpec handlerStrategies(HandlerStrategies hand
@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());
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit ecd0d7f

Please sign in to comment.