Test7.java revision 0
187N/A/*
1636N/A * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
187N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
187N/A *
187N/A * This code is free software; you can redistribute it and/or modify it
187N/A * under the terms of the GNU General Public License version 2 only, as
187N/A * published by the Free Software Foundation.
187N/A *
187N/A * This code is distributed in the hope that it will be useful, but WITHOUT
187N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
187N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
187N/A * version 2 for more details (a copy is included in the LICENSE file that
187N/A * accompanied this code).
187N/A *
187N/A * You should have received a copy of the GNU General Public License version
187N/A * 2 along with this work; if not, write to the Free Software Foundation,
187N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
187N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1472N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1472N/A * have any questions.
187N/A */
187N/A
187N/A/**
187N/A * @test
187N/A * @bug 6270015
187N/A * @summary Light weight HTTP server
187N/A */
187N/A
187N/Aimport com.sun.net.httpserver.*;
187N/A
187N/Aimport java.util.*;
187N/Aimport java.util.concurrent.*;
187N/Aimport java.io.*;
187N/Aimport java.net.*;
187N/Aimport java.security.*;
187N/Aimport javax.security.auth.callback.*;
187N/Aimport javax.net.ssl.*;
187N/A
187N/A/**
187N/A * Test POST large file via chunked encoding (large chunks)
187N/A */
187N/A
187N/Apublic class Test7 extends Test {
187N/A
187N/A public static void main (String[] args) throws Exception {
187N/A Handler handler = new Handler();
187N/A InetSocketAddress addr = new InetSocketAddress (0);
187N/A HttpServer server = HttpServer.create (addr, 0);
187N/A HttpContext ctx = server.createContext ("/test", handler);
187N/A ExecutorService executor = Executors.newCachedThreadPool();
187N/A server.setExecutor (executor);
187N/A server.start ();
187N/A
187N/A URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
187N/A System.out.print ("Test7: " );
187N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
187N/A urlc.setDoOutput (true);
187N/A urlc.setRequestMethod ("POST");
187N/A urlc.setChunkedStreamingMode (16 * 1024); // big chunks
187N/A OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
187N/A for (int i=0; i<SIZE; i++) {
187N/A os.write (i % 100);
187N/A }
187N/A os.close();
187N/A int resp = urlc.getResponseCode();
187N/A if (resp != 200) {
187N/A throw new RuntimeException ("test failed response code");
187N/A }
187N/A if (error) {
187N/A throw new RuntimeException ("test failed error");
187N/A }
187N/A delay();
187N/A server.stop(2);
187N/A executor.shutdown();
187N/A System.out.println ("OK");
187N/A
187N/A }
187N/A
187N/A public static boolean error = false;
187N/A final static int SIZE = 999999;
187N/A
187N/A static class Handler implements HttpHandler {
187N/A int invocation = 1;
187N/A public void handle (HttpExchange t)
187N/A throws IOException
187N/A {
263N/A InputStream is = t.getRequestBody();
263N/A Headers map = t.getRequestHeaders();
187N/A Headers rmap = t.getResponseHeaders();
187N/A int c, count=0;
187N/A while ((c=is.read ()) != -1) {
187N/A if (c != (count % 100)) {
187N/A error = true;
187N/A break;
187N/A }
187N/A count ++;
187N/A }
187N/A if (count != SIZE) {
187N/A error = true;
187N/A }
187N/A is.close();
187N/A t.sendResponseHeaders (200, -1);
187N/A t.close();
187N/A }
187N/A }
187N/A}
187N/A