1 /*
   2  * Copyright (c) 2015, 2018, 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 import java.io.IOException;
  25 import java.net.InetAddress;
  26 import java.net.InetSocketAddress;
  27 import java.net.ServerSocket;
  28 import java.net.URI;
  29 import java.net.http.HttpClient;
  30 import java.net.http.HttpRequest;
  31 import java.net.http.HttpResponse;
  32 import java.net.http.HttpResponse.BodyHandlers;
  33 import java.net.http.HttpTimeoutException;
  34 import jdk.testlibrary.SimpleSSLContext;
  35 
  36 import javax.net.ServerSocketFactory;
  37 import javax.net.ssl.SSLContext;
  38 import javax.net.ssl.SSLServerSocketFactory;
  39 import java.time.Duration;
  40 import java.util.Arrays;
  41 import java.util.List;
  42 import java.util.concurrent.CompletionException;
  43 import java.util.function.Function;
  44 
  45 import static java.lang.System.out;
  46 
  47 /**
  48  * @test
  49  * @library /lib/testlibrary
  50  * @build jdk.testlibrary.SimpleSSLContext
  51  * @summary Basic tests for response timeouts
  52  * @run main/othervm TimeoutBasic
  53  */
  54 
  55 public class TimeoutBasic {
  56 
  57     static List<Duration> TIMEOUTS = List.of(Duration.ofSeconds(1),
  58                                              Duration.ofMillis(100),
  59                                              Duration.ofNanos(99),
  60                                              Duration.ofNanos(1));
  61 
  62     static final List<Function<HttpRequest.Builder, HttpRequest.Builder>> METHODS =
  63             Arrays.asList(HttpRequest.Builder::GET,
  64                           TimeoutBasic::DELETE,
  65                           TimeoutBasic::PUT,
  66                           TimeoutBasic::POST,
  67                           null);
  68 
  69     static final List<HttpClient.Version> VERSIONS =
  70             Arrays.asList(HttpClient.Version.HTTP_2, HttpClient.Version.HTTP_1_1, null);
  71 
  72     static final List<String> SCHEMES = List.of("https", "http");
  73 
  74     static {
  75         try {
  76             SSLContext.setDefault(new SimpleSSLContext().get());
  77         } catch (IOException x) {
  78             throw new ExceptionInInitializerError(x);
  79         }
  80     }
  81 
  82     public static void main(String[] args) throws Exception {
  83         for (Function<HttpRequest.Builder, HttpRequest.Builder> m : METHODS) {
  84             for (HttpClient.Version version : List.of(HttpClient.Version.HTTP_1_1)) {
  85                 for (HttpClient.Version reqVersion : VERSIONS) {
  86                     for (String scheme : SCHEMES) {
  87                         ServerSocketFactory ssf;
  88                         if (scheme.equalsIgnoreCase("https")) {
  89                             ssf = SSLServerSocketFactory.getDefault();
  90                         } else {
  91                             ssf = ServerSocketFactory.getDefault();
  92                         }
  93                         test(version, reqVersion, scheme, m, ssf);
  94                     }
  95                 }
  96             }
  97         }
  98     }
  99 
 100     static HttpRequest.Builder DELETE(HttpRequest.Builder builder) {
 101         return builder.DELETE();
 102     }
 103 
 104     static HttpRequest.Builder PUT(HttpRequest.Builder builder) {
 105         HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublishers.noBody();
 106         return builder.PUT(noBody);
 107     }
 108 
 109     static HttpRequest.Builder POST(HttpRequest.Builder builder) {
 110         HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublishers.noBody();
 111         return builder.POST(noBody);
 112     }
 113 
 114     static HttpRequest newRequest(URI uri,
 115                                   Duration duration,
 116                                   HttpClient.Version reqVersion,
 117                                   Function<HttpRequest.Builder, HttpRequest.Builder> method) {
 118         HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(uri)
 119                 .timeout(duration);
 120         if (method != null) reqBuilder = method.apply(reqBuilder);
 121         if (reqVersion != null) reqBuilder = reqBuilder.version(reqVersion);
 122         HttpRequest request = reqBuilder.build();
 123         if (duration.compareTo(Duration.ofSeconds(1)) >= 0) {
 124             if (method == null || !request.method().equalsIgnoreCase("get")) {
 125                 out.println("Skipping " + duration + " for " + request.method());
 126                 return null;
 127             }
 128         }
 129         return request;
 130     }
 131 
 132     public static void test(HttpClient.Version version,
 133                             HttpClient.Version reqVersion,
 134                             String scheme,
 135                             Function<HttpRequest.Builder, HttpRequest.Builder> method,
 136                             ServerSocketFactory ssf)
 137             throws Exception
 138     {
 139         HttpClient.Builder builder = HttpClient.newBuilder()
 140                 .proxy(HttpClient.Builder.NO_PROXY);
 141         if (version != null) builder.version(version);
 142         HttpClient client = builder.build();
 143         out.printf("%ntest(version=%s, reqVersion=%s, scheme=%s)%n", version, reqVersion, scheme);
 144         try (ServerSocket ss = ssf.createServerSocket()) {
 145             ss.setReuseAddress(false);
 146             ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
 147             int port = ss.getLocalPort();
 148             URI uri = new URI(scheme +"://localhost:" + port + "/");
 149 
 150             out.println("--- TESTING Async");
 151             int count = 0;
 152             for (Duration duration : TIMEOUTS) {
 153                 out.println("  with duration of " + duration);
 154                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
 155                 if (request == null) continue;
 156                 count++;
 157                 try {
 158                     HttpResponse<?> resp = client.sendAsync(request, BodyHandlers.discarding()).join();
 159                     out.println("Unexpected response for: " + request);
 160                     out.println("\t from " + ss.getLocalSocketAddress());
 161                     out.println("Response is: " + resp);
 162                     out.println("Headers: " + resp.headers().map());
 163                     out.println("Body (should be null): " + resp.body());
 164                     throw new RuntimeException("Unexpected response: " + resp.statusCode());
 165                 } catch (CompletionException e) {
 166                     if (!(e.getCause() instanceof HttpTimeoutException)) {
 167                         throw new RuntimeException("Unexpected exception: " + e.getCause());
 168                     } else {
 169                         out.println("Caught expected timeout: " + e.getCause());
 170                     }
 171                 }
 172             }
 173             assert count >= TIMEOUTS.size() -1;
 174 
 175             out.println("--- TESTING Sync");
 176             count = 0;
 177             for (Duration duration : TIMEOUTS) {
 178                 out.println("  with duration of " + duration);
 179                 HttpRequest request = newRequest(uri, duration, reqVersion, method);
 180                 if (request == null) continue;
 181                 count++;
 182                 try {
 183                     client.send(request, BodyHandlers.discarding());
 184                 } catch (HttpTimeoutException e) {
 185                     out.println("Caught expected timeout: " + e);
 186                 }
 187             }
 188             assert count >= TIMEOUTS.size() -1;
 189 
 190         }
 191     }
 192 }