author | chegar |
Thu, 26 Apr 2018 14:42:36 +0100 | |
branch | http-client-branch |
changeset 56486 | b664a887e277 |
parent 56451 | 9585061fdb04 |
child 56507 | 2294c51eae30 |
permissions | -rw-r--r-- |
49765 | 1 |
/* |
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. |
|
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 java.net.http |
|
30 |
* jdk.httpserver |
|
31 |
* @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest |
|
32 |
*/ |
|
33 |
||
34 |
import com.sun.net.httpserver.HttpServer; |
|
35 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
36 |
import com.sun.net.httpserver.HttpsServer; |
|
37 |
||
38 |
import java.net.InetAddress; |
|
39 |
import java.net.http.HttpClient; |
|
40 |
import java.net.http.WebSocket; |
|
41 |
import java.net.http.WebSocketHandshakeException; |
|
42 |
import jdk.testlibrary.SimpleSSLContext; |
|
43 |
import org.testng.annotations.AfterTest; |
|
44 |
import org.testng.annotations.BeforeTest; |
|
45 |
import org.testng.annotations.DataProvider; |
|
46 |
import org.testng.annotations.Test; |
|
47 |
import javax.net.ssl.SSLContext; |
|
48 |
import java.net.InetSocketAddress; |
|
49 |
import java.net.URI; |
|
50 |
import java.util.concurrent.CompletionException; |
|
51 |
import java.util.concurrent.ExecutionException; |
|
52 |
import java.util.concurrent.ExecutorService; |
|
53 |
import java.util.concurrent.Executors; |
|
56486
b664a887e277
http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents:
56451
diff
changeset
|
54 |
|
b664a887e277
http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents:
56451
diff
changeset
|
55 |
import static java.net.http.HttpClient.Builder.NO_PROXY; |
49765 | 56 |
import static org.testng.Assert.assertEquals; |
57 |
import static org.testng.Assert.assertNotNull; |
|
58 |
import static org.testng.Assert.fail; |
|
59 |
||
60 |
public class WSHandshakeExceptionTest { |
|
61 |
||
62 |
SSLContext sslContext; |
|
63 |
HttpServer httpTestServer; // HTTP/1.1 [ 2 servers ] |
|
64 |
HttpsServer httpsTestServer; // HTTPS/1.1 |
|
65 |
String httpURI; |
|
66 |
String httpsURI; |
|
67 |
||
68 |
static final int ITERATION_COUNT = 10; |
|
69 |
// a shared executor helps reduce the amount of threads created by the test |
|
70 |
static final ExecutorService executor = Executors.newCachedThreadPool(); |
|
71 |
||
72 |
@DataProvider(name = "variants") |
|
73 |
public Object[][] variants() { |
|
74 |
return new Object[][]{ |
|
75 |
{ httpURI, false }, |
|
76 |
{ httpsURI, false }, |
|
77 |
{ httpURI, true }, |
|
78 |
{ httpsURI, true }, |
|
79 |
}; |
|
80 |
} |
|
81 |
||
82 |
HttpClient newHttpClient() { |
|
83 |
return HttpClient.newBuilder() |
|
56486
b664a887e277
http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents:
56451
diff
changeset
|
84 |
.proxy(NO_PROXY) |
49765 | 85 |
.executor(executor) |
86 |
.sslContext(sslContext) |
|
87 |
.build(); |
|
88 |
} |
|
89 |
||
90 |
@Test(dataProvider = "variants") |
|
91 |
public void test(String uri, boolean sameClient) { |
|
92 |
HttpClient client = null; |
|
93 |
for (int i = 0; i < ITERATION_COUNT; i++) { |
|
94 |
System.out.printf("iteration %s%n", i); |
|
95 |
if (!sameClient || client == null) |
|
96 |
client = newHttpClient(); |
|
97 |
||
98 |
try { |
|
99 |
client.newWebSocketBuilder() |
|
100 |
.buildAsync(URI.create(uri), new WebSocket.Listener() { }) |
|
101 |
.join(); |
|
102 |
fail("Expected to throw"); |
|
103 |
} catch (CompletionException ce) { |
|
104 |
Throwable t = getCompletionCause(ce); |
|
105 |
if (!(t instanceof WebSocketHandshakeException)) { |
|
106 |
throw new AssertionError("Unexpected exception", t); |
|
107 |
} |
|
108 |
WebSocketHandshakeException wse = (WebSocketHandshakeException) t; |
|
109 |
assertNotNull(wse.getResponse()); |
|
110 |
assertEquals(wse.getResponse().statusCode(), 404); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
@BeforeTest |
|
116 |
public void setup() throws Exception { |
|
117 |
sslContext = new SimpleSSLContext().get(); |
|
118 |
if (sslContext == null) |
|
119 |
throw new AssertionError("Unexpected null sslContext"); |
|
120 |
||
121 |
// HTTP/1.1 |
|
122 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
|
123 |
httpTestServer = HttpServer.create(sa, 0); |
|
124 |
httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/"; |
|
125 |
||
126 |
httpsTestServer = HttpsServer.create(sa, 0); |
|
127 |
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
128 |
httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/"; |
|
129 |
||
130 |
httpTestServer.start(); |
|
131 |
httpsTestServer.start(); |
|
132 |
} |
|
133 |
||
134 |
@AfterTest |
|
135 |
public void teardown() { |
|
136 |
httpTestServer.stop(0); |
|
137 |
httpsTestServer.stop(0); |
|
138 |
executor.shutdownNow(); |
|
139 |
} |
|
140 |
||
141 |
private static Throwable getCompletionCause(Throwable x) { |
|
142 |
if (!(x instanceof CompletionException) |
|
143 |
&& !(x instanceof ExecutionException)) return x; |
|
144 |
final Throwable cause = x.getCause(); |
|
145 |
if (cause == null) { |
|
146 |
throw new InternalError("Unexpected null cause", x); |
|
147 |
} |
|
148 |
return cause; |
|
149 |
} |
|
150 |
} |