Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6431193.java
41154 views
/*1* Copyright (c) 2006, 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*/2223/**24* @test25* @bug 643119326* @library /test/lib27* @summary The new HTTP server exits immediately28* @run main B643119329* @run main/othervm -Djava.net.preferIPv6Addresses=true B643119330*/3132import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.net.*;36import jdk.test.lib.net.URIBuilder;3738import com.sun.net.httpserver.*;3940public class B6431193 {4142static boolean error = false;4344public static void read (InputStream i) throws IOException {45while (i.read() != -1);46i.close();47}4849/**50* @param args51*/52public static void main(String[] args) {53class MyHandler implements HttpHandler {54public void handle(HttpExchange t) throws IOException {55InputStream is = t.getRequestBody();56read(is);57// .. read the request body58String response = "This is the response";59t.sendResponseHeaders(200, response.length());60OutputStream os = t.getResponseBody();61os.write(response.getBytes());62os.close();63error = Thread.currentThread().isDaemon();64}65}666768HttpServer server;69try {70InetAddress loopback = InetAddress.getLoopbackAddress();71server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);7273server.createContext("/apps", new MyHandler());74server.setExecutor(null);75// creates a default executor76server.start();77int port = server.getAddress().getPort();78String s = "http://localhost:"+port+"/apps/foo";79URL url = URIBuilder.newBuilder()80.scheme("http")81.loopback()82.port(port)83.path("/apps/foo")84.toURL();85InputStream is = url.openConnection(Proxy.NO_PROXY).getInputStream();86read (is);87server.stop (1);88if (error) {89throw new RuntimeException ("error in test");90}9192}93catch (Exception e) {94throw new AssertionError("Unexpected exception: " + e, e);95}96}97}9899100