Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test6.java
38855 views
/*1* Copyright (c) 2005, 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*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import java.security.*;36import javax.security.auth.callback.*;37import javax.net.ssl.*;3839/**40* Test POST large file via chunked encoding (unusually small chunks)41*/4243public class Test6 extends Test {4445public static void main (String[] args) throws Exception {46Handler handler = new Handler();47InetSocketAddress addr = new InetSocketAddress (0);48HttpServer server = HttpServer.create (addr, 0);49HttpContext ctx = server.createContext ("/test", handler);50ExecutorService executor = Executors.newCachedThreadPool();51server.setExecutor (executor);52server.start ();5354URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");55System.out.print ("Test6: " );56HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();57urlc.setDoOutput (true);58urlc.setRequestMethod ("POST");59urlc.setChunkedStreamingMode (32); // small chunks60OutputStream os = new BufferedOutputStream (urlc.getOutputStream());61for (int i=0; i<SIZE; i++) {62os.write (i % 100);63}64os.close();65int resp = urlc.getResponseCode();66if (resp != 200) {67throw new RuntimeException ("test failed response code");68}69if (error) {70throw new RuntimeException ("test failed error");71}72delay();73server.stop(2);74executor.shutdown();75System.out.println ("OK");7677}7879public static boolean error = false;80final static int SIZE = 999999;8182static class Handler implements HttpHandler {83int invocation = 1;84public void handle (HttpExchange t)85throws IOException86{87InputStream is = t.getRequestBody();88Headers map = t.getRequestHeaders();89Headers rmap = t.getResponseHeaders();90int c, count=0;91while ((c=is.read ()) != -1) {92if (c != (count % 100)) {93error = true;94break;95}96count ++;97}98if (count != SIZE) {99error = true;100}101is.close();102t.sendResponseHeaders (200, -1);103t.close();104}105}106}107108109