test/jdk/java/net/httpclient/NoBodyPartOne.java
branchhttp-client-branch
changeset 55936 4182d9625130
parent 55876 42cf279e4a89
child 55942 8d4770c22b63
equal deleted inserted replaced
55925:81aa2f98ea52 55936:4182d9625130
       
     1 /*
       
     2  * Copyright (c) 2015, 2017, 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 /*
       
    25  * @test
       
    26  * @bug 8161157
       
    27  * @summary Test response body handlers/subscribers when there is no body
       
    28  * @library /lib/testlibrary http2/server
       
    29  * @build jdk.testlibrary.SimpleSSLContext
       
    30  * @modules java.base/sun.net.www.http
       
    31  *          jdk.incubator.httpclient/jdk.incubator.http.internal.common
       
    32  *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
       
    33  *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
       
    34  * @run testng/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=all NoBodyPartOne
       
    35  */
       
    36 
       
    37 import java.net.URI;
       
    38 import java.nio.file.Files;
       
    39 import java.nio.file.Path;
       
    40 import java.nio.file.Paths;
       
    41 import jdk.incubator.http.HttpClient;
       
    42 import jdk.incubator.http.HttpRequest;
       
    43 import jdk.incubator.http.HttpResponse;
       
    44 import jdk.incubator.http.HttpResponse.BodyHandler;
       
    45 import org.testng.annotations.Test;
       
    46 import static java.nio.charset.StandardCharsets.UTF_8;
       
    47 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
       
    48 import static jdk.incubator.http.HttpResponse.BodyHandler.asByteArray;
       
    49 import static jdk.incubator.http.HttpResponse.BodyHandler.asFile;
       
    50 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
       
    51 import static org.testng.Assert.assertEquals;
       
    52 import static org.testng.Assert.assertTrue;
       
    53 
       
    54 public class NoBodyPartOne extends AbstractNoBody {
       
    55 
       
    56     @Test(dataProvider = "variants")
       
    57     public void testAsString(String uri, boolean sameClient) throws Exception {
       
    58         HttpClient client = null;
       
    59         for (int i=0; i< ITERATION_COUNT; i++) {
       
    60             if (!sameClient || client == null)
       
    61                 client = newHttpClient();
       
    62 
       
    63             HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
       
    64                                          .PUT(fromString(SIMPLE_STRING))
       
    65                                          .build();
       
    66             BodyHandler<String> handler = i % 2 == 0 ? asString() : asString(UTF_8);
       
    67             HttpResponse<String> response = client.send(req, handler);
       
    68             String body = response.body();
       
    69             assertEquals(body, "");
       
    70         }
       
    71     }
       
    72 
       
    73     @Test(dataProvider = "variants")
       
    74     public void testAsFile(String uri, boolean sameClient) throws Exception {
       
    75         HttpClient client = null;
       
    76         for (int i=0; i< ITERATION_COUNT; i++) {
       
    77             if (!sameClient || client == null)
       
    78                 client = newHttpClient();
       
    79 
       
    80             HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
       
    81                                          .PUT(fromString(SIMPLE_STRING))
       
    82                                          .build();
       
    83             Path p = Paths.get("NoBody_testAsFile.txt");
       
    84             HttpResponse<Path> response = client.send(req, asFile(p));
       
    85             Path bodyPath = response.body();
       
    86             assertTrue(Files.exists(bodyPath));
       
    87             assertEquals(Files.size(bodyPath), 0);
       
    88         }
       
    89     }
       
    90 
       
    91     @Test(dataProvider = "variants")
       
    92     public void testAsByteArray(String uri, boolean sameClient) throws Exception {
       
    93         HttpClient client = null;
       
    94         for (int i=0; i< ITERATION_COUNT; i++) {
       
    95             if (!sameClient || client == null)
       
    96                 client = newHttpClient();
       
    97 
       
    98             HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
       
    99                                          .PUT(fromString(SIMPLE_STRING))
       
   100                                          .build();
       
   101             HttpResponse<byte[]> response = client.send(req, asByteArray());
       
   102             byte[] body = response.body();
       
   103             assertEquals(body.length, 0);
       
   104         }
       
   105     }
       
   106 }