Path: blob/master/test/jdk/com/sun/net/httpserver/Test8a.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 Test8a29* @run main/othervm -Djava.net.preferIPv6Addresses=true Test8a30* @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 fixed len encoding44*/4546public class Test8a extends Test {4748public static void main (String[] args) throws Exception {49//Logger log = Logger.getLogger ("com.sun.net.httpserver");50//ConsoleHandler h = new ConsoleHandler();51//h.setLevel (Level.INFO);52//log.addHandler (h);53//log.setLevel (Level.INFO);54HttpsServer server = null;55ExecutorService executor = null;56try {57Handler handler = new Handler();58InetAddress loopback = InetAddress.getLoopbackAddress();59InetSocketAddress addr = new InetSocketAddress(loopback, 0);60server = HttpsServer.create (addr, 0);61HttpContext ctx = server.createContext ("/test", handler);62executor = Executors.newCachedThreadPool();63SSLContext ssl = new SimpleSSLContext().get();64server.setHttpsConfigurator(new HttpsConfigurator (ssl));65server.setExecutor (executor);66server.start ();6768URL url = URIBuilder.newBuilder()69.scheme("https")70.loopback()71.port(server.getAddress().getPort())72.path("/test/foo.html")73.toURL();7475System.out.print ("Test8a: " );76HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);77urlc.setDoOutput (true);78urlc.setRequestMethod ("POST");79urlc.setHostnameVerifier (new DummyVerifier());80urlc.setSSLSocketFactory (ssl.getSocketFactory());81OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);82for (int i=0; i<SIZE; i++) {83os.write (i % 250);84}85os.close();86int resp = urlc.getResponseCode();87if (resp != 200) {88throw new RuntimeException ("test failed response code");89}90InputStream is = urlc.getInputStream ();91for (int i=0; i<SIZE; i++) {92int f = is.read();93if (f != (i % 250)) {94System.out.println ("Setting error(" +f +")("+i+")" );95error = true;96break;97}98}99is.close();100} finally {101delay();102if (server != null) server.stop(2);103if (executor != null) executor.shutdown();104}105if (error) {106throw new RuntimeException ("test failed error");107}108System.out.println ("OK");109110}111112public static boolean error = false;113//final static int SIZE = 999999;114final static int SIZE = 9999;115116static class Handler implements HttpHandler {117int invocation = 1;118public void handle (HttpExchange t)119throws IOException120{121System.out.println ("Handler.handle");122InputStream is = t.getRequestBody();123Headers map = t.getRequestHeaders();124Headers rmap = t.getResponseHeaders();125int c, count=0;126while ((c=is.read ()) != -1) {127if (c != (count % 250)) {128System.out.println ("Setting error 1");129error = true;130break;131}132count ++;133}134if (count != SIZE) {135System.out.println ("Setting error 2");136error = true;137}138is.close();139t.sendResponseHeaders (200, SIZE);140System.out.println ("Sending 200 OK");141OutputStream os = new BufferedOutputStream(t.getResponseBody(), 8000);142for (int i=0; i<SIZE; i++) {143os.write (i % 250);144}145os.close();146System.out.println ("Finished");147}148}149}150151152