Refactor WebTestClient exchange mutator support

This commit factors ServerWebExchange mutator support out of
WebTestClient in favor of an independent public class called
MockServerExchangeMutator which implements WebFilter and can be
applied to the WebTestClient as any other WebFilter.

The MockServerExchangeMutator also exposes a method to apply
a client-side filter for "per request" mutators. See the Javadoc
of the MockServerExchangeMutator.

Issue: SPR-15570
This commit is contained in:
Rossen Stoyanchev
2017-05-24 13:37:09 -04:00
parent a5b94f3a77
commit 246e72ff2f
8 changed files with 171 additions and 181 deletions

View File

@@ -26,6 +26,7 @@ import reactor.core.publisher.Mono;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.web.reactive.server.MockServerExchangeMutator;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
@@ -46,6 +47,8 @@ public class ApplicationContextTests {
private WebTestClient client;
private MockServerExchangeMutator exchangeMutator;
@Before
public void setUp() throws Exception {
@@ -54,9 +57,15 @@ public class ApplicationContextTests {
context.register(WebConfig.class);
context.refresh();
this.exchangeMutator = new MockServerExchangeMutator(principal("Pablo"));
WebFilter userPrefixFilter = (exchange, chain) -> {
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser("Mr. " + p.getName()));
return chain.filter(exchange.mutate().principal(user).build());
};
this.client = WebTestClient.bindToApplicationContext(context)
.exchangeMutator(principal("Pablo"))
.webFilter(prefixFilter("Mr."))
.webFilter(this.exchangeMutator, userPrefixFilter)
.build();
}
@@ -79,7 +88,7 @@ public class ApplicationContextTests {
@Test
public void perRequestExchangeMutator() throws Exception {
this.client.exchangeMutator(principal("Giovanni"))
this.exchangeMutator.filterClient(this.client, principal("Giovanni"))
.get().uri("/principal")
.exchange()
.expectStatus().isOk()
@@ -88,9 +97,8 @@ public class ApplicationContextTests {
@Test
public void perRequestMultipleExchangeMutators() throws Exception {
this.client
.exchangeMutator(attribute("attr1", "foo"))
.exchangeMutator(attribute("attr2", "bar"))
this.exchangeMutator
.filterClient(this.client, attribute("attr1", "foo"), attribute("attr2", "bar"))
.get().uri("/attributes")
.exchange()
.expectStatus().isOk()
@@ -102,13 +110,6 @@ public class ApplicationContextTests {
return exchange -> exchange.mutate().principal(Mono.just(new TestUser(userName))).build();
}
private WebFilter prefixFilter(String prefix) {
return (exchange, chain) -> {
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser(prefix + " " + p.getName()));
return chain.filter(exchange.mutate().principal(user).build());
};
}
private UnaryOperator<ServerWebExchange> attribute(String attrName, String attrValue) {
return exchange -> {
exchange.getAttributes().put(attrName, attrValue);

View File

@@ -19,9 +19,11 @@ package org.springframework.test.web.reactive.server.samples.bind;
import java.security.Principal;
import java.util.function.UnaryOperator;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.test.web.reactive.server.MockServerExchangeMutator;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
@@ -39,11 +41,25 @@ import static org.junit.Assert.assertEquals;
*/
public class ControllerTests {
private final WebTestClient client = WebTestClient.bindToController(new TestController())
.exchangeMutator(principal("Pablo"))
.webFilter(prefixFilter("Mr."))
.build();
private WebTestClient client;
private MockServerExchangeMutator exchangeMutator;
@Before
public void setUp() throws Exception {
this.exchangeMutator = new MockServerExchangeMutator(principal("Pablo"));
WebFilter userPrefixFilter = (exchange, chain) -> {
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser("Mr. " + p.getName()));
return chain.filter(exchange.mutate().principal(user).build());
};
this.client = WebTestClient.bindToController(new TestController())
.webFilter(this.exchangeMutator, userPrefixFilter)
.build();
}
@Test
public void bodyContent() throws Exception {
@@ -63,7 +79,7 @@ public class ControllerTests {
@Test
public void perRequestExchangeMutator() throws Exception {
this.client.exchangeMutator(principal("Giovanni"))
this.exchangeMutator.filterClient(this.client, principal("Giovanni"))
.get().uri("/principal")
.exchange()
.expectStatus().isOk()
@@ -72,9 +88,8 @@ public class ControllerTests {
@Test
public void perRequestMultipleExchangeMutators() throws Exception {
this.client
.exchangeMutator(attribute("attr1", "foo"))
.exchangeMutator(attribute("attr2", "bar"))
this.exchangeMutator
.filterClient(this.client, attribute("attr1", "foo"), attribute("attr2", "bar"))
.get().uri("/attributes")
.exchange()
.expectStatus().isOk()
@@ -86,13 +101,6 @@ public class ControllerTests {
return exchange -> exchange.mutate().principal(Mono.just(new TestUser(userName))).build();
}
private WebFilter prefixFilter(String prefix) {
return (exchange, chain) -> {
Mono<Principal> user = exchange.getPrincipal().map(p -> new TestUser(prefix + " " + p.getName()));
return chain.filter(exchange.mutate().principal(user).build());
};
}
private UnaryOperator<ServerWebExchange> attribute(String attrName, String attrValue) {
return exchange -> {
exchange.getAttributes().put(attrName, attrValue);