Replace bindToHttpHandler with bindToWebHandler

This commit replaces the option to bind the WebTestClient to an
HttpHandler to bind to a WebHandler instead.

This allows testing below the WebFlux level such as WebFilter,
WebHandler, or WebSession scenarios, but still a level above
HttpHandler so that WebTestClient is in charge of creating the
ServerWebExchange and expose consistently the
WebTestClient#MockServerSpec setup across all "mock" server bindToXxx
options.

Issue: SPR-15570
This commit is contained in:
Rossen Stoyanchev
2017-05-24 13:56:38 -04:00
parent 246e72ff2f
commit 4d4c3d5c0b
4 changed files with 62 additions and 22 deletions

View File

@@ -0,0 +1,43 @@
/*
* 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.util.Assert;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
/**
*
* @author Rossen Stoyanchev
* @since 5.0
*/
class DefaultMockServerSpec extends AbstractMockServerSpec<DefaultMockServerSpec> {
private final WebHandler webHandler;
DefaultMockServerSpec(WebHandler webHandler) {
Assert.notNull(webHandler, "'WebHandler' is required");
this.webHandler = webHandler;
}
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
return WebHttpHandlerBuilder.webHandler(this.webHandler);
}
}

View File

@@ -33,9 +33,10 @@ import org.springframework.web.server.WebFilterChain;
* transformations during requests from the {@code WebTestClient} to a mock
* server -- i.e. when one of the following is in use:
* <ul>
* <li>{@link WebTestClient#bindToController},
* <li>{@link WebTestClient#bindToController}
* <li>{@link WebTestClient#bindToRouterFunction}
* <li>{@link WebTestClient#bindToApplicationContext}.
* <li>{@link WebTestClient#bindToApplicationContext}
* <li>{@link WebTestClient#bindToWebHandler}
* </ul>
*
* <p>Example of registering a "global" transformation:

View File

@@ -34,7 +34,6 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.Validator;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
@@ -51,6 +50,7 @@ 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.WebFilter;
import org.springframework.web.server.WebHandler;
import org.springframework.web.util.UriBuilder;
import org.springframework.web.util.UriBuilderFactory;
@@ -130,7 +130,7 @@ public interface WebTestClient {
// Static, factory methods
/**
* Integration testing without a server targeting specific annotated,
* Integration testing with a "mock" server targeting specific annotated,
* WebFlux controllers. The default configuration is the same as for
* {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}
* but can also be further customized through the returned spec.
@@ -142,9 +142,9 @@ public interface WebTestClient {
}
/**
* Integration testing without a server with WebFlux infrastructure detected
* from an {@link ApplicationContext} such as {@code @EnableWebFlux}
* Java config and annotated controller Spring beans.
* Integration testing with a "mock" server with WebFlux infrastructure
* detected from an {@link ApplicationContext} such as
* {@code @EnableWebFlux} Java config and annotated controller Spring beans.
* @param applicationContext the context
* @return the {@link WebTestClient} builder
* @see org.springframework.web.reactive.config.EnableWebFlux
@@ -163,12 +163,12 @@ public interface WebTestClient {
}
/**
* Integration testing without a server targeting the given HttpHandler.
* @param httpHandler the handler to test
* Integration testing with a "mock" server targeting the given WebHandler.
* @param webHandler the handler to test
* @return the {@link WebTestClient} builder
*/
static Builder bindToHttpHandler(HttpHandler httpHandler) {
return new DefaultWebTestClientBuilder(httpHandler);
static MockServerSpec<?> bindToWebHandler(WebHandler webHandler) {
return new DefaultMockServerSpec(webHandler);
}
/**