48083
|
1 |
/*
|
49765
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
|
48083
|
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 |
|
49765
|
24 |
import java.io.IOException;
|
48083
|
25 |
import java.lang.reflect.Method;
|
|
26 |
import java.net.Authenticator;
|
|
27 |
import java.net.CookieHandler;
|
|
28 |
import java.net.CookieManager;
|
|
29 |
import java.net.InetSocketAddress;
|
|
30 |
import java.net.ProxySelector;
|
49765
|
31 |
import java.net.URI;
|
|
32 |
import java.net.http.HttpHeaders;
|
|
33 |
import java.net.http.HttpRequest;
|
|
34 |
import java.net.http.HttpRequest.BodyPublishers;
|
|
35 |
import java.net.http.HttpResponse;
|
|
36 |
import java.net.http.HttpResponse.BodyHandler;
|
|
37 |
import java.net.http.HttpResponse.BodyHandlers;
|
|
38 |
import java.net.http.HttpResponse.PushPromiseHandler;
|
|
39 |
import java.time.Duration;
|
48083
|
40 |
import java.util.List;
|
49765
|
41 |
import java.util.Map;
|
|
42 |
import java.util.Optional;
|
|
43 |
import java.util.TreeMap;
|
|
44 |
import java.util.concurrent.CompletableFuture;
|
48083
|
45 |
import java.util.concurrent.Executor;
|
|
46 |
import javax.net.ssl.SSLContext;
|
|
47 |
import javax.net.ssl.SSLParameters;
|
49765
|
48 |
import java.net.http.HttpClient;
|
|
49 |
import java.net.http.HttpClient.Redirect;
|
|
50 |
import java.net.http.HttpClient.Version;
|
48083
|
51 |
import jdk.testlibrary.SimpleSSLContext;
|
|
52 |
import org.testng.annotations.Test;
|
|
53 |
import static org.testng.Assert.*;
|
|
54 |
|
|
55 |
/*
|
|
56 |
* @test
|
|
57 |
* @summary HttpClient[.Builder] API and behaviour checks
|
|
58 |
* @library /lib/testlibrary/
|
|
59 |
* @build jdk.testlibrary.SimpleSSLContext
|
|
60 |
* @run testng HttpClientBuilderTest
|
|
61 |
*/
|
|
62 |
|
|
63 |
public class HttpClientBuilderTest {
|
|
64 |
|
49765
|
65 |
static final Class<NullPointerException> NPE = NullPointerException.class;
|
|
66 |
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
|
|
67 |
|
48083
|
68 |
@Test
|
|
69 |
public void testDefaults() throws Exception {
|
|
70 |
List<HttpClient> clients = List.of(HttpClient.newHttpClient(),
|
|
71 |
HttpClient.newBuilder().build());
|
|
72 |
|
|
73 |
for (HttpClient client : clients) {
|
|
74 |
// Empty optionals and defaults
|
|
75 |
assertFalse(client.authenticator().isPresent());
|
|
76 |
assertFalse(client.cookieHandler().isPresent());
|
|
77 |
assertFalse(client.executor().isPresent());
|
|
78 |
assertFalse(client.proxy().isPresent());
|
|
79 |
assertTrue(client.sslParameters() != null);
|
|
80 |
assertTrue(client.followRedirects().equals(HttpClient.Redirect.NEVER));
|
|
81 |
assertTrue(client.sslContext() == SSLContext.getDefault());
|
|
82 |
assertTrue(client.version().equals(HttpClient.Version.HTTP_2));
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
@Test
|
|
87 |
public void testNull() throws Exception {
|
|
88 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
49765
|
89 |
assertThrows(NPE, () -> builder.authenticator(null));
|
|
90 |
assertThrows(NPE, () -> builder.cookieHandler(null));
|
|
91 |
assertThrows(NPE, () -> builder.executor(null));
|
|
92 |
assertThrows(NPE, () -> builder.proxy(null));
|
|
93 |
assertThrows(NPE, () -> builder.sslParameters(null));
|
|
94 |
assertThrows(NPE, () -> builder.followRedirects(null));
|
|
95 |
assertThrows(NPE, () -> builder.sslContext(null));
|
|
96 |
assertThrows(NPE, () -> builder.version(null));
|
48083
|
97 |
}
|
|
98 |
|
|
99 |
static class TestAuthenticator extends Authenticator { }
|
|
100 |
|
|
101 |
@Test
|
|
102 |
public void testAuthenticator() {
|
|
103 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
104 |
Authenticator a = new TestAuthenticator();
|
|
105 |
builder.authenticator(a);
|
|
106 |
assertTrue(builder.build().authenticator().get() == a);
|
|
107 |
Authenticator b = new TestAuthenticator();
|
|
108 |
builder.authenticator(b);
|
|
109 |
assertTrue(builder.build().authenticator().get() == b);
|
49765
|
110 |
assertThrows(NPE, () -> builder.authenticator(null));
|
48083
|
111 |
Authenticator c = new TestAuthenticator();
|
|
112 |
builder.authenticator(c);
|
|
113 |
assertTrue(builder.build().authenticator().get() == c);
|
|
114 |
}
|
|
115 |
|
|
116 |
@Test
|
|
117 |
public void testCookieHandler() {
|
|
118 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
119 |
CookieHandler a = new CookieManager();
|
|
120 |
builder.cookieHandler(a);
|
|
121 |
assertTrue(builder.build().cookieHandler().get() == a);
|
|
122 |
CookieHandler b = new CookieManager();
|
|
123 |
builder.cookieHandler(b);
|
|
124 |
assertTrue(builder.build().cookieHandler().get() == b);
|
49765
|
125 |
assertThrows(NPE, () -> builder.cookieHandler(null));
|
48083
|
126 |
CookieManager c = new CookieManager();
|
|
127 |
builder.cookieHandler(c);
|
|
128 |
assertTrue(builder.build().cookieHandler().get() == c);
|
|
129 |
}
|
|
130 |
|
|
131 |
static class TestExecutor implements Executor {
|
|
132 |
public void execute(Runnable r) { }
|
|
133 |
}
|
|
134 |
|
|
135 |
@Test
|
|
136 |
public void testExecutor() {
|
|
137 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
138 |
TestExecutor a = new TestExecutor();
|
|
139 |
builder.executor(a);
|
|
140 |
assertTrue(builder.build().executor().get() == a);
|
|
141 |
TestExecutor b = new TestExecutor();
|
|
142 |
builder.executor(b);
|
|
143 |
assertTrue(builder.build().executor().get() == b);
|
49765
|
144 |
assertThrows(NPE, () -> builder.executor(null));
|
48083
|
145 |
TestExecutor c = new TestExecutor();
|
|
146 |
builder.executor(c);
|
|
147 |
assertTrue(builder.build().executor().get() == c);
|
|
148 |
}
|
|
149 |
|
|
150 |
@Test
|
|
151 |
public void testProxySelector() {
|
|
152 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
153 |
ProxySelector a = ProxySelector.of(null);
|
|
154 |
builder.proxy(a);
|
|
155 |
assertTrue(builder.build().proxy().get() == a);
|
|
156 |
ProxySelector b = ProxySelector.of(InetSocketAddress.createUnresolved("foo", 80));
|
|
157 |
builder.proxy(b);
|
|
158 |
assertTrue(builder.build().proxy().get() == b);
|
49765
|
159 |
assertThrows(NPE, () -> builder.proxy(null));
|
48083
|
160 |
ProxySelector c = ProxySelector.of(InetSocketAddress.createUnresolved("bar", 80));
|
|
161 |
builder.proxy(c);
|
|
162 |
assertTrue(builder.build().proxy().get() == c);
|
|
163 |
}
|
|
164 |
|
|
165 |
@Test
|
|
166 |
public void testSSLParameters() {
|
|
167 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
168 |
SSLParameters a = new SSLParameters();
|
|
169 |
a.setCipherSuites(new String[] { "A" });
|
|
170 |
builder.sslParameters(a);
|
|
171 |
a.setCipherSuites(new String[] { "Z" });
|
|
172 |
assertTrue(builder.build().sslParameters() != (a));
|
|
173 |
assertTrue(builder.build().sslParameters().getCipherSuites()[0].equals("A"));
|
|
174 |
SSLParameters b = new SSLParameters();
|
|
175 |
b.setEnableRetransmissions(true);
|
|
176 |
builder.sslParameters(b);
|
|
177 |
assertTrue(builder.build().sslParameters() != b);
|
|
178 |
assertTrue(builder.build().sslParameters().getEnableRetransmissions());
|
49765
|
179 |
assertThrows(NPE, () -> builder.sslParameters(null));
|
48083
|
180 |
SSLParameters c = new SSLParameters();
|
|
181 |
c.setProtocols(new String[] { "C" });
|
|
182 |
builder.sslParameters(c);
|
|
183 |
c.setProtocols(new String[] { "D" });
|
|
184 |
assertTrue(builder.build().sslParameters().getProtocols()[0].equals("C"));
|
|
185 |
}
|
|
186 |
|
|
187 |
@Test
|
|
188 |
public void testSSLContext() throws Exception {
|
|
189 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
190 |
SSLContext a = (new SimpleSSLContext()).get();
|
|
191 |
builder.sslContext(a);
|
|
192 |
assertTrue(builder.build().sslContext() == a);
|
|
193 |
SSLContext b = (new SimpleSSLContext()).get();
|
|
194 |
builder.sslContext(b);
|
|
195 |
assertTrue(builder.build().sslContext() == b);
|
49765
|
196 |
assertThrows(NPE, () -> builder.sslContext(null));
|
48083
|
197 |
SSLContext c = (new SimpleSSLContext()).get();
|
|
198 |
builder.sslContext(c);
|
|
199 |
assertTrue(builder.build().sslContext() == c);
|
|
200 |
}
|
|
201 |
|
|
202 |
@Test
|
|
203 |
public void testFollowRedirects() {
|
|
204 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
205 |
builder.followRedirects(Redirect.ALWAYS);
|
|
206 |
assertTrue(builder.build().followRedirects() == Redirect.ALWAYS);
|
|
207 |
builder.followRedirects(Redirect.NEVER);
|
|
208 |
assertTrue(builder.build().followRedirects() == Redirect.NEVER);
|
49765
|
209 |
assertThrows(NPE, () -> builder.followRedirects(null));
|
|
210 |
builder.followRedirects(Redirect.NORMAL);
|
|
211 |
assertTrue(builder.build().followRedirects() == Redirect.NORMAL);
|
48083
|
212 |
}
|
|
213 |
|
|
214 |
@Test
|
|
215 |
public void testVersion() {
|
|
216 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
217 |
builder.version(Version.HTTP_2);
|
|
218 |
assertTrue(builder.build().version() == Version.HTTP_2);
|
|
219 |
builder.version(Version.HTTP_1_1);
|
|
220 |
assertTrue(builder.build().version() == Version.HTTP_1_1);
|
49765
|
221 |
assertThrows(NPE, () -> builder.version(null));
|
48083
|
222 |
builder.version(Version.HTTP_2);
|
|
223 |
assertTrue(builder.build().version() == Version.HTTP_2);
|
|
224 |
builder.version(Version.HTTP_1_1);
|
|
225 |
assertTrue(builder.build().version() == Version.HTTP_1_1);
|
|
226 |
}
|
|
227 |
|
|
228 |
@Test
|
|
229 |
static void testPriority() throws Exception {
|
|
230 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
49765
|
231 |
assertThrows(IAE, () -> builder.priority(-1));
|
|
232 |
assertThrows(IAE, () -> builder.priority(0));
|
|
233 |
assertThrows(IAE, () -> builder.priority(257));
|
|
234 |
assertThrows(IAE, () -> builder.priority(500));
|
48083
|
235 |
|
|
236 |
builder.priority(1);
|
|
237 |
builder.build();
|
|
238 |
builder.priority(256);
|
|
239 |
builder.build();
|
|
240 |
}
|
|
241 |
|
49765
|
242 |
// ---
|
|
243 |
|
|
244 |
static final URI uri = URI.create("http://foo.com/");
|
|
245 |
|
|
246 |
@Test
|
|
247 |
static void testHttpClientSendArgs() throws Exception {
|
|
248 |
HttpClient client = HttpClient.newHttpClient();
|
|
249 |
HttpRequest request = HttpRequest.newBuilder(uri).build();
|
|
250 |
|
|
251 |
assertThrows(NPE, () -> client.send(null, BodyHandlers.discarding()));
|
|
252 |
assertThrows(NPE, () -> client.send(request, null));
|
|
253 |
assertThrows(NPE, () -> client.send(null, null));
|
|
254 |
|
|
255 |
assertThrows(NPE, () -> client.sendAsync(null, BodyHandlers.discarding()));
|
|
256 |
assertThrows(NPE, () -> client.sendAsync(request, null));
|
|
257 |
assertThrows(NPE, () -> client.sendAsync(null, null));
|
|
258 |
|
|
259 |
assertThrows(NPE, () -> client.sendAsync(null, BodyHandlers.discarding(), null));
|
|
260 |
assertThrows(NPE, () -> client.sendAsync(request, null, null));
|
|
261 |
assertThrows(NPE, () -> client.sendAsync(null, null, null));
|
|
262 |
|
|
263 |
// CONNECT is disallowed in the implementation, since it is used for
|
|
264 |
// tunneling, and is handled separately for security checks.
|
|
265 |
HttpRequest connectRequest = new HttpConnectRequest();
|
|
266 |
assertThrows(IAE, () -> client.send(connectRequest, BodyHandlers.discarding()));
|
|
267 |
assertThrows(IAE, () -> client.sendAsync(connectRequest, BodyHandlers.discarding()));
|
|
268 |
assertThrows(IAE, () -> client.sendAsync(connectRequest, BodyHandlers.discarding(), null));
|
|
269 |
}
|
|
270 |
|
|
271 |
static class HttpConnectRequest extends HttpRequest {
|
|
272 |
@Override public Optional<BodyPublisher> bodyPublisher() { return Optional.empty(); }
|
|
273 |
@Override public String method() { return "CONNECT"; }
|
|
274 |
@Override public Optional<Duration> timeout() { return Optional.empty(); }
|
|
275 |
@Override public boolean expectContinue() { return false; }
|
|
276 |
@Override public URI uri() { return URI.create("http://foo.com/"); }
|
|
277 |
@Override public Optional<Version> version() { return Optional.empty(); }
|
50681
|
278 |
@Override public HttpHeaders headers() { return HttpHeaders.of(Map.of(), (x, y) -> true); }
|
49765
|
279 |
}
|
|
280 |
|
|
281 |
// ---
|
|
282 |
|
|
283 |
static final Class<UnsupportedOperationException> UOE =
|
|
284 |
UnsupportedOperationException.class;
|
|
285 |
|
|
286 |
@Test
|
|
287 |
static void testUnsupportedWebSocket() throws Exception {
|
|
288 |
// @implSpec The default implementation of this method throws
|
|
289 |
// {@code UnsupportedOperationException}.
|
|
290 |
assertThrows(UOE, () -> (new MockHttpClient()).newWebSocketBuilder());
|
|
291 |
}
|
|
292 |
|
|
293 |
static class MockHttpClient extends HttpClient {
|
|
294 |
@Override public Optional<CookieHandler> cookieHandler() { return null; }
|
|
295 |
@Override public Redirect followRedirects() { return null; }
|
|
296 |
@Override public Optional<ProxySelector> proxy() { return null; }
|
|
297 |
@Override public SSLContext sslContext() { return null; }
|
|
298 |
@Override public SSLParameters sslParameters() { return null; }
|
|
299 |
@Override public Optional<Authenticator> authenticator() { return null; }
|
|
300 |
@Override public Version version() { return null; }
|
|
301 |
@Override public Optional<Executor> executor() { return null; }
|
|
302 |
@Override public <T> HttpResponse<T>
|
|
303 |
send(HttpRequest request, BodyHandler<T> responseBodyHandler)
|
|
304 |
throws IOException, InterruptedException {
|
|
305 |
return null;
|
|
306 |
}
|
|
307 |
@Override public <T> CompletableFuture<HttpResponse<T>>
|
|
308 |
sendAsync(HttpRequest request, BodyHandler<T> responseBodyHandler) {
|
|
309 |
return null;
|
|
310 |
}
|
|
311 |
@Override
|
|
312 |
public <T> CompletableFuture<HttpResponse<T>>
|
|
313 |
sendAsync(HttpRequest x, BodyHandler<T> y, PushPromiseHandler<T> z) {
|
|
314 |
return null;
|
|
315 |
}
|
|
316 |
}
|
48083
|
317 |
|
|
318 |
/* ---- standalone entry point ---- */
|
|
319 |
|
|
320 |
public static void main(String[] args) throws Exception {
|
|
321 |
HttpClientBuilderTest test = new HttpClientBuilderTest();
|
|
322 |
for (Method m : HttpClientBuilderTest.class.getDeclaredMethods()) {
|
|
323 |
if (m.isAnnotationPresent(Test.class)) {
|
|
324 |
try {
|
|
325 |
m.invoke(test);
|
|
326 |
System.out.printf("test %s: success%n", m.getName());
|
|
327 |
} catch (Throwable t ) {
|
|
328 |
System.out.printf("test %s: failed%n", m.getName());
|
|
329 |
t.printStackTrace();
|
|
330 |
}
|
|
331 |
}
|
|
332 |
}
|
|
333 |
}
|
|
334 |
}
|