55763
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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.lang.reflect.Method;
|
|
25 |
import java.net.Authenticator;
|
|
26 |
import java.net.CookieManager;
|
|
27 |
import java.net.InetSocketAddress;
|
|
28 |
import java.net.ProxySelector;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.concurrent.Executor;
|
|
31 |
import javax.net.ssl.SSLContext;
|
|
32 |
import javax.net.ssl.SSLParameters;
|
|
33 |
import jdk.incubator.http.HttpClient;
|
|
34 |
import jdk.incubator.http.HttpClient.Redirect;
|
|
35 |
import jdk.incubator.http.HttpClient.Version;
|
|
36 |
import jdk.testlibrary.SimpleSSLContext;
|
|
37 |
import org.testng.annotations.Test;
|
|
38 |
import static org.testng.Assert.*;
|
|
39 |
|
|
40 |
/*
|
|
41 |
* @test
|
|
42 |
* @summary HttpClient[.Builder] API and behaviour checks
|
|
43 |
* @library /lib/testlibrary/
|
|
44 |
* @build jdk.testlibrary.SimpleSSLContext
|
|
45 |
* @run testng HttpClientBuilderTest
|
|
46 |
*/
|
|
47 |
|
|
48 |
public class HttpClientBuilderTest {
|
|
49 |
|
|
50 |
@Test
|
|
51 |
public void testDefaults() throws Exception {
|
|
52 |
List<HttpClient> clients = List.of(HttpClient.newHttpClient(),
|
|
53 |
HttpClient.newBuilder().build());
|
|
54 |
|
|
55 |
for (HttpClient client : clients) {
|
|
56 |
// Empty optionals and defaults
|
|
57 |
assertFalse(client.authenticator().isPresent());
|
|
58 |
assertFalse(client.cookieManager().isPresent());
|
|
59 |
assertFalse(client.executor().isPresent());
|
|
60 |
assertFalse(client.proxy().isPresent());
|
|
61 |
assertTrue(client.sslParameters() != null);
|
|
62 |
assertTrue(client.followRedirects().equals(HttpClient.Redirect.NEVER));
|
|
63 |
assertTrue(client.sslContext() == SSLContext.getDefault());
|
|
64 |
assertTrue(client.version().equals(HttpClient.Version.HTTP_2));
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
@Test
|
|
69 |
public void testNull() throws Exception {
|
|
70 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
71 |
assertThrows(NullPointerException.class, () -> builder.authenticator(null));
|
|
72 |
assertThrows(NullPointerException.class, () -> builder.cookieManager(null));
|
|
73 |
assertThrows(NullPointerException.class, () -> builder.executor(null));
|
|
74 |
assertThrows(NullPointerException.class, () -> builder.proxy(null));
|
|
75 |
assertThrows(NullPointerException.class, () -> builder.sslParameters(null));
|
|
76 |
assertThrows(NullPointerException.class, () -> builder.followRedirects(null));
|
|
77 |
assertThrows(NullPointerException.class, () -> builder.sslContext(null));
|
|
78 |
assertThrows(NullPointerException.class, () -> builder.version(null));
|
|
79 |
}
|
|
80 |
|
|
81 |
static class TestAuthenticator extends Authenticator { }
|
|
82 |
|
|
83 |
@Test
|
|
84 |
public void testAuthenticator() {
|
|
85 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
86 |
Authenticator a = new TestAuthenticator();
|
|
87 |
builder.authenticator(a);
|
|
88 |
assertTrue(builder.build().authenticator().get() == a);
|
|
89 |
Authenticator b = new TestAuthenticator();
|
|
90 |
builder.authenticator(b);
|
|
91 |
assertTrue(builder.build().authenticator().get() == b);
|
|
92 |
assertThrows(NullPointerException.class, () -> builder.authenticator(null));
|
|
93 |
Authenticator c = new TestAuthenticator();
|
|
94 |
builder.authenticator(c);
|
|
95 |
assertTrue(builder.build().authenticator().get() == c);
|
|
96 |
}
|
|
97 |
|
|
98 |
@Test
|
|
99 |
public void testCookieManager() {
|
|
100 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
101 |
CookieManager a = new CookieManager();
|
|
102 |
builder.cookieManager(a);
|
|
103 |
assertTrue(builder.build().cookieManager().get() == a);
|
|
104 |
CookieManager b = new CookieManager();
|
|
105 |
builder.cookieManager(b);
|
|
106 |
assertTrue(builder.build().cookieManager().get() == b);
|
|
107 |
assertThrows(NullPointerException.class, () -> builder.cookieManager(null));
|
|
108 |
CookieManager c = new CookieManager();
|
|
109 |
builder.cookieManager(c);
|
|
110 |
assertTrue(builder.build().cookieManager().get() == c);
|
|
111 |
}
|
|
112 |
|
|
113 |
static class TestExecutor implements Executor {
|
|
114 |
public void execute(Runnable r) { }
|
|
115 |
}
|
|
116 |
|
|
117 |
@Test
|
|
118 |
public void testExecutor() {
|
|
119 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
120 |
TestExecutor a = new TestExecutor();
|
|
121 |
builder.executor(a);
|
|
122 |
assertTrue(builder.build().executor().get() == a);
|
|
123 |
TestExecutor b = new TestExecutor();
|
|
124 |
builder.executor(b);
|
|
125 |
assertTrue(builder.build().executor().get() == b);
|
|
126 |
assertThrows(NullPointerException.class, () -> builder.executor(null));
|
|
127 |
TestExecutor c = new TestExecutor();
|
|
128 |
builder.executor(c);
|
|
129 |
assertTrue(builder.build().executor().get() == c);
|
|
130 |
}
|
|
131 |
|
|
132 |
@Test
|
|
133 |
public void testProxySelector() {
|
|
134 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
135 |
ProxySelector a = ProxySelector.of(null);
|
|
136 |
builder.proxy(a);
|
|
137 |
assertTrue(builder.build().proxy().get() == a);
|
|
138 |
ProxySelector b = ProxySelector.of(InetSocketAddress.createUnresolved("foo", 80));
|
|
139 |
builder.proxy(b);
|
|
140 |
assertTrue(builder.build().proxy().get() == b);
|
|
141 |
assertThrows(NullPointerException.class, () -> builder.proxy(null));
|
|
142 |
ProxySelector c = ProxySelector.of(InetSocketAddress.createUnresolved("bar", 80));
|
|
143 |
builder.proxy(c);
|
|
144 |
assertTrue(builder.build().proxy().get() == c);
|
|
145 |
}
|
|
146 |
|
|
147 |
@Test
|
|
148 |
public void testSSLParameters() {
|
|
149 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
150 |
SSLParameters a = new SSLParameters();
|
|
151 |
a.setCipherSuites(new String[] { "A" });
|
|
152 |
builder.sslParameters(a);
|
|
153 |
a.setCipherSuites(new String[] { "Z" });
|
|
154 |
assertTrue(builder.build().sslParameters() != (a));
|
|
155 |
assertTrue(builder.build().sslParameters().getCipherSuites()[0].equals("A"));
|
|
156 |
SSLParameters b = new SSLParameters();
|
|
157 |
b.setEnableRetransmissions(true);
|
|
158 |
builder.sslParameters(b);
|
|
159 |
assertTrue(builder.build().sslParameters() != b);
|
|
160 |
assertTrue(builder.build().sslParameters().getEnableRetransmissions());
|
|
161 |
assertThrows(NullPointerException.class, () -> builder.sslParameters(null));
|
|
162 |
SSLParameters c = new SSLParameters();
|
|
163 |
c.setProtocols(new String[] { "C" });
|
|
164 |
builder.sslParameters(c);
|
|
165 |
c.setProtocols(new String[] { "D" });
|
|
166 |
assertTrue(builder.build().sslParameters().getProtocols()[0].equals("C"));
|
|
167 |
}
|
|
168 |
|
|
169 |
@Test
|
|
170 |
public void testSSLContext() throws Exception {
|
|
171 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
172 |
SSLContext a = (new SimpleSSLContext()).get();
|
|
173 |
builder.sslContext(a);
|
|
174 |
assertTrue(builder.build().sslContext() == a);
|
|
175 |
SSLContext b = (new SimpleSSLContext()).get();
|
|
176 |
builder.sslContext(b);
|
|
177 |
assertTrue(builder.build().sslContext() == b);
|
|
178 |
assertThrows(NullPointerException.class, () -> builder.sslContext(null));
|
|
179 |
SSLContext c = (new SimpleSSLContext()).get();
|
|
180 |
builder.sslContext(c);
|
|
181 |
assertTrue(builder.build().sslContext() == c);
|
|
182 |
}
|
|
183 |
|
|
184 |
@Test
|
|
185 |
public void testFollowRedirects() {
|
|
186 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
187 |
builder.followRedirects(Redirect.ALWAYS);
|
|
188 |
assertTrue(builder.build().followRedirects() == Redirect.ALWAYS);
|
|
189 |
builder.followRedirects(Redirect.NEVER);
|
|
190 |
assertTrue(builder.build().followRedirects() == Redirect.NEVER);
|
|
191 |
assertThrows(NullPointerException.class, () -> builder.followRedirects(null));
|
|
192 |
builder.followRedirects(Redirect.SAME_PROTOCOL);
|
|
193 |
assertTrue(builder.build().followRedirects() == Redirect.SAME_PROTOCOL);
|
|
194 |
builder.followRedirects(Redirect.SECURE);
|
|
195 |
assertTrue(builder.build().followRedirects() == Redirect.SECURE);
|
|
196 |
}
|
|
197 |
|
|
198 |
@Test
|
|
199 |
public void testVersion() {
|
|
200 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
201 |
builder.version(Version.HTTP_2);
|
|
202 |
assertTrue(builder.build().version() == Version.HTTP_2);
|
|
203 |
builder.version(Version.HTTP_1_1);
|
|
204 |
assertTrue(builder.build().version() == Version.HTTP_1_1);
|
|
205 |
assertThrows(NullPointerException.class, () -> builder.version(null));
|
|
206 |
builder.version(Version.HTTP_2);
|
|
207 |
assertTrue(builder.build().version() == Version.HTTP_2);
|
|
208 |
builder.version(Version.HTTP_1_1);
|
|
209 |
assertTrue(builder.build().version() == Version.HTTP_1_1);
|
|
210 |
}
|
|
211 |
|
|
212 |
@Test
|
|
213 |
static void testPriority() throws Exception {
|
|
214 |
HttpClient.Builder builder = HttpClient.newBuilder();
|
|
215 |
assertThrows(IllegalArgumentException.class, () -> builder.priority(-1));
|
|
216 |
assertThrows(IllegalArgumentException.class, () -> builder.priority(0));
|
|
217 |
assertThrows(IllegalArgumentException.class, () -> builder.priority(257));
|
|
218 |
assertThrows(IllegalArgumentException.class, () -> builder.priority(500));
|
|
219 |
|
|
220 |
builder.priority(1);
|
|
221 |
builder.build();
|
|
222 |
builder.priority(256);
|
|
223 |
builder.build();
|
|
224 |
}
|
|
225 |
|
|
226 |
|
|
227 |
/* ---- standalone entry point ---- */
|
|
228 |
|
|
229 |
public static void main(String[] args) throws Exception {
|
|
230 |
HttpClientBuilderTest test = new HttpClientBuilderTest();
|
|
231 |
for (Method m : HttpClientBuilderTest.class.getDeclaredMethods()) {
|
|
232 |
if (m.isAnnotationPresent(Test.class)) {
|
|
233 |
try {
|
|
234 |
m.invoke(test);
|
|
235 |
System.out.printf("test %s: success%n", m.getName());
|
|
236 |
} catch (Throwable t ) {
|
|
237 |
System.out.printf("test %s: failed%n", m.getName());
|
|
238 |
t.printStackTrace();
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|