Skip to content

Commit 9ca7b62

Browse files
committed
Introduce MockSslInfo
Closes gh-35078
1 parent a2b90d9 commit 9ca7b62

File tree

1 file changed

+95
-0
lines changed
  • spring-test/src/main/java/org/springframework/mock/http/server/reactive

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.mock.http.server.reactive;
18+
19+
import java.security.cert.X509Certificate;
20+
21+
import org.jspecify.annotations.Nullable;
22+
23+
import org.springframework.core.style.DefaultToStringStyler;
24+
import org.springframework.core.style.SimpleValueStyler;
25+
import org.springframework.core.style.ToStringCreator;
26+
import org.springframework.http.server.reactive.SslInfo;
27+
28+
/**
29+
* Mock implementation of {@link SslInfo} for use in tests without an actual server.
30+
*
31+
* @author Sam Brannen
32+
* @since 7.0
33+
*/
34+
public class MockSslInfo implements SslInfo {
35+
36+
private @Nullable String sessionId;
37+
38+
private X509Certificate @Nullable [] peerCertificates;
39+
40+
41+
/**
42+
* Construct {@code MockSslInfo} without a session ID or certificates.
43+
*/
44+
public MockSslInfo() {
45+
}
46+
47+
/**
48+
* Construct {@code MockSslInfo} with a session ID.
49+
*/
50+
public MockSslInfo(String sessionId) {
51+
this.sessionId = sessionId;
52+
}
53+
54+
/**
55+
* Construct {@code MockSslInfo} with a session ID and certificates.
56+
*/
57+
public MockSslInfo(String sessionId, X509Certificate[] peerCertificates) {
58+
this.sessionId = sessionId;
59+
this.peerCertificates = peerCertificates;
60+
}
61+
62+
63+
/**
64+
* Set the SSL session ID.
65+
*/
66+
public void setSessionId(String sessionId) {
67+
this.sessionId = sessionId;
68+
}
69+
70+
@Override
71+
public @Nullable String getSessionId() {
72+
return this.sessionId;
73+
}
74+
75+
/**
76+
* Set the SSL certificates associated with the request.
77+
*/
78+
public void setPeerCertificates(X509Certificate[] peerCertificates) {
79+
this.peerCertificates = peerCertificates;
80+
}
81+
82+
@Override
83+
public X509Certificate @Nullable [] getPeerCertificates() {
84+
return this.peerCertificates;
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return new ToStringCreator(this, new DefaultToStringStyler(new SimpleValueStyler()))
90+
.append("sessionId", this.sessionId)
91+
.append("peerCertificates", this.peerCertificates)
92+
.toString();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)