WebTestClient (server-less) setup accepts WebFilter's

Issue: SPR-15349
This commit is contained in:
Rossen Stoyanchev
2017-03-16 15:38:49 -04:00
parent a99fe3eda4
commit 54192cf513
4 changed files with 101 additions and 33 deletions

View File

@@ -32,9 +32,7 @@ import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.server.ServerWebExchange;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.web.server.WebFilter;
/**
* Binding to server infrastructure declared in a Spring ApplicationContext.
@@ -56,24 +54,16 @@ public class ApplicationContextTests {
this.client = WebTestClient.bindToApplicationContext(context)
.exchangeMutator(principal("Pablo"))
.webFilter(prefixFilter("Mr."))
.build();
}
private UnaryOperator<ServerWebExchange> principal(String userName) {
return exchange -> {
Principal user = mock(Principal.class);
when(user.getName()).thenReturn(userName);
return exchange.mutate().principal(Mono.just(user)).build();
};
}
@Test
public void basic() throws Exception {
this.client.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("Hello Pablo!");
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
}
@Test
@@ -82,7 +72,7 @@ public class ApplicationContextTests {
.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("Hello Giovanni!");
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
}
@Test
@@ -96,6 +86,18 @@ public class ApplicationContextTests {
.expectBody(String.class).value().isEqualTo("foo+bar");
}
private UnaryOperator<ServerWebExchange> principal(String userName) {
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);
@@ -129,4 +131,18 @@ public class ApplicationContextTests {
}
}
private static class TestUser implements Principal {
private final String name;
TestUser(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
}

View File

@@ -27,9 +27,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.web.server.WebFilter;
/**
* Bind to annotated controllers.
@@ -39,27 +37,18 @@ import static org.mockito.Mockito.when;
*/
public class ControllerTests {
private final WebTestClient client = WebTestClient
.bindToController(new TestController())
private final WebTestClient client = WebTestClient.bindToController(new TestController())
.exchangeMutator(principal("Pablo"))
.webFilter(prefixFilter("Mr."))
.build();
private UnaryOperator<ServerWebExchange> principal(String userName) {
return exchange -> {
Principal user = mock(Principal.class);
when(user.getName()).thenReturn(userName);
return exchange.mutate().principal(Mono.just(user)).build();
};
}
@Test
public void basic() throws Exception {
this.client.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("Hello Pablo!");
.expectBody(String.class).value().isEqualTo("Hello Mr. Pablo!");
}
@Test
@@ -68,7 +57,7 @@ public class ControllerTests {
.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).value().isEqualTo("Hello Giovanni!");
.expectBody(String.class).value().isEqualTo("Hello Mr. Giovanni!");
}
@Test
@@ -82,6 +71,18 @@ public class ControllerTests {
.expectBody(String.class).value().isEqualTo("foo+bar");
}
private UnaryOperator<ServerWebExchange> principal(String userName) {
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);
@@ -104,4 +105,18 @@ public class ControllerTests {
}
}
private static class TestUser implements Principal {
private final String name;
TestUser(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
}