4646import jdk .httpclient .test .lib .common .ServerNameMatcher ;
4747import jdk .test .lib .net .SimpleSSLContext ;
4848import jdk .test .lib .net .URIBuilder ;
49- import org .junit .jupiter .api .BeforeAll ;
5049import org .junit .jupiter .params .ParameterizedTest ;
5150import org .junit .jupiter .params .provider .ValueSource ;
5251import static java .nio .charset .StandardCharsets .US_ASCII ;
7069public class HttpClientSNITest {
7170 private static final String RESP_BODY_TEXT = "hello world" ;
7271
73- private static SSLContext sslContext ;
74-
7572 private static final class Handler implements HttpTestHandler {
7673
7774 @ Override
@@ -85,12 +82,6 @@ public void handle(final HttpTestExchange exch) throws IOException {
8582 }
8683 }
8784
88- @ BeforeAll
89- static void beforeAll () throws Exception {
90- sslContext = new SimpleSSLContext ().get ();
91- assertNotNull (sslContext , "could not create a SSLContext" );
92- }
93-
9485 /*
9586 * Creates and configures a HTTPS server with a SNIMatcher that
9687 * expects a specific SNI name to be sent by the connection client.
@@ -104,9 +95,11 @@ static void beforeAll() throws Exception {
10495 @ ParameterizedTest
10596 @ ValueSource (booleans = {false , true })
10697 void testRequestToIPLiteralHost (final boolean sniConfiguredOnClient ) throws Exception {
98+ final SSLContext sslContext = new SimpleSSLContext ().get ();
99+ assertNotNull (sslContext , "could not create a SSLContext" );
107100 final String expectedSNI = "non-dns-resolvable.foo.bar.localhost" ;
108101 final ServerNameMatcher matcher = new ServerNameMatcher (expectedSNI );
109- final HttpTestServer server = createServer (matcher );
102+ final HttpTestServer server = createServer (matcher , sslContext );
110103 try {
111104 final HttpClient .Builder builder = HttpClient .newBuilder ().sslContext (sslContext );
112105 if (sniConfiguredOnClient ) {
@@ -156,10 +149,12 @@ void testRequestToIPLiteralHost(final boolean sniConfiguredOnClient) throws Exce
156149 @ ParameterizedTest
157150 @ ValueSource (booleans = {false , true })
158151 void testRequestResolvedHostName (final boolean sniConfiguredOnClient ) throws Exception {
152+ final SSLContext sslContext = new SimpleSSLContext ().get ();
153+ assertNotNull (sslContext , "could not create a SSLContext" );
159154 final String resolvedHostName = InetAddress .getLoopbackAddress ().getHostName ();
160155 final String expectedSNI = resolvedHostName ;
161156 final ServerNameMatcher matcher = new ServerNameMatcher (expectedSNI );
162- final HttpTestServer server = createServer (matcher );
157+ final HttpTestServer server = createServer (matcher , sslContext );
163158 try {
164159 final HttpClient .Builder builder = HttpClient .newBuilder ().sslContext (sslContext );
165160 if (sniConfiguredOnClient ) {
@@ -190,7 +185,8 @@ void testRequestResolvedHostName(final boolean sniConfiguredOnClient) throws Exc
190185 /*
191186 * Creates a HttpsServer configured to use the given SNIMatcher
192187 */
193- private static HttpTestServer createServer (final SNIMatcher matcher ) throws Exception {
188+ private static HttpTestServer createServer (final SNIMatcher matcher ,
189+ final SSLContext sslContext ) throws Exception {
194190 final InetSocketAddress addr = new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 );
195191 final int backlog = 0 ;
196192 final HttpsServer httpsServer = HttpsServer .create (addr , backlog );
0 commit comments