author | chegar |
Thu, 08 Mar 2018 17:42:16 +0000 | |
branch | http-client-branch |
changeset 56265 | ec34ae013fbe |
parent 56256 | 0fe17c3f9b4f |
child 56324 | 3edf200fff01 |
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; |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56256
diff
changeset
|
36 |
|
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56256
diff
changeset
|
37 |
import java.net.InetAddress; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
38 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
39 |
import java.net.http.WebSocket; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55978
diff
changeset
|
40 |
import java.net.http.WebSocketHandshakeException; |
55978 | 41 |
import jdk.testlibrary.SimpleSSLContext; |
42 |
import org.testng.annotations.AfterTest; |
|
43 |
import org.testng.annotations.BeforeTest; |
|
44 |
import org.testng.annotations.DataProvider; |
|
45 |
import org.testng.annotations.Test; |
|
46 |
import javax.net.ssl.SSLContext; |
|
47 |
import java.net.InetSocketAddress; |
|
48 |
import java.net.URI; |
|
49 |
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
|
50 |
import java.util.concurrent.ExecutionException; |
55978 | 51 |
import java.util.concurrent.ExecutorService; |
52 |
import java.util.concurrent.Executors; |
|
53 |
import static org.testng.Assert.assertEquals; |
|
54 |
import static org.testng.Assert.assertNotNull; |
|
55 |
import static org.testng.Assert.fail; |
|
56 |
||
57 |
public class WSHandshakeExceptionTest { |
|
58 |
||
59 |
SSLContext sslContext; |
|
60 |
HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ] |
|
61 |
HttpsServer httpsTestServer; // HTTPS/1.1 |
|
62 |
String httpURI; |
|
63 |
String httpsURI; |
|
64 |
||
65 |
static final int ITERATION_COUNT = 10; |
|
66 |
// a shared executor helps reduce the amount of threads created by the test |
|
67 |
static final ExecutorService executor = Executors.newCachedThreadPool(); |
|
68 |
||
69 |
@DataProvider(name = "variants") |
|
70 |
public Object[][] variants() { |
|
71 |
return new Object[][]{ |
|
72 |
{ httpURI, false }, |
|
73 |
{ httpsURI, false }, |
|
74 |
{ httpURI, true }, |
|
75 |
{ httpsURI, true }, |
|
76 |
}; |
|
77 |
} |
|
78 |
||
79 |
HttpClient newHttpClient() { |
|
80 |
return HttpClient.newBuilder() |
|
81 |
.executor(executor) |
|
82 |
.sslContext(sslContext) |
|
83 |
.build(); |
|
84 |
} |
|
85 |
||
86 |
@Test(dataProvider = "variants") |
|
87 |
public void test(String uri, boolean sameClient) { |
|
88 |
HttpClient client = null; |
|
89 |
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
|
90 |
System.out.printf("iteration %s%n", i); |
55978 | 91 |
if (!sameClient || client == null) |
92 |
client = newHttpClient(); |
|
93 |
||
94 |
try { |
|
95 |
client.newWebSocketBuilder() |
|
96 |
.buildAsync(URI.create(uri), new WebSocket.Listener() { }) |
|
97 |
.join(); |
|
98 |
fail("Expected to throw"); |
|
99 |
} catch (CompletionException ce) { |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
100 |
Throwable t = getCompletionCause(ce); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
101 |
if (!(t instanceof WebSocketHandshakeException)) { |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
102 |
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
|
103 |
} |
55978 | 104 |
WebSocketHandshakeException wse = (WebSocketHandshakeException) t; |
105 |
assertNotNull(wse.getResponse()); |
|
106 |
assertEquals(wse.getResponse().statusCode(), 404); |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
@BeforeTest |
|
112 |
public void setup() throws Exception { |
|
113 |
sslContext = new SimpleSSLContext().get(); |
|
114 |
if (sslContext == null) |
|
115 |
throw new AssertionError("Unexpected null sslContext"); |
|
116 |
||
117 |
// HTTP/1.1 |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56256
diff
changeset
|
118 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
55978 | 119 |
httpTestServer = HttpServer.create(sa, 0); |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56256
diff
changeset
|
120 |
httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/"; |
55978 | 121 |
|
122 |
httpsTestServer = HttpsServer.create(sa, 0); |
|
123 |
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56256
diff
changeset
|
124 |
httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/"; |
55978 | 125 |
|
126 |
httpTestServer.start(); |
|
127 |
httpsTestServer.start(); |
|
128 |
} |
|
129 |
||
130 |
@AfterTest |
|
131 |
public void teardown() { |
|
132 |
httpTestServer.stop(0); |
|
133 |
httpsTestServer.stop(0); |
|
134 |
executor.shutdownNow(); |
|
135 |
} |
|
56256
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
136 |
|
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
137 |
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
|
138 |
if (!(x instanceof CompletionException) |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
139 |
&& !(x instanceof ExecutionException)) return x; |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
140 |
final Throwable cause = x.getCause(); |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
141 |
if (cause == null) { |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
142 |
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
|
143 |
} |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
144 |
return cause; |
0fe17c3f9b4f
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents:
56089
diff
changeset
|
145 |
} |
55978 | 146 |
} |