Browse Source

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
pull/1325/merge
Rossen Stoyanchev 8 years ago
parent
commit
4d4c3d5c0b
  1. 43
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultMockServerSpec.java
  2. 5
      spring-test/src/main/java/org/springframework/test/web/reactive/server/MockServerExchangeMutator.java
  3. 18
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
  4. 18
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/WebFilterTests.java

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

@ -0,0 +1,43 @@ @@ -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);
}
}

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

@ -33,9 +33,10 @@ import org.springframework.web.server.WebFilterChain; @@ -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:

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

@ -34,7 +34,6 @@ import org.springframework.http.HttpHeaders; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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);
}
/**

18
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpHandlerTests.java → spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/WebFilterTests.java

@ -17,39 +17,35 @@ @@ -17,39 +17,35 @@
package org.springframework.test.web.reactive.server.samples.bind;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
/**
* Bind to an {@link HttpHandler}.
* Tests for a {@link WebFilter}.
* @author Rossen Stoyanchev
*/
public class HttpHandlerTests {
public class WebFilterTests {
@Test
public void testWebFilter() throws Exception {
WebFilter myFilter = (exchange, chain) -> {
WebFilter filter = (exchange, chain) -> {
DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Mono.just(buffer));
};
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> Mono.empty())
.filters(Collections.singletonList(myFilter)).build();
WebTestClient client = WebTestClient.bindToWebHandler(exchange -> Mono.empty())
.webFilter(filter)
.build();
WebTestClient.bindToHttpHandler(httpHandler).build()
.get().uri("/")
client.get().uri("/")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("It works!");
Loading…
Cancel
Save