Path: blob/master/test/jdk/sun/net/www/http/HttpClient/B7025238.java
41154 views
/*1* Copyright (c) 2013, 2019, 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;28import jdk.test.lib.net.URIBuilder;2930/*31* @test32* @bug 702523833* @modules jdk.httpserver34* @library /test/lib35* @summary HttpURLConnection does not handle URLs with an empty path component36*/37public class B7025238 {3839public static void main(String[] args) throws Exception {40new B7025238().runTest();41}4243public void runTest() throws Exception {44Server s = null;45try {46s = new Server();47s.startServer();48URL url = URIBuilder.newBuilder()49.scheme("http")50.loopback()51.port(s.getPort())52.query("q=test")53.toURL();54System.out.println("Connecting to: " + url);55HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();56urlConnection.setRequestMethod("GET");57urlConnection.connect();58int code = urlConnection.getResponseCode();5960if (code != 200) {61throw new RuntimeException("Test failed!");62}63} finally {64s.stopServer();65}66}6768class Server {69HttpServer server;7071public void startServer() {72InetAddress loopback = InetAddress.getLoopbackAddress();73InetSocketAddress addr = new InetSocketAddress(loopback, 0);74try {75server = HttpServer.create(addr, 0);76} catch (IOException ioe) {77throw new RuntimeException("Server could not be created");78}7980server.createContext("/", new EmptyPathHandler());81server.start();82}8384public int getPort() {85return server.getAddress().getPort();86}8788public void stopServer() {89server.stop(0);90}91}9293class EmptyPathHandler implements HttpHandler {9495@Override96public void handle(HttpExchange exchange) throws IOException {97String requestMethod = exchange.getRequestMethod();9899if (requestMethod.equalsIgnoreCase("GET")) {100Headers responseHeaders = exchange.getResponseHeaders();101responseHeaders.set("Content-Type", "text/plain");102exchange.sendResponseHeaders(200, 0);103OutputStream os = exchange.getResponseBody();104String str = "Hello from server!";105os.write(str.getBytes());106os.flush();107os.close();108}109}110}111}112113114