Add WebTestClientConfigurer

Issue: SPR-15674
This commit is contained in:
Rossen Stoyanchev
2017-06-23 14:45:46 -04:00
parent 4db0ce12e1
commit 8fc3b3bc37
11 changed files with 260 additions and 253 deletions

View File

@@ -0,0 +1,148 @@
/*
* 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.samples;
import java.security.Principal;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.lang.Nullable;
import org.springframework.test.web.reactive.server.MockServerConfigurer;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClientConfigurer;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
/**
* Samples tests that demonstrate applying ServerWebExchange initialization.
* @author Rossen Stoyanchev
*/
public class ExchangeMutatorTests {
private WebTestClient webTestClient;
@Before
public void setUp() throws Exception {
this.webTestClient = WebTestClient.bindToController(new TestController())
.apply(globalIdentity("Pablo"))
.build();
}
@Test
public void useGloballyConfiguredIdentity() throws Exception {
this.webTestClient.get().uri("/userIdentity")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello Pablo!");
}
@Test
public void useLocallyConfiguredIdentity() throws Exception {
withIdentity(this.webTestClient, "Giovanni")
.get().uri("/userIdentity")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello Giovanni!");
}
private static MockServerConfigurer globalIdentity(String userName) {
return new IdentityConfigurer(userName);
}
private static WebTestClient withIdentity(WebTestClient client, String userName) {
return client.mutate().apply(new IdentityConfigurer(userName)).build();
}
@RestController
static class TestController {
@GetMapping("/userIdentity")
public String handle(Principal principal) {
return "Hello " + principal.getName() + "!";
}
}
private static class TestUser implements Principal {
private final String name;
TestUser(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
private static class IdentityConfigurer implements MockServerConfigurer, WebTestClientConfigurer {
private final IdentityFilter filter;
public IdentityConfigurer(String userName) {
this.filter = new IdentityFilter(userName);
}
@Override
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
builder.filters(filters -> filters.add(0, this.filter));
}
@Override
public void afterConfigurerAdded(WebTestClient.Builder builder,
@Nullable WebHttpHandlerBuilder httpHandlerBuilder,
@Nullable ClientHttpConnector connector) {
Assert.notNull(httpHandlerBuilder, "Not a mock server");
httpHandlerBuilder.filters(filters -> {
filters.removeIf(filter -> filter instanceof IdentityFilter);
filters.add(0, this.filter);
});
}
}
private static class IdentityFilter implements WebFilter {
private final Mono<Principal> userMono;
IdentityFilter(String userName) {
this.userMono = Mono.just(new TestUser(userName));
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
exchange = exchange.mutate().principal(this.userMono).build();
return chain.filter(exchange);
}
}
}

View File

@@ -1,106 +0,0 @@
/*
* 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.samples;
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.ExchangeMutatorWebFilter;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
/**
* Samples tests that demonstrate applying ServerWebExchange initialization.
* @author Rossen Stoyanchev
*/
public class ExchangeMutatorWebFilterTests {
private ExchangeMutatorWebFilter exchangeMutator;
private WebTestClient webTestClient;
@Before
public void setUp() throws Exception {
this.exchangeMutator = new ExchangeMutatorWebFilter(userIdentity("Pablo"));
this.webTestClient = WebTestClient.bindToController(new TestController())
.webFilter(this.exchangeMutator)
.build();
}
@Test
public void globalMutator() throws Exception {
this.webTestClient.get().uri("/userIdentity")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello Pablo!");
}
@Test
public void perRequestMutators() throws Exception {
this.webTestClient = WebTestClient.bindToController(new TestController())
.webFilter(this.exchangeMutator)
.configureClient()
.filter(this.exchangeMutator.perClient(userIdentity("Giovanni")))
.build();
this.webTestClient
.get().uri("/userIdentity")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello Giovanni!");
}
private UnaryOperator<ServerWebExchange> userIdentity(String userName) {
return exchange -> exchange.mutate().principal(Mono.just(new TestUser(userName))).build();
}
@RestController
static class TestController {
@GetMapping("/userIdentity")
public String handle(Principal principal) {
return "Hello " + principal.getName() + "!";
}
}
private static class TestUser implements Principal {
private final String name;
TestUser(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
}