B7025238.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2013, 2016, 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 24import com.sun.net.httpserver.*; 25import java.io.IOException; 26import java.io.OutputStream; 27import java.net.*; 28import java.util.concurrent.Executors; 29 30/* 31 * @test 32 * @bug 7025238 33 * @modules jdk.httpserver 34 * @summary HttpURLConnection does not handle URLs with an empty path component 35 */ 36public class B7025238 { 37 38 public static void main(String[] args) throws Exception { 39 new B7025238().runTest(); 40 } 41 42 public void runTest() throws Exception { 43 Server s = null; 44 try { 45 s = new Server(); 46 s.startServer(); 47 URL url = new URL("http://localhost:" + s.getPort() + "?q=test"); 48 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 49 urlConnection.setRequestMethod("GET"); 50 urlConnection.connect(); 51 int code = urlConnection.getResponseCode(); 52 53 if (code != 200) { 54 throw new RuntimeException("Test failed!"); 55 } 56 } finally { 57 s.stopServer(); 58 } 59 } 60 61 class Server { 62 HttpServer server; 63 64 public void startServer() { 65 InetSocketAddress addr = new InetSocketAddress(0); 66 try { 67 server = HttpServer.create(addr, 0); 68 } catch (IOException ioe) { 69 throw new RuntimeException("Server could not be created"); 70 } 71 72 server.createContext("/", new EmptyPathHandler()); 73 server.start(); 74 } 75 76 public int getPort() { 77 return server.getAddress().getPort(); 78 } 79 80 public void stopServer() { 81 server.stop(0); 82 } 83 } 84 85 class EmptyPathHandler implements HttpHandler { 86 87 @Override 88 public void handle(HttpExchange exchange) throws IOException { 89 String requestMethod = exchange.getRequestMethod(); 90 91 if (requestMethod.equalsIgnoreCase("GET")) { 92 Headers responseHeaders = exchange.getResponseHeaders(); 93 responseHeaders.set("Content-Type", "text/plain"); 94 exchange.sendResponseHeaders(200, 0); 95 OutputStream os = exchange.getResponseBody(); 96 String str = "Hello from server!"; 97 os.write(str.getBytes()); 98 os.flush(); 99 os.close(); 100 } 101 } 102 } 103} 104 105