Path: blob/master/test/jdk/com/sun/net/httpserver/Test8.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* @library /test/lib27* @summary Light weight HTTP server28* @run main Test829* @run main/othervm -Djava.net.preferIPv6Addresses=true Test830*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import java.security.*;39import javax.security.auth.callback.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243/**44* Test POST large file via fixed len encoding45*/4647public class Test8 extends Test {4849public static void main (String[] args) throws Exception {50Handler handler = new Handler();51InetAddress loopback = InetAddress.getLoopbackAddress();52InetSocketAddress addr = new InetSocketAddress(loopback, 0);53HttpServer server = HttpServer.create (addr, 0);54HttpContext ctx = server.createContext ("/test", handler);55ExecutorService executor = Executors.newCachedThreadPool();56server.setExecutor (executor);57server.start ();5859URL url = URIBuilder.newBuilder()60.scheme("http")61.loopback()62.port(server.getAddress().getPort())63.path("/test/foo.html")64.toURL();6566System.out.print ("Test8: " );67HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);68urlc.setDoOutput (true);69urlc.setRequestMethod ("POST");70OutputStream os = new BufferedOutputStream (urlc.getOutputStream());71for (int i=0; i<SIZE; i++) {72os.write (i % 100);73}74os.close();75int resp = urlc.getResponseCode();76if (resp != 200) {77throw new RuntimeException ("test failed response code");78}79if (error) {80throw new RuntimeException ("test failed error");81}82delay();83server.stop(2);84executor.shutdown();85System.out.println ("OK");8687}8889public static boolean error = false;90final static int SIZE = 999999;9192static class Handler implements HttpHandler {93int invocation = 1;94public void handle (HttpExchange t)95throws IOException96{97InputStream is = t.getRequestBody();98Headers map = t.getRequestHeaders();99Headers rmap = t.getResponseHeaders();100int c, count=0;101while ((c=is.read ()) != -1) {102if (c != (count % 100)) {103error = true;104break;105}106count ++;107}108if (count != SIZE) {109error = true;110}111if (!t.getRequestMethod().equals ("POST")) {112error = true;113}114is.close();115t.sendResponseHeaders (200, -1);116t.close();117}118}119}120121122