Path: blob/master/test/jdk/sun/net/www/http/ChunkedInputStream/ChunkedEncodingTest.java
41154 views
/*1* Copyright (c) 2004, 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 433392026* @modules jdk.httpserver27* @library /test/lib28* @run main ChunkedEncodingTest29* @summary ChunkedEncodingTest unit test30*/3132import java.io.*;33import java.net.*;34import java.security.*;35import com.sun.net.httpserver.HttpServer;36import com.sun.net.httpserver.HttpHandler;37import com.sun.net.httpserver.HttpExchange;38import static java.lang.System.out;39import jdk.test.lib.net.URIBuilder;4041public class ChunkedEncodingTest{42private static MessageDigest serverDigest, clientDigest;43private static volatile byte[] serverMac, clientMac;4445static void client(String u) throws Exception {46URL url = new URL(u);47out.println("client opening connection to: " + u);48URLConnection urlc = url.openConnection();49DigestInputStream dis =50new DigestInputStream(urlc.getInputStream(), clientDigest);51while (dis.read() != -1);52clientMac = dis.getMessageDigest().digest();53dis.close();54}5556public static void test() {57HttpServer server = null;58try {59serverDigest = MessageDigest.getInstance("MD5");60clientDigest = MessageDigest.getInstance("MD5");61server = startHttpServer();6263int port = server.getAddress().getPort();64out.println ("Server listening on port: " + port);65String url = URIBuilder.newBuilder()66.scheme("http")67.host(server.getAddress().getAddress())68.port(port)69.path("/chunked/")70.build().toString();71client(url);7273if (!MessageDigest.isEqual(clientMac, serverMac)) {74throw new RuntimeException(75"Data received is NOT equal to the data sent");76}77} catch (Exception e) {78throw new RuntimeException(e);79} finally {80if (server != null)81server.stop(0);82}83}8485public static void main(String[] args) {86test();87}8889/**90* Http Server91*/92static HttpServer startHttpServer() throws IOException {93InetAddress loopback = InetAddress.getLoopbackAddress();94HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);95HttpHandler httpHandler = new SimpleHandler();96httpServer.createContext("/chunked/", httpHandler);97httpServer.start();98return httpServer;99}100101static class SimpleHandler implements HttpHandler {102static byte[] baMessage;103final static int CHUNK_SIZE = 8 * 1024;104final static int MESSAGE_LENGTH = 52 * CHUNK_SIZE;105106static {107baMessage = new byte[MESSAGE_LENGTH];108for (int i=0; i<MESSAGE_LENGTH; i++)109baMessage[i] = (byte)i;110}111112@Override113public void handle(HttpExchange t) throws IOException {114InputStream is = t.getRequestBody();115while (is.read() != -1);116is.close();117118t.sendResponseHeaders (200, 0);119OutputStream os = t.getResponseBody();120DigestOutputStream dos = new DigestOutputStream(os, serverDigest);121122int offset = 0;123for (int i=0; i<52; i++) {124dos.write(baMessage, offset, CHUNK_SIZE);125offset += CHUNK_SIZE;126}127serverMac = serverDigest.digest();128os.close();129t.close();130}131}132}133134135