< prev index next >
   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 8087112
  27  * @library /lib/testlibrary
  28  * @build jdk.testlibrary.SimpleSSLContext
  29  * @modules java.httpclient
  30  * @compile/module=java.httpclient java/net/http/BodyOutputStream.java
  31  * @compile/module=java.httpclient java/net/http/BodyInputStream.java
  32  * @compile/module=java.httpclient java/net/http/EchoHandler.java
  33  * @compile/module=java.httpclient java/net/http/Http2Handler.java
  34  * @compile/module=java.httpclient java/net/http/Http2TestExchange.java
  35  * @compile/module=java.httpclient java/net/http/Http2TestServerConnection.java
  36  * @compile/module=java.httpclient java/net/http/Http2TestServer.java
  37  * @compile/module=java.httpclient java/net/http/OutgoingPushPromise.java
  38  * @compile/module=java.httpclient java/net/http/TestUtil.java
  39  * @run testng/othervm -Djava.net.http.HttpClient.log=ssl,requests,responses,errors BasicTest
  40  */
  41 
  42 import java.io.*;
  43 import java.net.*;
  44 import java.net.http.*;
  45 import static java.net.http.HttpClient.Version.HTTP_2;
  46 import javax.net.ssl.*;
  47 import java.nio.file.*;
  48 import java.util.concurrent.*;
  49 import jdk.testlibrary.SimpleSSLContext;
  50 
  51 
  52 import org.testng.annotations.Test;
  53 import org.testng.annotations.Parameters;
  54 
  55 @Test
  56 public class BasicTest {
  57     static int httpPort, httpsPort;
  58     static Http2TestServer httpServer, httpsServer;
  59     static HttpClient client = null;
  60     static ExecutorService exec;
  61     static SSLContext sslContext;
  62 
  63     static String httpURIString, httpsURIString;
  64 
  65     static void initialize() throws Exception {
  66         try {
  67             SimpleSSLContext sslct = new SimpleSSLContext();
  68             sslContext = sslct.get();
  69             client = getClient();
  70             exec = client.executorService();
  71             httpServer = new Http2TestServer(false, 0, new EchoHandler(),
  72                     exec, sslContext);
  73             httpPort = httpServer.getAddress().getPort();
  74 
  75             httpsServer = new Http2TestServer(true, 0, new EchoHandler(),
  76                     exec, sslContext);
  77 
  78             httpsPort = httpsServer.getAddress().getPort();
  79             httpURIString = "http://127.0.0.1:" + Integer.toString(httpPort) +
  80                     "/foo/";
  81             httpsURIString = "https://127.0.0.1:" + Integer.toString(httpsPort) +
  82                     "/bar/";
  83 
  84             httpServer.start();
  85             httpsServer.start();
  86         } catch (Throwable e) {
  87             System.err.println("Throwing now");
  88             e.printStackTrace();
  89             throw e;
  90         }
  91     }
  92 
  93     @Test(timeOut=30000)
  94     public static void test() throws Exception {
  95         try {
  96             initialize();
  97             simpleTest(false);
  98             simpleTest(true);
  99             Thread.sleep(1000 * 4);
 100         } finally {
 101             httpServer.stop();
 102             httpsServer.stop();
 103             exec.shutdownNow();
 104         }
 105     }
 106 
 107     static HttpClient getClient() {
 108         if (client == null) {
 109             client = HttpClient.create()
 110                 .sslContext(sslContext)
 111                 .version(HTTP_2)
 112                 .build();
 113         }
 114         return client;
 115     }
 116 
 117     static URI getURI(boolean secure) {
 118         if (secure)
 119             return URI.create(httpsURIString);
 120         else
 121             return URI.create(httpURIString);
 122     }
 123 
 124     static void checkStatus(int expected, int found) throws Exception {
 125         if (expected != found) {
 126             System.err.printf ("Test failed: wrong status code %d/%d\n",
 127                 expected, found);
 128             throw new RuntimeException("Test failed");
 129         }
 130     }
 131 
 132     static void checkStrings(String expected, String found) throws Exception {
 133         if (!expected.equals(found)) {
 134             System.err.printf ("Test failed: wrong string %s/%s\n",
 135                 expected, found);
 136             throw new RuntimeException("Test failed");
 137         }
 138     }
 139 
 140     static Void compareFiles(Path path1, Path path2) {
 141         return java.net.http.TestUtil.compareFiles(path1, path2);
 142     }
 143 
 144     static Path tempFile() {
 145         return java.net.http.TestUtil.tempFile();
 146     }
 147 
 148     static final String SIMPLE_STRING = "Hello world Goodbye world";
 149 
 150     static final int LOOPS = 13;
 151     static final int FILESIZE = 64 * 1024;
 152 
 153     static void simpleTest(boolean secure) throws Exception {
 154         URI uri = getURI(secure);
 155         System.err.println("Request to " + uri);
 156 
 157         // Do a simple warmup request
 158 
 159         HttpClient client = getClient();
 160         HttpRequest req = client.request(uri)
 161                 .body(HttpRequest.fromString(SIMPLE_STRING))
 162                 .POST();
 163         HttpResponse response = req.response();
 164         HttpHeaders h = response.headers();
 165 
 166         checkStatus(200, response.statusCode());
 167 
 168         String responseBody = response.body(HttpResponse.asString());
 169         checkStrings(SIMPLE_STRING, responseBody);
 170 
 171         checkStrings(h.firstValue("x-hello").get(), "world");
 172         checkStrings(h.firstValue("x-bye").get(), "universe");
 173 
 174         // Do loops asynchronously
 175 
 176         CompletableFuture[] responses = new CompletableFuture[LOOPS];
 177         final Path source = java.net.http.TestUtil.getAFile(FILESIZE);
 178         for (int i = 0; i < LOOPS; i++) {
 179             responses[i] = client.request(uri)
 180                 .body(HttpRequest.fromFile(source))
 181                 .version(HTTP_2)
 182                 .POST()
 183                 .responseAsync()
 184                 .thenCompose(r -> r.bodyAsync(HttpResponse.asFile(tempFile())))
 185                 .thenApply(path -> compareFiles(path, source));
 186             Thread.sleep(100);
 187         }
 188         CompletableFuture.allOf(responses).join();
 189         System.out.println("DONE");
 190     }
 191 }
< prev index next >