Skip to content

Commit 54192cf

Browse files
committed
WebTestClient (server-less) setup accepts WebFilter's
Issue: SPR-15349
1 parent a99fe3e commit 54192cf

File tree

4 files changed

+101
-33
lines changed

4 files changed

+101
-33
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package org.springframework.test.web.reactive.server;
1818

19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
1923
import java.util.function.UnaryOperator;
2024

21-
import org.springframework.http.server.reactive.HttpHandler;
2225
import org.springframework.web.server.ServerWebExchange;
26+
import org.springframework.web.server.WebFilter;
2327
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
2428

2529
/**
@@ -33,13 +37,26 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
3337

3438
private final ExchangeMutatorWebFilter exchangeMutatorFilter = new ExchangeMutatorWebFilter();
3539

40+
private final List<WebFilter> filters = new ArrayList<>(4);
41+
42+
43+
AbstractMockServerSpec() {
44+
this.filters.add(this.exchangeMutatorFilter);
45+
}
46+
3647

3748
@Override
3849
public <T extends B> T exchangeMutator(UnaryOperator<ServerWebExchange> mutator) {
3950
this.exchangeMutatorFilter.register(mutator);
4051
return self();
4152
}
4253

54+
@Override
55+
public <T extends B> T webFilter(WebFilter... filter) {
56+
this.filters.addAll(Arrays.asList(filter));
57+
return self();
58+
}
59+
4360
@SuppressWarnings("unchecked")
4461
private <T extends B> T self() {
4562
return (T) this;
@@ -48,12 +65,25 @@ private <T extends B> T self() {
4865

4966
@Override
5067
public WebTestClient.Builder configureClient() {
51-
HttpHandler handler = initHttpHandlerBuilder().prependFilter(this.exchangeMutatorFilter).build();
52-
return new DefaultWebTestClientBuilder(handler, this.exchangeMutatorFilter);
68+
WebHttpHandlerBuilder builder = initHttpHandlerBuilder();
69+
filtersInReverse().forEach(builder::prependFilter);
70+
return new DefaultWebTestClientBuilder(builder.build(), this.exchangeMutatorFilter);
5371
}
5472

73+
/**
74+
* Sub-classes to create the {@code WebHttpHandlerBuilder} to use.
75+
*/
5576
protected abstract WebHttpHandlerBuilder initHttpHandlerBuilder();
5677

78+
/**
79+
* Return the filters in reverse order for pre-pending.
80+
*/
81+
private List<WebFilter> filtersInReverse() {
82+
List<WebFilter> result = new ArrayList<>(this.filters);
83+
Collections.reverse(result);
84+
return result;
85+
}
86+
5787
@Override
5888
public WebTestClient build() {
5989
return configureClient().build();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.web.reactive.function.client.WebClient;
5151
import org.springframework.web.reactive.function.server.RouterFunction;
5252
import org.springframework.web.server.ServerWebExchange;
53+
import org.springframework.web.server.WebFilter;
5354
import org.springframework.web.util.UriBuilder;
5455
import org.springframework.web.util.UriBuilderFactory;
5556

@@ -193,6 +194,12 @@ interface MockServerSpec<B extends MockServerSpec<B>> {
193194
*/
194195
<T extends B> T exchangeMutator(UnaryOperator<ServerWebExchange> mutator);
195196

197+
/**
198+
* Configure {@link WebFilter}'s for server request processing.
199+
* @param filter one or more filters
200+
*/
201+
<T extends B> T webFilter(WebFilter... filter);
202+
196203
/**
197204
* Proceed to configure and build the test client.
198205
*/

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
import org.springframework.web.bind.annotation.RestController;
3333
import org.springframework.web.reactive.config.EnableWebFlux;
3434
import org.springframework.web.server.ServerWebExchange;
35-
36-
import static org.mockito.Mockito.mock;
37-
import static org.mockito.Mockito.when;
35+
import org.springframework.web.server.WebFilter;
3836

3937
/**
4038
* Binding to server infrastructure declared in a Spring ApplicationContext.
@@ -56,24 +54,16 @@ public void setUp() throws Exception {
5654

5755
this.client = WebTestClient.bindToApplicationContext(context)
5856
.exchangeMutator(principal("Pablo"))
57+
.webFilter(prefixFilter("Mr."))
5958
.build();
6059
}
6160

62-
private UnaryOperator<ServerWebExchange> principal(String userName) {
63-
return exchange -> {
64-
Principal user = mock(Principal.class);
65-
when(user.getName()).thenReturn(userName);
66-
return exchange.mutate().principal(Mono.just(user)).build();
67-
};
68-
}
69-
70-
7161
@Test
7262
public void basic() throws Exception {
7363
this.client.get().uri("/principal")
7464
.exchange()
7565
.expectStatus().isOk()
76-
.expectBody(String.class).value().isEqualTo("Hello Pablo!");
66+
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
7767
}
7868

7969
@Test
@@ -82,7 +72,7 @@ public void perRequestExchangeMutator() throws Exception {
8272
.get().uri("/principal")
8373
.exchange()
8474
.expectStatus().isOk()
85-
.expectBody(String.class).value().isEqualTo("Hello Giovanni!");
75+
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
8676
}
8777

8878
@Test
@@ -96,6 +86,18 @@ public void perRequestMultipleExchangeMutators() throws Exception {
9686
.expectBody(String.class).value().isEqualTo("foo+bar");
9787
}
9888

89+
90+
private UnaryOperator<ServerWebExchange> principal(String userName) {
91+
return exchange -> exchange.mutate().principal(Mono.just(new TestUser(userName))).build();
92+
}
93+
94+
private WebFilter prefixFilter(String prefix) {
95+
return (exchange, chain) -> {
96+
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser(prefix + " " + p.getName()));
97+
return chain.filter(exchange.mutate().principal(user).build());
98+
};
99+
}
100+
99101
private UnaryOperator<ServerWebExchange> attribute(String attrName, String attrValue) {
100102
return exchange -> {
101103
exchange.getAttributes().put(attrName, attrValue);
@@ -129,4 +131,18 @@ public String handle(@RequestAttribute String attr1, @RequestAttribute String at
129131
}
130132
}
131133

134+
private static class TestUser implements Principal {
135+
136+
private final String name;
137+
138+
TestUser(String name) {
139+
this.name = name;
140+
}
141+
142+
@Override
143+
public String getName() {
144+
return this.name;
145+
}
146+
}
147+
132148
}

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import org.springframework.web.bind.annotation.RequestAttribute;
2828
import org.springframework.web.bind.annotation.RestController;
2929
import org.springframework.web.server.ServerWebExchange;
30-
31-
import static org.mockito.Mockito.mock;
32-
import static org.mockito.Mockito.when;
30+
import org.springframework.web.server.WebFilter;
3331

3432
/**
3533
* Bind to annotated controllers.
@@ -39,27 +37,18 @@
3937
*/
4038
public class ControllerTests {
4139

42-
private final WebTestClient client = WebTestClient
43-
.bindToController(new TestController())
40+
private final WebTestClient client = WebTestClient.bindToController(new TestController())
4441
.exchangeMutator(principal("Pablo"))
42+
.webFilter(prefixFilter("Mr."))
4543
.build();
4644

4745

48-
private UnaryOperator<ServerWebExchange> principal(String userName) {
49-
return exchange -> {
50-
Principal user = mock(Principal.class);
51-
when(user.getName()).thenReturn(userName);
52-
return exchange.mutate().principal(Mono.just(user)).build();
53-
};
54-
}
55-
56-
5746
@Test
5847
public void basic() throws Exception {
5948
this.client.get().uri("/principal")
6049
.exchange()
6150
.expectStatus().isOk()
62-
.expectBody(String.class).value().isEqualTo("Hello Pablo!");
51+
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
6352
}
6453

6554
@Test
@@ -68,7 +57,7 @@ public void perRequestExchangeMutator() throws Exception {
6857
.get().uri("/principal")
6958
.exchange()
7059
.expectStatus().isOk()
71-
.expectBody(String.class).value().isEqualTo("Hello Giovanni!");
60+
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
7261
}
7362

7463
@Test
@@ -82,6 +71,18 @@ public void perRequestMultipleExchangeMutators() throws Exception {
8271
.expectBody(String.class).value().isEqualTo("foo+bar");
8372
}
8473

74+
75+
private UnaryOperator<ServerWebExchange> principal(String userName) {
76+
return exchange -> exchange.mutate().principal(Mono.just(new TestUser(userName))).build();
77+
}
78+
79+
private WebFilter prefixFilter(String prefix) {
80+
return (exchange, chain) -> {
81+
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser(prefix + " " + p.getName()));
82+
return chain.filter(exchange.mutate().principal(user).build());
83+
};
84+
}
85+
8586
private UnaryOperator<ServerWebExchange> attribute(String attrName, String attrValue) {
8687
return exchange -> {
8788
exchange.getAttributes().put(attrName, attrValue);
@@ -104,4 +105,18 @@ public String handle(@RequestAttribute String attr1, @RequestAttribute String at
104105
}
105106
}
106107

108+
private static class TestUser implements Principal {
109+
110+
private final String name;
111+
112+
TestUser(String name) {
113+
this.name = name;
114+
}
115+
116+
@Override
117+
public String getName() {
118+
return this.name;
119+
}
120+
}
121+
107122
}

0 commit comments

Comments
 (0)