author | dfuchs |
Wed, 31 Jan 2018 16:18:41 +0000 | |
branch | http-client-branch |
changeset 56054 | 352e845ae744 |
parent 55973 | 4d9b002587db |
child 56082 | 1da51fab3032 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
48083 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
import java.net.ServerSocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
import jdk.incubator.http.HttpClient; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import jdk.incubator.http.HttpResponse; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import jdk.incubator.http.HttpTimeoutException; |
48083 | 31 |
import jdk.testlibrary.SimpleSSLContext; |
32 |
||
33 |
import javax.net.ServerSocketFactory; |
|
34 |
import javax.net.ssl.SSLContext; |
|
35 |
import javax.net.ssl.SSLServerSocketFactory; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.time.Duration; |
48083 | 37 |
import java.util.Arrays; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.List; |
48083 | 39 |
import java.util.concurrent.CompletionException; |
40 |
import java.util.function.Function; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import static java.lang.System.out; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
* @test |
48083 | 47 |
* @library /lib/testlibrary |
48 |
* @build jdk.testlibrary.SimpleSSLContext |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
* @summary Basic tests for response timeouts |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
* @run main/othervm TimeoutBasic |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
public class TimeoutBasic { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
|
48083 | 55 |
static List<Duration> TIMEOUTS = List.of(Duration.ofSeconds(1), |
56 |
Duration.ofMillis(100), |
|
57 |
Duration.ofNanos(99), |
|
58 |
Duration.ofNanos(1)); |
|
59 |
||
60 |
static final List<Function<HttpRequest.Builder, HttpRequest.Builder>> METHODS = |
|
61 |
Arrays.asList(HttpRequest.Builder::GET, |
|
62 |
TimeoutBasic::DELETE, |
|
63 |
TimeoutBasic::PUT, |
|
64 |
TimeoutBasic::POST, |
|
65 |
null); |
|
66 |
||
67 |
static final List<HttpClient.Version> VERSIONS = |
|
68 |
Arrays.asList(HttpClient.Version.HTTP_2, HttpClient.Version.HTTP_1_1, null); |
|
69 |
||
70 |
static final List<String> SCHEMES = List.of("https", "http"); |
|
71 |
||
72 |
static { |
|
73 |
try { |
|
74 |
SSLContext.setDefault(new SimpleSSLContext().get()); |
|
75 |
} catch (IOException x) { |
|
76 |
throw new ExceptionInInitializerError(x); |
|
77 |
} |
|
78 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
public static void main(String[] args) throws Exception { |
48083 | 81 |
for (Function<HttpRequest.Builder, HttpRequest.Builder> m : METHODS) { |
82 |
for (HttpClient.Version version : List.of(HttpClient.Version.HTTP_1_1)) { |
|
83 |
for (HttpClient.Version reqVersion : VERSIONS) { |
|
84 |
for (String scheme : SCHEMES) { |
|
85 |
ServerSocketFactory ssf; |
|
86 |
if (scheme.equalsIgnoreCase("https")) { |
|
87 |
ssf = SSLServerSocketFactory.getDefault(); |
|
88 |
} else { |
|
89 |
ssf = ServerSocketFactory.getDefault(); |
|
90 |
} |
|
91 |
test(version, reqVersion, scheme, m, ssf); |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
static HttpRequest.Builder DELETE(HttpRequest.Builder builder) { |
|
99 |
HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
|
100 |
return builder.DELETE(noBody); |
|
101 |
} |
|
102 |
||
103 |
static HttpRequest.Builder PUT(HttpRequest.Builder builder) { |
|
104 |
HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
|
105 |
return builder.PUT(noBody); |
|
106 |
} |
|
107 |
||
108 |
static HttpRequest.Builder POST(HttpRequest.Builder builder) { |
|
109 |
HttpRequest.BodyPublisher noBody = HttpRequest.BodyPublisher.noBody(); |
|
110 |
return builder.POST(noBody); |
|
111 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
|
48083 | 113 |
static HttpRequest newRequest(URI uri, |
114 |
Duration duration, |
|
115 |
HttpClient.Version reqVersion, |
|
116 |
Function<HttpRequest.Builder, HttpRequest.Builder> method) { |
|
117 |
HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(uri) |
|
118 |
.timeout(duration); |
|
119 |
if (method != null) reqBuilder = method.apply(reqBuilder); |
|
120 |
if (reqVersion != null) reqBuilder = reqBuilder.version(reqVersion); |
|
121 |
HttpRequest request = reqBuilder.build(); |
|
122 |
if (duration.compareTo(Duration.ofSeconds(1)) >= 0) { |
|
123 |
if (method == null || !request.method().equalsIgnoreCase("get")) { |
|
124 |
out.println("Skipping " + duration + " for " + request.method()); |
|
125 |
return null; |
|
126 |
} |
|
127 |
} |
|
128 |
return request; |
|
129 |
} |
|
130 |
||
131 |
public static void test(HttpClient.Version version, |
|
132 |
HttpClient.Version reqVersion, |
|
133 |
String scheme, |
|
134 |
Function<HttpRequest.Builder, HttpRequest.Builder> method, |
|
135 |
ServerSocketFactory ssf) |
|
136 |
throws Exception |
|
137 |
{ |
|
138 |
HttpClient.Builder builder = HttpClient.newBuilder() |
|
139 |
.proxy(HttpClient.Builder.NO_PROXY); |
|
140 |
if (version != null) builder.version(version); |
|
141 |
HttpClient client = builder.build(); |
|
142 |
out.printf("%ntest(version=%s, reqVersion=%s, scheme=%s)%n", version, reqVersion, scheme); |
|
143 |
try (ServerSocket ss = ssf.createServerSocket(0, 20)) { |
|
144 |
int port = ss.getLocalPort(); |
|
145 |
URI uri = new URI(scheme +"://127.0.0.1:" + port + "/"); |
|
146 |
||
147 |
out.println("--- TESTING Async"); |
|
148 |
int count = 0; |
|
149 |
for (Duration duration : TIMEOUTS) { |
|
150 |
out.println(" with duration of " + duration); |
|
151 |
HttpRequest request = newRequest(uri, duration, reqVersion, method); |
|
152 |
if (request == null) continue; |
|
153 |
count++; |
|
154 |
try { |
|
155 |
HttpResponse<?> resp = client.sendAsync(request, discard(null)).join(); |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
55973
diff
changeset
|
156 |
out.println("Unexpected response for: " + request); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
55973
diff
changeset
|
157 |
out.println("\t from " + ss.getLocalSocketAddress()); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
55973
diff
changeset
|
158 |
out.println("Response is: " + resp); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
55973
diff
changeset
|
159 |
out.println("Headers: " + resp.headers().map()); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
55973
diff
changeset
|
160 |
out.println("Body (should be null): " + resp.body()); |
48083 | 161 |
throw new RuntimeException("Unexpected response: " + resp.statusCode()); |
162 |
} catch (CompletionException e) { |
|
163 |
if (!(e.getCause() instanceof HttpTimeoutException)) { |
|
164 |
throw new RuntimeException("Unexpected exception: " + e.getCause()); |
|
165 |
} else { |
|
166 |
out.println("Caught expected timeout: " + e.getCause()); |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
assert count >= TIMEOUTS.size() -1; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
out.println("--- TESTING Sync"); |
48083 | 173 |
count = 0; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
for (Duration duration : TIMEOUTS) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
out.println(" with duration of " + duration); |
48083 | 176 |
HttpRequest request = newRequest(uri, duration, reqVersion, method); |
177 |
if (request == null) continue; |
|
178 |
count++; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
client.send(request, discard(null)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
} catch (HttpTimeoutException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
out.println("Caught expected timeout: " + e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
} |
48083 | 185 |
assert count >= TIMEOUTS.size() -1; |
186 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |