Path: blob/master/test/jdk/com/sun/net/httpserver/Test6a.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.SimpleSSLContext28* @run main/othervm Test6a29* @run main/othervm -Djava.net.preferIPv6Addresses=true Test6a30* @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;4041import jdk.test.lib.net.URIBuilder;4243/**44* Test https POST large file via chunked encoding (unusually small chunks)45*/4647public class Test6a extends Test {4849public static void main (String[] args) throws Exception {50Handler handler = new Handler();51InetAddress loopback = InetAddress.getLoopbackAddress();52InetSocketAddress addr = new InetSocketAddress (loopback, 0);53HttpsServer server = HttpsServer.create (addr, 0);54HttpContext ctx = server.createContext ("/test", handler);55ExecutorService executor = Executors.newCachedThreadPool();56SSLContext ssl = new SimpleSSLContext().get();57server.setExecutor (executor);58server.setHttpsConfigurator(new HttpsConfigurator (ssl));59server.start ();6061URL url = URIBuilder.newBuilder()62.scheme("https")63.host(server.getAddress().getAddress())64.port(server.getAddress().getPort())65.path("/test/foo.html")66.toURL();6768System.out.println("Test6a: URL=" + url);69System.out.print("Test6a: ");70HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);71urlc.setDoOutput (true);72urlc.setRequestMethod ("POST");73urlc.setChunkedStreamingMode (32); // small chunks74urlc.setSSLSocketFactory (ssl.getSocketFactory());75urlc.setHostnameVerifier (new DummyVerifier());76OutputStream os = new BufferedOutputStream (urlc.getOutputStream());77for (int i=0; i<SIZE; i++) {78os.write (i % 100);79}80os.close();81int resp = urlc.getResponseCode();82if (resp != 200) {83throw new RuntimeException ("test failed response code");84}85if (error) {86throw new RuntimeException ("test failed error");87}88delay();89server.stop(2);90executor.shutdown();91System.out.println ("OK");9293}9495public static boolean error = false;96final static int SIZE = 999999;9798static class Handler implements HttpHandler {99int invocation = 1;100public void handle (HttpExchange t)101throws IOException102{103InputStream is = t.getRequestBody();104Headers map = t.getRequestHeaders();105Headers rmap = t.getResponseHeaders();106int c, count=0;107while ((c=is.read ()) != -1) {108if (c != (count % 100)) {109error = true;110break;111}112count ++;113}114if (count != SIZE) {115error = true;116}117is.close();118t.sendResponseHeaders (200, -1);119t.close();120}121}122}123124125