1 /* |
1 /* |
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
26 * @summary Basic test for WebSocketHandshakeException |
26 * @summary Basic test for WebSocketHandshakeException |
27 * @library /lib/testlibrary |
27 * @library /lib/testlibrary |
28 * @build jdk.testlibrary.SimpleSSLContext |
28 * @build jdk.testlibrary.SimpleSSLContext |
29 * @modules java.net.http |
29 * @modules java.net.http |
30 * jdk.httpserver |
30 * jdk.httpserver |
31 * @run testng/othervm WSHandshakeExceptionTest |
31 * @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest |
32 */ |
32 */ |
33 import com.sun.net.httpserver.HttpServer; |
33 import com.sun.net.httpserver.HttpServer; |
34 import com.sun.net.httpserver.HttpsConfigurator; |
34 import com.sun.net.httpserver.HttpsConfigurator; |
35 import com.sun.net.httpserver.HttpsServer; |
35 import com.sun.net.httpserver.HttpsServer; |
36 import java.net.http.HttpClient; |
36 import java.net.http.HttpClient; |
43 import org.testng.annotations.Test; |
43 import org.testng.annotations.Test; |
44 import javax.net.ssl.SSLContext; |
44 import javax.net.ssl.SSLContext; |
45 import java.net.InetSocketAddress; |
45 import java.net.InetSocketAddress; |
46 import java.net.URI; |
46 import java.net.URI; |
47 import java.util.concurrent.CompletionException; |
47 import java.util.concurrent.CompletionException; |
|
48 import java.util.concurrent.ExecutionException; |
48 import java.util.concurrent.ExecutorService; |
49 import java.util.concurrent.ExecutorService; |
49 import java.util.concurrent.Executors; |
50 import java.util.concurrent.Executors; |
50 import static org.testng.Assert.assertEquals; |
51 import static org.testng.Assert.assertEquals; |
51 import static org.testng.Assert.assertNotNull; |
52 import static org.testng.Assert.assertNotNull; |
52 import static org.testng.Assert.fail; |
53 import static org.testng.Assert.fail; |
82 |
83 |
83 @Test(dataProvider = "variants") |
84 @Test(dataProvider = "variants") |
84 public void test(String uri, boolean sameClient) { |
85 public void test(String uri, boolean sameClient) { |
85 HttpClient client = null; |
86 HttpClient client = null; |
86 for (int i = 0; i < ITERATION_COUNT; i++) { |
87 for (int i = 0; i < ITERATION_COUNT; i++) { |
|
88 System.out.printf("iteration %s%n", i); |
87 if (!sameClient || client == null) |
89 if (!sameClient || client == null) |
88 client = newHttpClient(); |
90 client = newHttpClient(); |
89 |
91 |
90 try { |
92 try { |
91 client.newWebSocketBuilder() |
93 client.newWebSocketBuilder() |
92 .buildAsync(URI.create(uri), new WebSocket.Listener() { }) |
94 .buildAsync(URI.create(uri), new WebSocket.Listener() { }) |
93 .join(); |
95 .join(); |
94 fail("Expected to throw"); |
96 fail("Expected to throw"); |
95 } catch (CompletionException ce) { |
97 } catch (CompletionException ce) { |
96 Throwable t = ce.getCause(); |
98 Throwable t = getCompletionCause(ce); |
97 assertEquals(t.getClass(), WebSocketHandshakeException.class); |
99 if (!(t instanceof WebSocketHandshakeException)) { |
|
100 throw new AssertionError("Unexpected exception", t); |
|
101 } |
98 WebSocketHandshakeException wse = (WebSocketHandshakeException) t; |
102 WebSocketHandshakeException wse = (WebSocketHandshakeException) t; |
99 assertNotNull(wse.getResponse()); |
103 assertNotNull(wse.getResponse()); |
100 assertEquals(wse.getResponse().statusCode(), 404); |
104 assertEquals(wse.getResponse().statusCode(), 404); |
101 } |
105 } |
102 } |
106 } |
125 public void teardown() { |
129 public void teardown() { |
126 httpTestServer.stop(0); |
130 httpTestServer.stop(0); |
127 httpsTestServer.stop(0); |
131 httpsTestServer.stop(0); |
128 executor.shutdownNow(); |
132 executor.shutdownNow(); |
129 } |
133 } |
|
134 |
|
135 private static Throwable getCompletionCause(Throwable x) { |
|
136 if (!(x instanceof CompletionException) |
|
137 && !(x instanceof ExecutionException)) return x; |
|
138 final Throwable cause = x.getCause(); |
|
139 if (cause == null) { |
|
140 throw new InternalError("Unexpected null cause", x); |
|
141 } |
|
142 return cause; |
|
143 } |
130 } |
144 } |