Skip to content

Commit 1b30b46

Browse files
committed
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
1 parent 6091453 commit 1b30b46

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public <T extends B> T webSessionManager(WebSessionManager sessionManager) {
7171
}
7272

7373
@Override
74-
public <T extends B> T sslInfo(SslInfo sslInfo) {
75-
this.sslInfo = sslInfo;
74+
public <T extends B> T sslInfo(@Nullable SslInfo info) {
75+
this.sslInfo = info;
7676
return self();
7777
}
7878

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ interface MockServerSpec<B extends MockServerSpec<B>> {
277277
<T extends B> T webSessionManager(WebSessionManager sessionManager);
278278

279279
/**
280-
* Provide SSL session information and certificates for the mock server.
281-
* @param sslInfo the {@link SslInfo} to use
280+
* Set or reset SSL session information to assign to mock server requests.
281+
* @param info the {@link SslInfo} to use
282282
* @since 7.0
283283
* @see SslInfo#from(String)
284284
* @see SslInfo#from(String, java.security.cert.X509Certificate...)
285285
*/
286-
<T extends B> T sslInfo(SslInfo sslInfo);
286+
<T extends B> T sslInfo(@Nullable SslInfo info);
287287

288288
/**
289289
* Shortcut for pre-packaged customizations to the mock server setup.

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

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.nio.charset.StandardCharsets;
2020

21+
import org.jspecify.annotations.Nullable;
2122
import org.junit.jupiter.api.Test;
2223
import reactor.core.publisher.Mono;
2324

2425
import org.springframework.core.io.buffer.DataBuffer;
2526
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
27+
import org.springframework.http.server.reactive.SslInfo;
2628
import org.springframework.web.server.ServerWebExchange;
2729
import org.springframework.web.server.WebFilter;
2830
import org.springframework.web.server.WebFilterChain;
@@ -32,7 +34,6 @@
3234

3335
/**
3436
* Tests for {@link AbstractMockServerSpec}.
35-
*
3637
* @author Rossen Stoyanchev
3738
*/
3839
public class MockServerSpecTests {
@@ -43,30 +44,31 @@ public class MockServerSpecTests {
4344
@Test
4445
public void applyFiltersAfterConfigurerAdded() {
4546

46-
this.serverSpec.webFilter(new TestWebFilter("A"));
47-
48-
this.serverSpec.apply(new MockServerConfigurer() {
47+
MockServerConfigurer configurer = new MockServerConfigurer() {
4948

5049
@Override
5150
public void afterConfigureAdded(WebTestClient.MockServerSpec<?> spec) {
5251
spec.webFilter(new TestWebFilter("B"));
5352
}
54-
});
53+
};
5554

56-
this.serverSpec.build().get().uri("/")
55+
this.serverSpec
56+
.webFilter(new TestWebFilter("A"))
57+
.apply(configurer)
58+
.build()
59+
.get().uri("/")
5760
.exchange()
5861
.expectBody(String.class)
59-
.consumeWith(result -> assertThat(
60-
result.getResponseBody()).contains("test-attribute=:A:B"));
62+
.consumeWith(result -> {
63+
String body = result.getResponseBody();
64+
assertThat(body).contains("test-attribute=:A:B");
65+
});
6166
}
6267

6368
@Test
6469
public void applyFiltersBeforeServerCreated() {
6570

66-
this.serverSpec.webFilter(new TestWebFilter("App-A"));
67-
this.serverSpec.webFilter(new TestWebFilter("App-B"));
68-
69-
this.serverSpec.apply(new MockServerConfigurer() {
71+
MockServerConfigurer configurer = new MockServerConfigurer() {
7072

7173
@Override
7274
public void beforeServerCreated(WebHttpHandlerBuilder builder) {
@@ -75,21 +77,41 @@ public void beforeServerCreated(WebHttpHandlerBuilder builder) {
7577
filters.add(1, new TestWebFilter("Fwk-B"));
7678
});
7779
}
78-
});
79-
80-
this.serverSpec.build().get().uri("/")
81-
.exchange()
80+
};
81+
82+
this.serverSpec
83+
.webFilter(new TestWebFilter("App-A"))
84+
.webFilter(new TestWebFilter("App-B"))
85+
.apply(configurer)
86+
.build()
87+
.get().uri("/").exchange()
8288
.expectBody(String.class)
83-
.consumeWith(result -> assertThat(
84-
result.getResponseBody()).contains("test-attribute=:Fwk-A:Fwk-B:App-A:App-B"));
89+
.consumeWith(result -> {
90+
String body = result.getResponseBody();
91+
assertThat(body).contains("test-attribute=:Fwk-A:Fwk-B:App-A:App-B");
92+
});
93+
}
94+
95+
@Test
96+
void sslInfo() {
97+
SslInfo info = SslInfo.from("123");
98+
this.serverSpec.sslInfo(info).build().get().uri("/").exchange().expectStatus().isOk();
99+
assertThat(this.serverSpec.getSavedSslInfo()).isSameAs(info);
85100
}
86101

87102

88103
private static class TestMockServerSpec extends AbstractMockServerSpec<TestMockServerSpec> {
89104

105+
private @Nullable SslInfo savedSslInfo;
106+
107+
public @Nullable SslInfo getSavedSslInfo() {
108+
return this.savedSslInfo;
109+
}
110+
90111
@Override
91112
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
92113
return WebHttpHandlerBuilder.webHandler(exchange -> {
114+
this.savedSslInfo = exchange.getRequest().getSslInfo();
93115
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
94116
String text = exchange.getAttributes().toString();
95117
DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
@@ -98,13 +120,8 @@ protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
98120
}
99121
}
100122

101-
private static class TestWebFilter implements WebFilter {
102-
103-
private final String name;
104123

105-
TestWebFilter(String name) {
106-
this.name = name;
107-
}
124+
private record TestWebFilter(String name) implements WebFilter {
108125

109126
@Override
110127
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {

0 commit comments

Comments
 (0)