Support RouterFunction in @EnableWebFlux
This commit removes the RouterFunctions.toHandlerMapping method, in favor of native support for RouterFunctions in @EnableWebFlux configuration classes. In order to accomplish this, the HandlerStrategies components has been repurposed to only be used for the "bare-bones" HttpHandler, while the (newly introduced) RouterFunctionMapping uses the strategies as exposed through WebFluxConfigurationSupport. Furthermore, this commit also introduces support for testing RouterFunctions without resorting to an application context. Issue: SPR-15536
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.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.server.WebHandler;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
|
||||
/**
|
||||
* Spec for setting up server-less testing against a RouterFunction.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
*/
|
||||
class DefaultRouterFunctionSpec extends AbstractMockServerSpec<WebTestClient.RouterFunctionSpec>
|
||||
implements WebTestClient.RouterFunctionSpec {
|
||||
|
||||
private final RouterFunction<?> routerFunction;
|
||||
|
||||
private HandlerStrategies handlerStrategies = HandlerStrategies.withDefaults();
|
||||
|
||||
|
||||
DefaultRouterFunctionSpec(RouterFunction<?> routerFunction) {
|
||||
this.routerFunction = routerFunction;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public WebTestClient.RouterFunctionSpec handlerStrategies(HandlerStrategies handlerStrategies) {
|
||||
if (handlerStrategies != null) {
|
||||
this.handlerStrategies = handlerStrategies;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
|
||||
WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies);
|
||||
return WebHttpHandlerBuilder.webHandler(webHandler);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.web.reactive.DispatcherHandler;
|
||||
import org.springframework.web.reactive.HandlerAdapter;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.HandlerResultHandler;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.support.HandlerFunctionAdapter;
|
||||
import org.springframework.web.reactive.function.server.support.ServerResponseResultHandler;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
|
||||
/**
|
||||
* Spec for setting up server-less testing against a RouterFunction.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class RouterFunctionSpec extends AbstractMockServerSpec<RouterFunctionSpec> {
|
||||
|
||||
private final RouterFunction<?> routerFunction;
|
||||
|
||||
|
||||
RouterFunctionSpec(RouterFunction<?> routerFunction) {
|
||||
this.routerFunction = routerFunction;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
|
||||
return WebHttpHandlerBuilder.applicationContext(initApplicationContext());
|
||||
}
|
||||
|
||||
private ApplicationContext initApplicationContext() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.registerBean("webHandler", DispatcherHandler.class, () -> new DispatcherHandler());
|
||||
context.registerBean(HandlerMapping.class, () -> RouterFunctions.toHandlerMapping(this.routerFunction));
|
||||
context.registerBean(HandlerAdapter.class, () -> new HandlerFunctionAdapter());
|
||||
context.registerBean(HandlerResultHandler.class, () -> new ServerResponseResultHandler());
|
||||
context.refresh();
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFunction;
|
||||
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.server.HandlerStrategies;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -169,8 +170,8 @@ public interface WebTestClient {
|
||||
* @param routerFunction the RouterFunction to test
|
||||
* @return the {@link WebTestClient} builder
|
||||
*/
|
||||
static MockServerSpec<?> bindToRouterFunction(RouterFunction<?> routerFunction) {
|
||||
return new RouterFunctionSpec(routerFunction);
|
||||
static RouterFunctionSpec bindToRouterFunction(RouterFunction<?> routerFunction) {
|
||||
return new DefaultRouterFunctionSpec(routerFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -285,6 +286,18 @@ public interface WebTestClient {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Specification for customizing router function configuration.
|
||||
*/
|
||||
interface RouterFunctionSpec extends MockServerSpec<RouterFunctionSpec> {
|
||||
|
||||
/**
|
||||
* Configure handler strategies.
|
||||
*/
|
||||
RouterFunctionSpec handlerStrategies(HandlerStrategies handlerStrategies);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Steps for customizing the {@link WebClient} used to test with
|
||||
* internally delegating to a {@link WebClient.Builder}.
|
||||
|
||||
Reference in New Issue
Block a user