55978
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
29 |
* @modules jdk.incubator.httpclient
|
|
30 |
* jdk.httpserver
|
|
31 |
* @run testng/othervm WSHandshakeExceptionTest
|
|
32 |
*/
|
|
33 |
import com.sun.net.httpserver.HttpServer;
|
|
34 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
35 |
import com.sun.net.httpserver.HttpsServer;
|
|
36 |
import jdk.incubator.http.HttpClient;
|
|
37 |
import jdk.incubator.http.WebSocket;
|
|
38 |
import jdk.incubator.http.WebSocketHandshakeException;
|
|
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;
|
|
48 |
import java.util.concurrent.ExecutorService;
|
|
49 |
import java.util.concurrent.Executors;
|
|
50 |
import static org.testng.Assert.assertEquals;
|
|
51 |
import static org.testng.Assert.assertNotNull;
|
|
52 |
import static org.testng.Assert.fail;
|
|
53 |
|
|
54 |
public class WSHandshakeExceptionTest {
|
|
55 |
|
|
56 |
SSLContext sslContext;
|
|
57 |
HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ]
|
|
58 |
HttpsServer httpsTestServer; // HTTPS/1.1
|
|
59 |
String httpURI;
|
|
60 |
String httpsURI;
|
|
61 |
|
|
62 |
static final int ITERATION_COUNT = 10;
|
|
63 |
// a shared executor helps reduce the amount of threads created by the test
|
|
64 |
static final ExecutorService executor = Executors.newCachedThreadPool();
|
|
65 |
|
|
66 |
@DataProvider(name = "variants")
|
|
67 |
public Object[][] variants() {
|
|
68 |
return new Object[][]{
|
|
69 |
{ httpURI, false },
|
|
70 |
{ httpsURI, false },
|
|
71 |
{ httpURI, true },
|
|
72 |
{ httpsURI, true },
|
|
73 |
};
|
|
74 |
}
|
|
75 |
|
|
76 |
HttpClient newHttpClient() {
|
|
77 |
return HttpClient.newBuilder()
|
|
78 |
.executor(executor)
|
|
79 |
.sslContext(sslContext)
|
|
80 |
.build();
|
|
81 |
}
|
|
82 |
|
|
83 |
@Test(dataProvider = "variants")
|
|
84 |
public void test(String uri, boolean sameClient) {
|
|
85 |
HttpClient client = null;
|
|
86 |
for (int i = 0; i < ITERATION_COUNT; i++) {
|
|
87 |
if (!sameClient || client == null)
|
|
88 |
client = newHttpClient();
|
|
89 |
|
|
90 |
try {
|
|
91 |
client.newWebSocketBuilder()
|
|
92 |
.buildAsync(URI.create(uri), new WebSocket.Listener() { })
|
|
93 |
.join();
|
|
94 |
fail("Expected to throw");
|
|
95 |
} catch (CompletionException ce) {
|
|
96 |
Throwable t = ce.getCause();
|
|
97 |
assertEquals(t.getClass(), WebSocketHandshakeException.class);
|
|
98 |
WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
|
|
99 |
assertNotNull(wse.getResponse());
|
|
100 |
assertEquals(wse.getResponse().statusCode(), 404);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
@BeforeTest
|
|
106 |
public void setup() throws Exception {
|
|
107 |
sslContext = new SimpleSSLContext().get();
|
|
108 |
if (sslContext == null)
|
|
109 |
throw new AssertionError("Unexpected null sslContext");
|
|
110 |
|
|
111 |
// HTTP/1.1
|
|
112 |
InetSocketAddress sa = new InetSocketAddress("localhost", 0);
|
|
113 |
httpTestServer = HttpServer.create(sa, 0);
|
|
114 |
httpURI = "ws://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/";
|
|
115 |
|
|
116 |
httpsTestServer = HttpsServer.create(sa, 0);
|
|
117 |
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
118 |
httpsURI = "wss://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/";
|
|
119 |
|
|
120 |
httpTestServer.start();
|
|
121 |
httpsTestServer.start();
|
|
122 |
}
|
|
123 |
|
|
124 |
@AfterTest
|
|
125 |
public void teardown() {
|
|
126 |
httpTestServer.stop(0);
|
|
127 |
httpsTestServer.stop(0);
|
|
128 |
executor.shutdownNow();
|
|
129 |
}
|
|
130 |
}
|