mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-24 15:49:26 +00:00
Make SslInfo nullable
This allows autowiring a MockServerSpec in test class and using a different identity or none at all with each test. Polishing in MockServerSpecTests. See gh-35042
This commit is contained in:
+40
-23
@@ -18,11 +18,13 @@ package org.springframework.test.web.reactive.server;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.server.reactive.SslInfo;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
@@ -32,7 +34,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AbstractMockServerSpec}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockServerSpecTests {
|
||||
@@ -43,30 +44,31 @@ public class MockServerSpecTests {
|
||||
@Test
|
||||
public void applyFiltersAfterConfigurerAdded() {
|
||||
|
||||
this.serverSpec.webFilter(new TestWebFilter("A"));
|
||||
|
||||
this.serverSpec.apply(new MockServerConfigurer() {
|
||||
MockServerConfigurer configurer = new MockServerConfigurer() {
|
||||
|
||||
@Override
|
||||
public void afterConfigureAdded(WebTestClient.MockServerSpec<?> spec) {
|
||||
spec.webFilter(new TestWebFilter("B"));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.serverSpec.build().get().uri("/")
|
||||
this.serverSpec
|
||||
.webFilter(new TestWebFilter("A"))
|
||||
.apply(configurer)
|
||||
.build()
|
||||
.get().uri("/")
|
||||
.exchange()
|
||||
.expectBody(String.class)
|
||||
.consumeWith(result -> assertThat(
|
||||
result.getResponseBody()).contains("test-attribute=:A:B"));
|
||||
.consumeWith(result -> {
|
||||
String body = result.getResponseBody();
|
||||
assertThat(body).contains("test-attribute=:A:B");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyFiltersBeforeServerCreated() {
|
||||
|
||||
this.serverSpec.webFilter(new TestWebFilter("App-A"));
|
||||
this.serverSpec.webFilter(new TestWebFilter("App-B"));
|
||||
|
||||
this.serverSpec.apply(new MockServerConfigurer() {
|
||||
MockServerConfigurer configurer = new MockServerConfigurer() {
|
||||
|
||||
@Override
|
||||
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
|
||||
@@ -75,21 +77,41 @@ public class MockServerSpecTests {
|
||||
filters.add(1, new TestWebFilter("Fwk-B"));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.serverSpec.build().get().uri("/")
|
||||
.exchange()
|
||||
this.serverSpec
|
||||
.webFilter(new TestWebFilter("App-A"))
|
||||
.webFilter(new TestWebFilter("App-B"))
|
||||
.apply(configurer)
|
||||
.build()
|
||||
.get().uri("/").exchange()
|
||||
.expectBody(String.class)
|
||||
.consumeWith(result -> assertThat(
|
||||
result.getResponseBody()).contains("test-attribute=:Fwk-A:Fwk-B:App-A:App-B"));
|
||||
.consumeWith(result -> {
|
||||
String body = result.getResponseBody();
|
||||
assertThat(body).contains("test-attribute=:Fwk-A:Fwk-B:App-A:App-B");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void sslInfo() {
|
||||
SslInfo info = SslInfo.from("123");
|
||||
this.serverSpec.sslInfo(info).build().get().uri("/").exchange().expectStatus().isOk();
|
||||
assertThat(this.serverSpec.getSavedSslInfo()).isSameAs(info);
|
||||
}
|
||||
|
||||
|
||||
private static class TestMockServerSpec extends AbstractMockServerSpec<TestMockServerSpec> {
|
||||
|
||||
private @Nullable SslInfo savedSslInfo;
|
||||
|
||||
public @Nullable SslInfo getSavedSslInfo() {
|
||||
return this.savedSslInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
|
||||
return WebHttpHandlerBuilder.webHandler(exchange -> {
|
||||
this.savedSslInfo = exchange.getRequest().getSslInfo();
|
||||
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
|
||||
String text = exchange.getAttributes().toString();
|
||||
DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
|
||||
@@ -98,13 +120,8 @@ public class MockServerSpecTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestWebFilter implements WebFilter {
|
||||
|
||||
private final String name;
|
||||
|
||||
TestWebFilter(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
private record TestWebFilter(String name) implements WebFilter {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
|
||||
Reference in New Issue
Block a user