46
46
import jdk .httpclient .test .lib .common .ServerNameMatcher ;
47
47
import jdk .test .lib .net .SimpleSSLContext ;
48
48
import jdk .test .lib .net .URIBuilder ;
49
- import org .junit .jupiter .api .BeforeAll ;
50
49
import org .junit .jupiter .params .ParameterizedTest ;
51
50
import org .junit .jupiter .params .provider .ValueSource ;
52
51
import static java .nio .charset .StandardCharsets .US_ASCII ;
70
69
public class HttpClientSNITest {
71
70
private static final String RESP_BODY_TEXT = "hello world" ;
72
71
73
- private static SSLContext sslContext ;
74
-
75
72
private static final class Handler implements HttpTestHandler {
76
73
77
74
@ Override
@@ -85,12 +82,6 @@ public void handle(final HttpTestExchange exch) throws IOException {
85
82
}
86
83
}
87
84
88
- @ BeforeAll
89
- static void beforeAll () throws Exception {
90
- sslContext = new SimpleSSLContext ().get ();
91
- assertNotNull (sslContext , "could not create a SSLContext" );
92
- }
93
-
94
85
/*
95
86
* Creates and configures a HTTPS server with a SNIMatcher that
96
87
* expects a specific SNI name to be sent by the connection client.
@@ -104,9 +95,11 @@ static void beforeAll() throws Exception {
104
95
@ ParameterizedTest
105
96
@ ValueSource (booleans = {false , true })
106
97
void testRequestToIPLiteralHost (final boolean sniConfiguredOnClient ) throws Exception {
98
+ final SSLContext sslContext = new SimpleSSLContext ().get ();
99
+ assertNotNull (sslContext , "could not create a SSLContext" );
107
100
final String expectedSNI = "non-dns-resolvable.foo.bar.localhost" ;
108
101
final ServerNameMatcher matcher = new ServerNameMatcher (expectedSNI );
109
- final HttpTestServer server = createServer (matcher );
102
+ final HttpTestServer server = createServer (matcher , sslContext );
110
103
try {
111
104
final HttpClient .Builder builder = HttpClient .newBuilder ().sslContext (sslContext );
112
105
if (sniConfiguredOnClient ) {
@@ -156,10 +149,12 @@ void testRequestToIPLiteralHost(final boolean sniConfiguredOnClient) throws Exce
156
149
@ ParameterizedTest
157
150
@ ValueSource (booleans = {false , true })
158
151
void testRequestResolvedHostName (final boolean sniConfiguredOnClient ) throws Exception {
152
+ final SSLContext sslContext = new SimpleSSLContext ().get ();
153
+ assertNotNull (sslContext , "could not create a SSLContext" );
159
154
final String resolvedHostName = InetAddress .getLoopbackAddress ().getHostName ();
160
155
final String expectedSNI = resolvedHostName ;
161
156
final ServerNameMatcher matcher = new ServerNameMatcher (expectedSNI );
162
- final HttpTestServer server = createServer (matcher );
157
+ final HttpTestServer server = createServer (matcher , sslContext );
163
158
try {
164
159
final HttpClient .Builder builder = HttpClient .newBuilder ().sslContext (sslContext );
165
160
if (sniConfiguredOnClient ) {
@@ -190,7 +185,8 @@ void testRequestResolvedHostName(final boolean sniConfiguredOnClient) throws Exc
190
185
/*
191
186
* Creates a HttpsServer configured to use the given SNIMatcher
192
187
*/
193
- private static HttpTestServer createServer (final SNIMatcher matcher ) throws Exception {
188
+ private static HttpTestServer createServer (final SNIMatcher matcher ,
189
+ final SSLContext sslContext ) throws Exception {
194
190
final InetSocketAddress addr = new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 );
195
191
final int backlog = 0 ;
196
192
final HttpsServer httpsServer = HttpsServer .create (addr , backlog );
0 commit comments