author | prappo |
Wed, 07 Mar 2018 13:00:11 +0000 | |
branch | http-client-branch |
changeset 56256 | 0fe17c3f9b4f |
parent 56089 | 42208b2f224e |
child 56265 | ec34ae013fbe |
permissions | -rw-r--r-- |
55978 | 1 |
/* |
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
55978 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Basic test for WebSocketHandshakeException |
|
27 |
* @library /lib/testlibrary |
|
28 |
* @build jdk.testlibrary.SimpleSSLContext |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
29 |
* @modules java.net.http |
55978 | 30 |
* jdk.httpserver |
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
31 |
* @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest |
55978 | 32 |
*/ |
33 |
import com.sun.net.httpserver.HttpServer; |
|
34 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
35 |
import com.sun.net.httpserver.HttpsServer; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
36 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
37 |
import java.net.http.WebSocket; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
38 |
import java.net.http.WebSocketHandshakeException; |
55978 | 39 |
import jdk.testlibrary.SimpleSSLContext; |
40 |
import org.testng.annotations.AfterTest; |
|
41 |
import org.testng.annotations.BeforeTest; |
|
42 |
import org.testng.annotations.DataProvider; |
|
43 |
import org.testng.annotations.Test; |
|
44 |
import javax.net.ssl.SSLContext; |
|
45 |
import java.net.InetSocketAddress; |
|
46 |
import java.net.URI; |
|
47 |
import java.util.concurrent.CompletionException; |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
48 |
import java.util.concurrent.ExecutionException; |
55978 | 49 |
import java.util.concurrent.ExecutorService; |
50 |
import java.util.concurrent.Executors; |
|
51 |
import static org.testng.Assert.assertEquals; |
|
52 |
import static org.testng.Assert.assertNotNull; |
|
53 |
import static org.testng.Assert.fail; |
|
54 |
||
55 |
public class WSHandshakeExceptionTest { |
|
56 |
||
57 |
SSLContext sslContext; |
|
58 |
HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ] |
|
59 |
HttpsServer httpsTestServer; // HTTPS/1.1 |
|
60 |
String httpURI; |
|
61 |
String httpsURI; |
|
62 |
||
63 |
static final int ITERATION_COUNT = 10; |
|
64 |
// a shared executor helps reduce the amount of threads created by the test |
|
65 |
static final ExecutorService executor = Executors.newCachedThreadPool(); |
|
66 |
||
67 |
@DataProvider(name = "variants") |
|
68 |
public Object[][] variants() { |
|
69 |
return new Object[][]{ |
|
70 |
{ httpURI, false }, |
|
71 |
{ httpsURI, false }, |
|
72 |
{ httpURI, true }, |
|
73 |
{ httpsURI, true }, |
|
74 |
}; |
|
75 |
} |
|
76 |
||
77 |
HttpClient newHttpClient() { |
|
78 |
return HttpClient.newBuilder() |
|
79 |
.executor(executor) |
|
80 |
.sslContext(sslContext) |
|
81 |
.build(); |
|
82 |
} |
|
83 |
||
84 |
@Test(dataProvider = "variants") |
|
85 |
public void test(String uri, boolean sameClient) { |
|
86 |
HttpClient client = null; |
|
87 |
for (int i = 0; i < ITERATION_COUNT; i++) { |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
88 |
System.out.printf("iteration %s%n", i); |
55978 | 89 |
if (!sameClient || client == null) |
90 |
client = newHttpClient(); |
|
91 |
||
92 |
try { |
|
93 |
client.newWebSocketBuilder() |
|
94 |
.buildAsync(URI.create(uri), new WebSocket.Listener() { }) |
|
95 |
.join(); |
|
96 |
fail("Expected to throw"); |
|
97 |
} catch (CompletionException ce) { |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
98 |
Throwable t = getCompletionCause(ce); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
99 |
if (!(t instanceof WebSocketHandshakeException)) { |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
100 |
throw new AssertionError("Unexpected exception", t); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
101 |
} |
55978 | 102 |
WebSocketHandshakeException wse = (WebSocketHandshakeException) t; |
103 |
assertNotNull(wse.getResponse()); |
|
104 |
assertEquals(wse.getResponse().statusCode(), 404); |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
@BeforeTest |
|
110 |
public void setup() throws Exception { |
|
111 |
sslContext = new SimpleSSLContext().get(); |
|
112 |
if (sslContext == null) |
|
113 |
throw new AssertionError("Unexpected null sslContext"); |
|
114 |
||
115 |
// HTTP/1.1 |
|
116 |
InetSocketAddress sa = new InetSocketAddress("localhost", 0); |
|
117 |
httpTestServer = HttpServer.create(sa, 0); |
|
118 |
httpURI = "ws://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/"; |
|
119 |
||
120 |
httpsTestServer = HttpsServer.create(sa, 0); |
|
121 |
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
122 |
httpsURI = "wss://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/"; |
|
123 |
||
124 |
httpTestServer.start(); |
|
125 |
httpsTestServer.start(); |
|
126 |
} |
|
127 |
||
128 |
@AfterTest |
|
129 |
public void teardown() { |
|
130 |
httpTestServer.stop(0); |
|
131 |
httpsTestServer.stop(0); |
|
132 |
executor.shutdownNow(); |
|
133 |
} |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
134 |
|
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
135 |
private static Throwable getCompletionCause(Throwable x) { |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
136 |
if (!(x instanceof CompletionException) |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
137 |
&& !(x instanceof ExecutionException)) return x; |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
138 |
final Throwable cause = x.getCause(); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
139 |
if (cause == null) { |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
140 |
throw new InternalError("Unexpected null cause", x); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
141 |
} |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
142 |
return cause; |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
143 |
} |
55978 | 144 |
} |