Skip to content

Commit 16901b1

Browse files
committed
Add bindToHttpHandler to WebTestClient
Issue: SPR-15499
1 parent 4db1eb1 commit 16901b1

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ class DefaultWebTestClient implements WebTestClient {
7777

7878

7979
DefaultWebTestClient(WebClient.Builder webClientBuilder, ClientHttpConnector connector,
80-
ExchangeMutatingWebFilter exchangeMutatingWebFilter, Duration timeout) {
80+
ExchangeMutatingWebFilter filter, Duration timeout) {
8181

8282
Assert.notNull(webClientBuilder, "WebClient.Builder is required");
8383

8484
this.wiretapConnector = new WiretapConnector(connector);
8585
this.webClient = webClientBuilder.clientConnector(this.wiretapConnector).build();
86-
this.exchangeMutatingWebFilter = exchangeMutatingWebFilter;
86+
this.exchangeMutatingWebFilter = (filter != null ? filter : new ExchangeMutatingWebFilter());
8787
this.timeout = (timeout != null ? timeout : Duration.ofSeconds(5));
8888
}
8989

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.http.MediaType;
3636
import org.springframework.http.client.reactive.ClientHttpRequest;
3737
import org.springframework.http.codec.ServerCodecConfigurer;
38+
import org.springframework.http.server.reactive.HttpHandler;
3839
import org.springframework.util.MultiValueMap;
3940
import org.springframework.validation.Validator;
4041
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
@@ -140,7 +141,7 @@ public interface WebTestClient {
140141
// Static, factory methods
141142

142143
/**
143-
* Integration testing without a server, targeting specific annotated,
144+
* Integration testing without a server targeting specific annotated,
144145
* WebFlux controllers. The default configuration is the same as for
145146
* {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}
146147
* but can also be further customized through the returned spec.
@@ -152,7 +153,7 @@ static ControllerSpec bindToController(Object... controllers) {
152153
}
153154

154155
/**
155-
* Integration testing without a server, with WebFlux infrastructure detected
156+
* Integration testing without a server with WebFlux infrastructure detected
156157
* from an {@link ApplicationContext} such as {@code @EnableWebFlux}
157158
* Java config and annotated controller Spring beans.
158159
* @param applicationContext the context
@@ -164,14 +165,23 @@ static MockServerSpec<?> bindToApplicationContext(ApplicationContext application
164165
}
165166

166167
/**
167-
* Integration testing without a server, targeting WebFlux functional endpoints.
168+
* Integration testing without a server targeting WebFlux functional endpoints.
168169
* @param routerFunction the RouterFunction to test
169170
* @return the {@link WebTestClient} builder
170171
*/
171172
static MockServerSpec<?> bindToRouterFunction(RouterFunction<?> routerFunction) {
172173
return new RouterFunctionSpec(routerFunction);
173174
}
174175

176+
/**
177+
* Integration testing without a server targeting the given HttpHandler.
178+
* @param httpHandler the handler to test
179+
* @return the {@link WebTestClient} builder
180+
*/
181+
static Builder bindToHttpHandler(HttpHandler httpHandler) {
182+
return new DefaultWebTestClientBuilder(httpHandler, null);
183+
}
184+
175185
/**
176186
* Complete end-to-end integration tests with actual requests to a running server.
177187
* @return the {@link WebTestClient} builder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2017 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+
17+
package org.springframework.test.web.reactive.server.samples.bind;
18+
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.Collections;
21+
22+
import org.junit.Test;
23+
import reactor.core.publisher.Mono;
24+
25+
import org.springframework.core.io.buffer.DataBuffer;
26+
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
27+
import org.springframework.http.server.reactive.HttpHandler;
28+
import org.springframework.test.web.reactive.server.WebTestClient;
29+
import org.springframework.web.server.WebFilter;
30+
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
31+
32+
/**
33+
* Bind to an {@link HttpHandler}.
34+
* @author Rossen Stoyanchev
35+
*/
36+
public class HttpHandlerTests {
37+
38+
39+
@Test
40+
public void testWebFilter() throws Exception {
41+
42+
WebFilter myFilter = (exchange, chain) -> {
43+
DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
44+
buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
45+
return exchange.getResponse().writeWith(Mono.just(buffer));
46+
};
47+
48+
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> Mono.empty())
49+
.filters(Collections.singletonList(myFilter)).build();
50+
51+
WebTestClient.bindToHttpHandler(httpHandler).build()
52+
.get().uri("/")
53+
.exchange()
54+
.expectStatus().isOk()
55+
.expectBody(String.class).isEqualTo("It works!");
56+
}
57+
58+
}

0 commit comments

Comments
 (0)