Path: blob/master/test/jdk/com/sun/net/httpserver/Test11.java
41152 views
/*1* Copyright (c) 2005, 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 627001526* @summary Light weight HTTP server27* @library /test/lib28* @run main Test1129* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1130*/3132import java.net.*;33import java.util.concurrent.*;34import java.io.*;35import com.sun.net.httpserver.*;36import jdk.test.lib.net.URIBuilder;3738public class Test11 {39static class Handler implements HttpHandler {40public void handle(HttpExchange t) throws IOException {41read (t.getRequestBody());42String response = "response";43t.sendResponseHeaders (200, response.length());44OutputStream os = t.getResponseBody();45os.write (response.getBytes ("ISO8859_1"));46t.close();47}4849void read (InputStream is ) throws IOException {50byte[] b = new byte [8096];51while (is.read (b) != -1) {}52}53}5455public static void main (String[] args) throws Exception {56System.out.print ("Test 11: ");57InetAddress loopback = InetAddress.getLoopbackAddress();58HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);59ExecutorService s = Executors.newCachedThreadPool();60try {61HttpContext ctx = server.createContext (62"/foo/bar/", new Handler ()63);64s = Executors.newCachedThreadPool();65server.start ();66URL url = URIBuilder.newBuilder()67.scheme("http")68.loopback()69.port(server.getAddress().getPort())70.path("/Foo/bar/test.html")71.toURL();72HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);73int r = urlc.getResponseCode();74if (r == 200) {75throw new RuntimeException ("wrong response received");76}77System.out.println ("OK");78} finally {79s.shutdown();80server.stop(2);81}82}83}848586