jdk/test/java/net/httpclient/http2/RedirectTest.java
changeset 42460 7133f144981a
child 45535 4b19310ae4ee
equal deleted inserted replaced
42459:1ad58e0cbf16 42460:7133f144981a
       
     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 8156514
       
    27  * @key intermittent
       
    28  * @library /lib/testlibrary server
       
    29  * @build jdk.testlibrary.SimpleSSLContext
       
    30  * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
       
    31  * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.frame
       
    32  * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
       
    33  * @run testng/othervm -Djdk.httpclient.HttpClient.log=frames,ssl,requests,responses,errors RedirectTest
       
    34  */
       
    35 
       
    36 import java.net.*;
       
    37 import jdk.incubator.http.*;
       
    38 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
       
    39 import java.nio.file.*;
       
    40 import java.util.concurrent.*;
       
    41 import java.util.function.*;
       
    42 import java.util.Arrays;
       
    43 import java.util.Iterator;
       
    44 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
       
    45 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
       
    46 
       
    47 import org.testng.annotations.Test;
       
    48 
       
    49 @Test
       
    50 public class RedirectTest {
       
    51     static int httpPort, altPort;
       
    52     static Http2TestServer httpServer, altServer;
       
    53     static HttpClient client;
       
    54     static ExecutorService exec;
       
    55 
       
    56     static String httpURIString, altURIString1, altURIString2;
       
    57 
       
    58     static Supplier<String> sup(String... args) {
       
    59         Iterator<String> i = Arrays.asList(args).iterator();
       
    60         // need to know when to stop calling it.
       
    61         return () -> i.next();
       
    62     }
       
    63 
       
    64     static void initialize() throws Exception {
       
    65         try {
       
    66             client = getClient();
       
    67             httpServer = new Http2TestServer(false, 0, exec, null);
       
    68 
       
    69             httpPort = httpServer.getAddress().getPort();
       
    70             altServer = new Http2TestServer(false, 0, exec, null);
       
    71             altPort = altServer.getAddress().getPort();
       
    72 
       
    73             // urls are accessed in sequence below
       
    74             // first two on different servers. Third on same server
       
    75             // as second. So, the client should use the same http connection
       
    76             httpURIString = "http://127.0.0.1:" + httpPort + "/foo/";
       
    77             altURIString1 = "http://127.0.0.1:" + altPort + "/redir";
       
    78             altURIString2 = "http://127.0.0.1:" + altPort + "/redir/again";
       
    79 
       
    80             httpServer.addHandler(new RedirectHandler(sup(altURIString1)), "/foo");
       
    81             altServer.addHandler(new RedirectHandler(sup(altURIString2)), "/redir");
       
    82             altServer.addHandler(new EchoHandler(), "/redir/again");
       
    83 
       
    84             httpServer.start();
       
    85             altServer.start();
       
    86         } catch (Throwable e) {
       
    87             System.err.println("Throwing now");
       
    88             e.printStackTrace();
       
    89             throw e;
       
    90         }
       
    91     }
       
    92 
       
    93     @Test(timeOut=3000000)
       
    94     public static void test() throws Exception {
       
    95         try {
       
    96             initialize();
       
    97             simpleTest();
       
    98         } catch (Throwable tt) {
       
    99             System.err.println("tt caught");
       
   100             tt.printStackTrace();
       
   101         } finally {
       
   102             httpServer.stop();
       
   103             altServer.stop();
       
   104             exec.shutdownNow();
       
   105         }
       
   106     }
       
   107 
       
   108     static HttpClient getClient() {
       
   109         if (client == null) {
       
   110             exec = Executors.newCachedThreadPool();
       
   111             client = HttpClient.newBuilder()
       
   112                                .executor(exec)
       
   113                                .followRedirects(HttpClient.Redirect.ALWAYS)
       
   114                                .version(HTTP_2)
       
   115                                .build();
       
   116         }
       
   117         return client;
       
   118     }
       
   119 
       
   120     static URI getURI() {
       
   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 TestUtil.compareFiles(path1, path2);
       
   142     }
       
   143 
       
   144     static Path tempFile() {
       
   145         return TestUtil.tempFile();
       
   146     }
       
   147 
       
   148     static final String SIMPLE_STRING = "Hello world Goodbye world";
       
   149 
       
   150     static final int FILESIZE = 64 * 1024 + 200;
       
   151 
       
   152     static void simpleTest() throws Exception {
       
   153         URI uri = getURI();
       
   154         System.err.println("Request to " + uri);
       
   155 
       
   156         HttpClient client = getClient();
       
   157         HttpRequest req = HttpRequest.newBuilder(uri)
       
   158                                      .POST(fromString(SIMPLE_STRING))
       
   159                                      .build();
       
   160         CompletableFuture<HttpResponse<String>> cf = client.sendAsync(req, asString());
       
   161         HttpResponse<String> response = cf.join();
       
   162 
       
   163         checkStatus(200, response.statusCode());
       
   164         String responseBody = response.body();
       
   165         checkStrings(SIMPLE_STRING, responseBody);
       
   166 
       
   167         System.err.println("DONE");
       
   168         Thread.sleep (6000);
       
   169     }
       
   170 }