From f478f5cdc8c73ed302d7c315c72ed18d018b4989 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 19 Jun 2025 13:39:31 +0200 Subject: [PATCH] Introduce factory methods in SslInfo and remove MockSslInfo After further consideration, we have decided to remove the recently introduced MockSslInfo in favor of introducing the following static factory methods directly in the SslInfo interface. - SslInfo.from(String sessionId) - SslInfo from(String sessionId, X509Certificate... peerCertificates) See gh-35042 See gh-35078 --- .../http/server/reactive/MockSslInfo.java | 95 ------------------- .../samples/bind/ApplicationContextTests.java | 4 +- .../http/server/reactive/SslInfo.java | 21 +++- 3 files changed, 22 insertions(+), 98 deletions(-) delete mode 100644 spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockSslInfo.java diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockSslInfo.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockSslInfo.java deleted file mode 100644 index bc8708fb926..00000000000 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockSslInfo.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2002-present 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 - * - * https://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.mock.http.server.reactive; - -import java.security.cert.X509Certificate; - -import org.jspecify.annotations.Nullable; - -import org.springframework.core.style.DefaultToStringStyler; -import org.springframework.core.style.SimpleValueStyler; -import org.springframework.core.style.ToStringCreator; -import org.springframework.http.server.reactive.SslInfo; - -/** - * Mock implementation of {@link SslInfo} for use in tests without an actual server. - * - * @author Sam Brannen - * @since 7.0 - */ -public class MockSslInfo implements SslInfo { - - private @Nullable String sessionId; - - private X509Certificate @Nullable [] peerCertificates; - - - /** - * Construct {@code MockSslInfo} without a session ID or certificates. - */ - public MockSslInfo() { - } - - /** - * Construct {@code MockSslInfo} with a session ID. - */ - public MockSslInfo(String sessionId) { - this.sessionId = sessionId; - } - - /** - * Construct {@code MockSslInfo} with a session ID and certificates. - */ - public MockSslInfo(String sessionId, X509Certificate[] peerCertificates) { - this.sessionId = sessionId; - this.peerCertificates = peerCertificates; - } - - - /** - * Set the SSL session ID. - */ - public void setSessionId(String sessionId) { - this.sessionId = sessionId; - } - - @Override - public @Nullable String getSessionId() { - return this.sessionId; - } - - /** - * Set the SSL certificates associated with the request. - */ - public void setPeerCertificates(X509Certificate[] peerCertificates) { - this.peerCertificates = peerCertificates; - } - - @Override - public X509Certificate @Nullable [] getPeerCertificates() { - return this.peerCertificates; - } - - @Override - public String toString() { - return new ToStringCreator(this, new DefaultToStringStyler(new SimpleValueStyler())) - .append("sessionId", this.sessionId) - .append("peerCertificates", this.peerCertificates) - .toString(); - } - -} diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java index 493631b7fdc..fcd9d7b651c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java @@ -24,7 +24,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.mock.http.server.reactive.MockSslInfo; +import org.springframework.http.server.reactive.SslInfo; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.bind.annotation.GetMapping; @@ -65,7 +65,7 @@ class ApplicationContextTests { @Test // gh-35042 void buildWithSslInfo() { var client = WebTestClient.bindToApplicationContext(context) - .sslInfo(new MockSslInfo("test123")) + .sslInfo(SslInfo.from("test123")) .webFilter(new SslSessionIdFilter()) .build(); diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/SslInfo.java b/spring-web/src/main/java/org/springframework/http/server/reactive/SslInfo.java index 4d2cf47dbb4..4cc0b952e32 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/SslInfo.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/SslInfo.java @@ -24,12 +24,14 @@ import org.jspecify.annotations.Nullable; * A holder for SSL session information. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 5.0.2 + * @see javax.net.ssl.SSLSession */ public interface SslInfo { /** - * Return the SSL session id, if any. + * Return the SSL session ID, if any. */ @Nullable String getSessionId(); @@ -38,4 +40,21 @@ public interface SslInfo { */ X509Certificate @Nullable [] getPeerCertificates(); + + /** + * Create {@link SslInfo} configured with the supplied session ID. + * @since 7.0 + */ + static SslInfo from(String sessionId) { + return new DefaultSslInfo(sessionId, new X509Certificate[0]); + } + + /** + * Create {@link SslInfo} configured with the supplied session ID and certificates. + * @since 7.0 + */ + static SslInfo from(String sessionId, X509Certificate... peerCertificates) { + return new DefaultSslInfo(sessionId, peerCertificates); + } + }