Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/B7025238.java
38868 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.net.httpserver.*;24import java.io.IOException;25import java.io.OutputStream;26import java.net.*;27import java.util.concurrent.Executors;2829/*30* @test31* @bug 702523832* @summary HttpURLConnection does not handle URLs with an empty path component33*/34public class B7025238 {3536public static void main(String[] args) throws Exception {37new B7025238().runTest();38}3940public void runTest() throws Exception {41Server s = null;42try {43s = new Server();44s.startServer();45URL url = new URL("http://localhost:" + s.getPort() + "?q=test");46HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();47urlConnection.setRequestMethod("GET");48urlConnection.connect();49int code = urlConnection.getResponseCode();5051if (code != 200) {52throw new RuntimeException("Test failed!");53}54} finally {55s.stopServer();56}57}5859class Server {60HttpServer server;6162public void startServer() {63InetSocketAddress addr = new InetSocketAddress(0);64try {65server = HttpServer.create(addr, 0);66} catch (IOException ioe) {67throw new RuntimeException("Server could not be created");68}6970server.createContext("/", new EmptyPathHandler());71server.start();72}7374public int getPort() {75return server.getAddress().getPort();76}7778public void stopServer() {79server.stop(0);80}81}8283class EmptyPathHandler implements HttpHandler {8485@Override86public void handle(HttpExchange exchange) throws IOException {87String requestMethod = exchange.getRequestMethod();8889if (requestMethod.equalsIgnoreCase("GET")) {90Headers responseHeaders = exchange.getResponseHeaders();91responseHeaders.set("Content-Type", "text/plain");92exchange.sendResponseHeaders(200, 0);93OutputStream os = exchange.getResponseBody();94String str = "Hello from server!";95os.write(str.getBytes());96os.flush();97os.close();98}99}100}101}102103104105