Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test8.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 fixed len encoding41*/4243public class Test8 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 ("Test8: " );56HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();57urlc.setDoOutput (true);58urlc.setRequestMethod ("POST");59OutputStream os = new BufferedOutputStream (urlc.getOutputStream());60for (int i=0; i<SIZE; i++) {61os.write (i % 100);62}63os.close();64int resp = urlc.getResponseCode();65if (resp != 200) {66throw new RuntimeException ("test failed response code");67}68if (error) {69throw new RuntimeException ("test failed error");70}71delay();72server.stop(2);73executor.shutdown();74System.out.println ("OK");7576}7778public static boolean error = false;79final static int SIZE = 999999;8081static class Handler implements HttpHandler {82int invocation = 1;83public void handle (HttpExchange t)84throws IOException85{86InputStream is = t.getRequestBody();87Headers map = t.getRequestHeaders();88Headers rmap = t.getResponseHeaders();89int c, count=0;90while ((c=is.read ()) != -1) {91if (c != (count % 100)) {92error = true;93break;94}95count ++;96}97if (count != SIZE) {98error = true;99}100if (!t.getRequestMethod().equals ("POST")) {101error = true;102}103is.close();104t.sendResponseHeaders (200, -1);105t.close();106}107}108}109110111