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 /*
  25  * @test
  26  * @bug 8161157
  27  * @library /lib/testlibrary server
  28  * @build jdk.testlibrary.SimpleSSLContext
  29  * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
  30  *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
  31  *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
  32  * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,frames,errors NoBody
  33  */
  34 
  35 import java.io.IOException;
  36 import java.net.URI;
  37 import jdk.incubator.http.HttpClient;
  38 import jdk.incubator.http.HttpRequest;
  39 import jdk.incubator.http.HttpResponse;
  40 import javax.net.ssl.SSLContext;
  41 import javax.net.ssl.SSLParameters;
  42 import java.util.concurrent.Executors;
  43 import java.util.concurrent.ExecutorService;
  44 import jdk.testlibrary.SimpleSSLContext;
  45 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
  46 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
  47 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
  48 
  49 import org.testng.annotations.Test;
  50 
  51 public class NoBody {
  52 
  53     static final String SIMPLE_STRING = "Hello world. Goodbye world";
  54 
  55     @Test(timeOut=60000)
  56     public void test() throws Exception {
  57         SSLContext sslContext = (new SimpleSSLContext()).get();
  58         ExecutorService exec = Executors.newCachedThreadPool();
  59         HttpClient client = HttpClient.newBuilder()
  60                                       .executor(exec)
  61                                       .sslContext(sslContext)
  62                                       .version(HTTP_2)
  63                                       .build();
  64 
  65         Http2TestServer httpsServer = null;
  66         try {
  67             httpsServer = new Http2TestServer(true,
  68                                               0,
  69                                               exec,
  70                                               sslContext);
  71             httpsServer.addHandler(new NoBodyHandler(), "/");
  72 
  73             int httpsPort = httpsServer.getAddress().getPort();
  74             String httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";
  75 
  76             httpsServer.start();
  77             URI uri = URI.create(httpsURIString);
  78             System.err.println("Request to " + uri);
  79 
  80             HttpRequest req = HttpRequest.newBuilder(uri)
  81                                          .PUT(fromString(SIMPLE_STRING))
  82                                          .build();
  83             HttpResponse<String> response = client.send(req, asString());
  84             String body = response.body();
  85             if (!body.equals(""))
  86                 throw new RuntimeException("expected empty body");
  87             System.err.println("DONE");
  88         } finally {
  89             if (httpsServer != null )  { httpsServer.stop(); }
  90             exec.shutdownNow();
  91         }
  92     }
  93 }