jdk/test/java/net/httpclient/SplitResponse.java
changeset 36131 379db4b2f95d
child 37816 b06d70715a79
equal deleted inserted replaced
36130:5423259ef9ea 36131:379db4b2f95d
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 //package javaapplication16;
       
    25 
       
    26 import java.io.IOException;
       
    27 import java.net.http.HttpClient;
       
    28 import java.net.http.HttpRequest;
       
    29 import java.net.http.HttpResponse;
       
    30 import java.net.URI;
       
    31 import java.util.concurrent.CompletableFuture;
       
    32 
       
    33 /**
       
    34  * @test
       
    35  * @bug 8087112
       
    36  * @build Server
       
    37  * @run main/othervm -Djava.net.HttpClient.log=all SplitResponse
       
    38  */
       
    39 
       
    40 /**
       
    41  * Similar test to QuickResponses except that each byte of the response
       
    42  * is sent in a separate packet, which tests the stability of the implementation
       
    43  * for receiving unusual packet sizes.
       
    44  */
       
    45 public class SplitResponse {
       
    46 
       
    47     static Server server;
       
    48 
       
    49     static String response(String body) {
       
    50         return "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-length: "
       
    51                 + Integer.toString(body.length())
       
    52                 + "\r\n\r\n" + body;
       
    53     }
       
    54 
       
    55     static final String responses[] = {
       
    56         "Lorem ipsum",
       
    57         "dolor sit amet",
       
    58         "consectetur adipiscing elit, sed do eiusmod tempor",
       
    59         "quis nostrud exercitation ullamco",
       
    60         "laboris nisi",
       
    61         "ut",
       
    62         "aliquip ex ea commodo consequat." +
       
    63         "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
       
    64         "cillum dolore eu fugiat nulla pariatur.",
       
    65         "Excepteur sint occaecat cupidatat non proident."
       
    66     };
       
    67 
       
    68     public static void main(String[] args) throws Exception {
       
    69         server = new Server(0);
       
    70         URI uri = new URI(server.getURL());
       
    71 
       
    72         HttpRequest request;
       
    73         HttpResponse r;
       
    74         CompletableFuture<HttpResponse> cf1;
       
    75 
       
    76         for (int i=0; i<responses.length; i++) {
       
    77             cf1 = HttpRequest.create(uri)
       
    78                     .GET()
       
    79                     .responseAsync();
       
    80             String body = responses[i];
       
    81 
       
    82             Server.Connection c = server.activity();
       
    83             sendSplitResponse(response(body), c);
       
    84             r = cf1.get();
       
    85             if (r.statusCode()!= 200)
       
    86                 throw new RuntimeException("Failed");
       
    87 
       
    88             String rxbody = r.body(HttpResponse.asString());
       
    89             System.out.println("received " + rxbody);
       
    90             if (!rxbody.equals(body))
       
    91                 throw new RuntimeException("Failed");
       
    92             c.close();
       
    93         }
       
    94         HttpClient.getDefault().executorService().shutdownNow();
       
    95         System.out.println("OK");
       
    96     }
       
    97 
       
    98     // send the response one byte at a time with a small delay between bytes
       
    99     // to ensure that each byte is read in a separate read
       
   100     static void sendSplitResponse(String s, Server.Connection conn) {
       
   101         System.out.println("Sending: ");
       
   102         Thread t = new Thread(() -> {
       
   103             try {
       
   104                 int len = s.length();
       
   105                 for (int i = 0; i < len; i++) {
       
   106                     String onechar = s.substring(i, i + 1);
       
   107                     conn.send(onechar);
       
   108                     Thread.sleep(30);
       
   109                 }
       
   110                 System.out.println("sent");
       
   111             } catch (IOException | InterruptedException e) {
       
   112             }
       
   113         });
       
   114         t.setDaemon(true);
       
   115         t.start();
       
   116     }
       
   117 }