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