51 import org.testng.Assert; |
51 import org.testng.Assert; |
52 import org.testng.annotations.AfterTest; |
52 import org.testng.annotations.AfterTest; |
53 import org.testng.annotations.BeforeTest; |
53 import org.testng.annotations.BeforeTest; |
54 import org.testng.annotations.DataProvider; |
54 import org.testng.annotations.DataProvider; |
55 import org.testng.annotations.Test; |
55 import org.testng.annotations.Test; |
|
56 |
|
57 import static java.net.http.HttpClient.Builder.NO_PROXY; |
56 import static java.net.http.HttpClient.Version.HTTP_1_1; |
58 import static java.net.http.HttpClient.Version.HTTP_1_1; |
57 import static java.net.http.HttpClient.Version.HTTP_2; |
59 import static java.net.http.HttpClient.Version.HTTP_2; |
58 |
60 |
59 |
61 |
60 public class InvalidSSLContextTest { |
62 public class InvalidSSLContextTest { |
73 |
75 |
74 @Test(dataProvider = "versions") |
76 @Test(dataProvider = "versions") |
75 public void testSync(Version version) throws Exception { |
77 public void testSync(Version version) throws Exception { |
76 // client-side uses a different context to that of the server-side |
78 // client-side uses a different context to that of the server-side |
77 HttpClient client = HttpClient.newBuilder() |
79 HttpClient client = HttpClient.newBuilder() |
|
80 .proxy(NO_PROXY) |
78 .sslContext(SSLContext.getDefault()) |
81 .sslContext(SSLContext.getDefault()) |
79 .build(); |
82 .build(); |
80 |
83 |
81 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
84 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
82 .version(version) |
85 .version(version) |
83 .build(); |
86 .build(); |
84 |
87 |
85 try { |
88 try { |
86 HttpResponse<?> response = client.send(request, BodyHandlers.discarding()); |
89 HttpResponse<?> response = client.send(request, BodyHandlers.discarding()); |
87 Assert.fail("UNEXPECTED response" + response); |
90 Assert.fail("UNEXPECTED response" + response); |
88 } catch (SSLException sslex) { |
91 } catch (IOException ex) { |
89 System.out.println("Caught expected: " + sslex); |
92 System.out.println("Caught expected: " + ex); |
|
93 assertExceptionOrCause(SSLException.class, ex); |
90 } |
94 } |
91 } |
95 } |
92 |
96 |
93 @Test(dataProvider = "versions") |
97 @Test(dataProvider = "versions") |
94 public void testAsync(Version version) throws Exception { |
98 public void testAsync(Version version) throws Exception { |
95 // client-side uses a different context to that of the server-side |
99 // client-side uses a different context to that of the server-side |
96 HttpClient client = HttpClient.newBuilder() |
100 HttpClient client = HttpClient.newBuilder() |
|
101 .proxy(NO_PROXY) |
97 .sslContext(SSLContext.getDefault()) |
102 .sslContext(SSLContext.getDefault()) |
98 .build(); |
103 .build(); |
99 |
104 |
100 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
105 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
101 .version(version) |
106 .version(version) |
115 if (error instanceof CompletionException) { |
120 if (error instanceof CompletionException) { |
116 Throwable cause = error.getCause(); |
121 Throwable cause = error.getCause(); |
117 if (cause == null) { |
122 if (cause == null) { |
118 Assert.fail("Unexpected null cause: " + error); |
123 Assert.fail("Unexpected null cause: " + error); |
119 } |
124 } |
120 assertException(clazz, cause); |
125 assertExceptionOrCause(clazz, cause); |
121 } else { |
126 } else { |
122 assertException(clazz, error); |
127 assertExceptionOrCause(clazz, error); |
123 } |
128 } |
124 return null; |
129 return null; |
125 }).join(); |
130 }).join(); |
126 } |
131 } |
127 |
132 |
128 static void assertException(Class<? extends Throwable> clazz, Throwable t) { |
133 static void assertExceptionOrCause(Class<? extends Throwable> clazz, Throwable t) { |
129 if (t == null) { |
134 if (t == null) { |
130 Assert.fail("Expected " + clazz + ", caught nothing"); |
135 Assert.fail("Expected " + clazz + ", caught nothing"); |
131 } |
136 } |
132 if (!clazz.isInstance(t)) { |
137 do { |
133 Assert.fail("Expected " + clazz + ", caught " + t); |
138 if (clazz.isInstance(t)) { |
134 } |
139 return; // found |
|
140 } |
|
141 } while ((t = t.getCause()) != null); |
|
142 t.printStackTrace(System.out); |
|
143 Assert.fail("Expected " + clazz + "in " + t); |
135 } |
144 } |
136 |
145 |
137 @BeforeTest |
146 @BeforeTest |
138 public void setup() throws Exception { |
147 public void setup() throws Exception { |
139 sslContext = new SimpleSSLContext().get(); |
148 sslContext = new SimpleSSLContext().get(); |