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 WSHandshakeException 32 */ 33 ; 34 import java.net.InetSocketAddress; 35 import java.net.URI; 36 import java.util.concurrent.CompletionException; 37 import java.util.concurrent.Executor; 38 import java.util.concurrent.Executors; 39 import com.sun.net.httpserver.HttpServer; 40 import com.sun.net.httpserver.HttpsConfigurator; 41 import com.sun.net.httpserver.HttpsServer; 42 import jdk.incubator.http.HttpClient; 43 import javax.net.ssl.SSLContext; 44 import jdk.incubator.http.WebSocket; 45 import jdk.incubator.http.WebSocketHandshakeException; 46 import jdk.testlibrary.SimpleSSLContext; 47 import org.testng.annotations.AfterTest; 48 import org.testng.annotations.BeforeTest; 49 import org.testng.annotations.DataProvider; 50 import org.testng.annotations.Test; 51 import static org.testng.Assert.assertEquals; 52 import static org.testng.Assert.fail; 53 import static org.testng.Assert.assertTrue; 54 55 public class WSHandshakeException { 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 64 static final int ITERATION_COUNT = 10; 65 // a shared executor helps reduce the amount of threads created by the test 66 static final Executor executor = Executors.newCachedThreadPool(); 67 68 @DataProvider(name = "variants") 69 public Object[][] variants() { 70 return new Object[][]{ 71 { httpURI, false }, 72 { httpsURI, false }, 73 { httpURI, true }, 74 { httpsURI, true }, 75 }; 76 } 77 78 HttpClient newHttpClient() { 79 return HttpClient.newBuilder() 80 .executor(executor) 81 .sslContext(sslContext) 82 .build(); 83 } 84 85 @Test(dataProvider = "variants") 86 public void test(String uri, boolean sameClient) throws Exception { 87 HttpClient client = null; 88 for (int i=0; i< ITERATION_COUNT; i++) { 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) { 98 Throwable t = ce.getCause(); 99 assertTrue(t instanceof WebSocketHandshakeException); 100 WebSocketHandshakeException wse = (WebSocketHandshakeException) t; 101 assertEquals(wse.getResponse().statusCode(), 404); 102 } 103 } 104 } 105 106 107 @BeforeTest 108 public void setup() throws Exception { 109 sslContext = new SimpleSSLContext().get(); 110 if (sslContext == null) 111 throw new AssertionError("Unexpected null sslContext"); 112 113 // HTTP/1.1 114 InetSocketAddress sa = new InetSocketAddress("localhost", 0); 115 httpTestServer = HttpServer.create(sa, 0); 116 httpURI = "ws://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/"; 117 118 httpsTestServer = HttpsServer.create(sa, 0); 119 httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); 120 httpsURI = "wss://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/"; 121 122 httpTestServer.start(); 123 httpsTestServer.start(); 124 } 125 126 @AfterTest 127 public void teardown() throws Exception { 128 httpTestServer.stop(0); 129 httpsTestServer.stop(0); 130 } 131 }