Path: blob/master/test/jdk/com/sun/net/httpserver/Test6.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 Test629* @run main/othervm -Djava.net.preferIPv6Addresses=true Test630*/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 chunked encoding (unusually small chunks)45*/4647public class Test6 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 ("Test6: " );67HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);68urlc.setDoOutput (true);69urlc.setRequestMethod ("POST");70urlc.setChunkedStreamingMode (32); // small chunks71OutputStream os = new BufferedOutputStream (urlc.getOutputStream());72for (int i=0; i<SIZE; i++) {73os.write (i % 100);74}75os.close();76int resp = urlc.getResponseCode();77if (resp != 200) {78throw new RuntimeException ("test failed response code");79}80if (error) {81throw new RuntimeException ("test failed error");82}83delay();84server.stop(2);85executor.shutdown();86System.out.println ("OK");8788}8990public static boolean error = false;91final static int SIZE = 999999;9293static class Handler implements HttpHandler {94int invocation = 1;95public void handle (HttpExchange t)96throws IOException97{98InputStream is = t.getRequestBody();99Headers map = t.getRequestHeaders();100Headers rmap = t.getResponseHeaders();101int c, count=0;102while ((c=is.read ()) != -1) {103if (c != (count % 100)) {104error = true;105break;106}107count ++;108}109if (count != SIZE) {110error = true;111}112is.close();113t.sendResponseHeaders (200, -1);114t.close();115}116}117}118119120