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