Path: blob/master/test/jdk/com/sun/net/httpserver/Test7a.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* @library /test/lib27* @build jdk.test.lib.net.SimpleSSLContext jdk.test.lib.net.URIBuilder28* @run main/othervm Test7a29* @run main/othervm -Djava.net.preferIPv6Addresses=true Test7a30* @summary Light weight HTTP server31*/3233import com.sun.net.httpserver.*;3435import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import javax.net.ssl.*;39import jdk.test.lib.net.SimpleSSLContext;40import jdk.test.lib.net.URIBuilder;4142/**43* Test POST large file via chunked encoding (large chunks)44*/4546public class Test7a extends Test {4748public static void main (String[] args) throws Exception {49//Logger log = Logger.getLogger ("com.sun.net.httpserver");50//log.setLevel (Level.FINE);51//ConsoleHandler h = new ConsoleHandler();52//h.setLevel (Level.ALL);53//log.addHandler (h);54Handler handler = new Handler();55InetAddress loopback = InetAddress.getLoopbackAddress();56InetSocketAddress addr = new InetSocketAddress(loopback, 0);57HttpsServer server = HttpsServer.create (addr, 0);58HttpContext ctx = server.createContext ("/test", handler);59ExecutorService executor = Executors.newCachedThreadPool();60SSLContext ssl = new SimpleSSLContext().get();61server.setHttpsConfigurator(new HttpsConfigurator (ssl));62server.setExecutor (executor);63server.start ();6465URL url = URIBuilder.newBuilder()66.scheme("https")67.loopback()68.port(server.getAddress().getPort())69.path("/test/foo.html")70.toURL();7172System.out.print ("Test7a: " );73HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);74urlc.setDoOutput (true);75urlc.setRequestMethod ("POST");76urlc.setChunkedStreamingMode (16 * 1024); // big chunks77urlc.setHostnameVerifier (new DummyVerifier());78urlc.setSSLSocketFactory (ssl.getSocketFactory());79OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);80for (int i=0; i<SIZE; i++) {81os.write (i % 100);82}83os.close();84int resp = urlc.getResponseCode();85if (resp != 200) {86throw new RuntimeException ("test failed response code");87}88if (error) {89throw new RuntimeException ("test failed error");90}91delay();92server.stop(2);93executor.shutdown();94System.out.println ("OK");9596}9798public static boolean error = false;99final static int SIZE = 999999;100101static class Handler implements HttpHandler {102int invocation = 1;103public void handle (HttpExchange t)104throws IOException105{106InputStream is = t.getRequestBody();107Headers map = t.getRequestHeaders();108Headers rmap = t.getResponseHeaders();109int c, count=0;110while ((c=is.read ()) != -1) {111if (c != (count % 100)) {112error = true;113break;114}115count ++;116}117if (count != SIZE) {118error = true;119}120is.close();121t.sendResponseHeaders (200, -1);122t.close();123}124}125}126127128