author | chegar |
Thu, 08 Mar 2018 17:42:16 +0000 | |
branch | http-client-branch |
changeset 56265 | ec34ae013fbe |
parent 56167 | 96fa4f49a9ff |
child 56281 | 7fdd89dabab2 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56033
diff
changeset
|
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 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary HttpRequest[.Builder] API and behaviour checks |
|
27 |
* @run testng RequestBuilderTest |
|
28 |
*/ |
|
29 |
||
30 |
import java.net.URI; |
|
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
31 |
import java.net.URISyntaxException; |
48083 | 32 |
import java.util.List; |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
33 |
import java.util.Map; |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
34 |
import java.util.Set; |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
35 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56041
diff
changeset
|
36 |
import java.net.http.HttpRequest; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
37 |
import java.net.http.HttpRequest.BodyPublishers; |
48083 | 38 |
import static java.time.Duration.ofNanos; |
39 |
import static java.time.Duration.ofMinutes; |
|
40 |
import static java.time.Duration.ofSeconds; |
|
41 |
import static java.time.Duration.ZERO; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56041
diff
changeset
|
42 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56041
diff
changeset
|
43 |
import static java.net.http.HttpClient.Version.HTTP_2; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56041
diff
changeset
|
44 |
import static java.net.http.HttpRequest.newBuilder; |
48083 | 45 |
import static org.testng.Assert.*; |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
46 |
|
48083 | 47 |
import org.testng.annotations.Test; |
48 |
||
49 |
public class RequestBuilderTest { |
|
50 |
||
51 |
static final URI uri = URI.create("http://foo.com/"); |
|
52 |
static final Class<NullPointerException> NPE = NullPointerException.class; |
|
53 |
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
54 |
static final Class<IllegalStateException> ISE = IllegalStateException.class; |
|
55 |
static final Class<NumberFormatException> NFE = NumberFormatException.class; |
|
56 |
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class; |
|
57 |
||
58 |
@Test |
|
59 |
public void testDefaults() { |
|
60 |
List<HttpRequest.Builder> builders = List.of(newBuilder().uri(uri), |
|
61 |
newBuilder(uri)); |
|
62 |
for (HttpRequest.Builder builder : builders) { |
|
63 |
assertFalse(builder.build().expectContinue()); |
|
64 |
assertEquals(builder.build().method(), "GET"); |
|
65 |
assertFalse(builder.build().bodyPublisher().isPresent()); |
|
66 |
assertFalse(builder.build().version().isPresent()); |
|
67 |
assertFalse(builder.build().timeout().isPresent()); |
|
68 |
assertTrue(builder.build().headers() != null); |
|
69 |
assertEquals(builder.build().headers().map().size(), 0); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
@Test |
|
74 |
public void testNull() { |
|
75 |
HttpRequest.Builder builder = newBuilder(); |
|
76 |
||
77 |
assertThrows(NPE, () -> newBuilder(null).build()); |
|
78 |
assertThrows(NPE, () -> newBuilder(uri).uri(null).build()); |
|
79 |
assertThrows(NPE, () -> builder.uri(null)); |
|
80 |
assertThrows(NPE, () -> builder.version(null)); |
|
81 |
assertThrows(NPE, () -> builder.header(null, null)); |
|
82 |
assertThrows(NPE, () -> builder.header("name", null)); |
|
83 |
assertThrows(NPE, () -> builder.header(null, "value")); |
|
84 |
assertThrows(NPE, () -> builder.headers(null)); |
|
85 |
assertThrows(NPE, () -> builder.headers(new String[] { null, null })); |
|
86 |
assertThrows(NPE, () -> builder.headers(new String[] { "name", null })); |
|
87 |
assertThrows(NPE, () -> builder.headers(new String[] { null, "value" })); |
|
88 |
assertThrows(NPE, () -> builder.method(null, null)); |
|
89 |
assertThrows(NPE, () -> builder.method("GET", null)); |
|
90 |
assertThrows(NPE, () -> builder.method("POST", null)); |
|
91 |
assertThrows(NPE, () -> builder.method("PUT", null)); |
|
92 |
assertThrows(NPE, () -> builder.method("DELETE", null)); |
|
93 |
assertThrows(NPE, () -> builder.setHeader(null, null)); |
|
94 |
assertThrows(NPE, () -> builder.setHeader("name", null)); |
|
95 |
assertThrows(NPE, () -> builder.setHeader(null, "value")); |
|
96 |
assertThrows(NPE, () -> builder.timeout(null)); |
|
97 |
assertThrows(NPE, () -> builder.DELETE(null)); |
|
98 |
assertThrows(NPE, () -> builder.POST(null)); |
|
99 |
assertThrows(NPE, () -> builder.PUT(null)); |
|
100 |
} |
|
101 |
||
102 |
@Test |
|
103 |
public void testURI() { |
|
104 |
assertThrows(ISE, () -> newBuilder().build()); |
|
105 |
List<URI> uris = List.of( |
|
106 |
URI.create("ws://foo.com"), |
|
107 |
URI.create("wss://foo.com"), |
|
108 |
URI.create("ftp://foo.com"), |
|
109 |
URI.create("gopher://foo.com"), |
|
110 |
URI.create("mailto:a@b.com"), |
|
111 |
URI.create("scheme:example.com"), |
|
112 |
URI.create("scheme:example.com"), |
|
113 |
URI.create("scheme:example.com/path"), |
|
114 |
URI.create("path"), |
|
115 |
URI.create("/path") |
|
116 |
); |
|
117 |
for (URI u : uris) { |
|
118 |
assertThrows(IAE, () -> newBuilder(u)); |
|
119 |
assertThrows(IAE, () -> newBuilder().uri(u)); |
|
120 |
} |
|
121 |
||
122 |
assertEquals(newBuilder(uri).build().uri(), uri); |
|
123 |
assertEquals(newBuilder().uri(uri).build().uri(), uri); |
|
124 |
URI https = URI.create("https://foo.com"); |
|
125 |
assertEquals(newBuilder(https).build().uri(), https); |
|
126 |
assertEquals(newBuilder().uri(https).build().uri(), https); |
|
127 |
} |
|
128 |
||
129 |
@Test |
|
130 |
public void testMethod() { |
|
131 |
HttpRequest request = newBuilder(uri).build(); |
|
132 |
assertEquals(request.method(), "GET"); |
|
133 |
assertTrue(!request.bodyPublisher().isPresent()); |
|
134 |
||
135 |
request = newBuilder(uri).GET().build(); |
|
136 |
assertEquals(request.method(), "GET"); |
|
137 |
assertTrue(!request.bodyPublisher().isPresent()); |
|
138 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
139 |
request = newBuilder(uri).POST(BodyPublishers.ofString("")).GET().build(); |
48083 | 140 |
assertEquals(request.method(), "GET"); |
141 |
assertTrue(!request.bodyPublisher().isPresent()); |
|
142 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
143 |
request = newBuilder(uri).PUT(BodyPublishers.ofString("")).GET().build(); |
48083 | 144 |
assertEquals(request.method(), "GET"); |
145 |
assertTrue(!request.bodyPublisher().isPresent()); |
|
146 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
147 |
request = newBuilder(uri).DELETE(BodyPublishers.ofString("")).GET().build(); |
48083 | 148 |
assertEquals(request.method(), "GET"); |
149 |
assertTrue(!request.bodyPublisher().isPresent()); |
|
150 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
151 |
request = newBuilder(uri).POST(BodyPublishers.ofString("")).build(); |
48083 | 152 |
assertEquals(request.method(), "POST"); |
153 |
assertTrue(request.bodyPublisher().isPresent()); |
|
154 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
155 |
request = newBuilder(uri).PUT(BodyPublishers.ofString("")).build(); |
48083 | 156 |
assertEquals(request.method(), "PUT"); |
157 |
assertTrue(request.bodyPublisher().isPresent()); |
|
158 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
159 |
request = newBuilder(uri).DELETE(BodyPublishers.ofString("")).build(); |
48083 | 160 |
assertEquals(request.method(), "DELETE"); |
161 |
assertTrue(request.bodyPublisher().isPresent()); |
|
162 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
163 |
request = newBuilder(uri).GET().POST(BodyPublishers.ofString("")).build(); |
48083 | 164 |
assertEquals(request.method(), "POST"); |
165 |
assertTrue(request.bodyPublisher().isPresent()); |
|
166 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
167 |
request = newBuilder(uri).GET().PUT(BodyPublishers.ofString("")).build(); |
48083 | 168 |
assertEquals(request.method(), "PUT"); |
169 |
assertTrue(request.bodyPublisher().isPresent()); |
|
170 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
171 |
request = newBuilder(uri).GET().DELETE(BodyPublishers.ofString("")).build(); |
48083 | 172 |
assertEquals(request.method(), "DELETE"); |
173 |
assertTrue(request.bodyPublisher().isPresent()); |
|
174 |
||
175 |
// CONNECT is disallowed in the implementation, since it is used for |
|
176 |
// tunneling, and is handled separately for security checks. |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
177 |
assertThrows(IAE, () -> newBuilder(uri).method("CONNECT", BodyPublishers.noBody()).build()); |
48083 | 178 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
179 |
request = newBuilder(uri).method("GET", BodyPublishers.noBody()).build(); |
48083 | 180 |
assertEquals(request.method(), "GET"); |
181 |
assertTrue(request.bodyPublisher().isPresent()); |
|
182 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
183 |
request = newBuilder(uri).method("POST", BodyPublishers.ofString("")).build(); |
48083 | 184 |
assertEquals(request.method(), "POST"); |
185 |
assertTrue(request.bodyPublisher().isPresent()); |
|
186 |
} |
|
187 |
||
188 |
@Test |
|
189 |
public void testHeaders() { |
|
190 |
HttpRequest.Builder builder = newBuilder(uri); |
|
191 |
||
192 |
String[] empty = new String[0]; |
|
193 |
assertThrows(IAE, () -> builder.headers(empty).build()); |
|
194 |
assertThrows(IAE, () -> builder.headers("1").build()); |
|
195 |
assertThrows(IAE, () -> builder.headers("1", "2", "3").build()); |
|
196 |
assertThrows(IAE, () -> builder.headers("1", "2", "3", "4", "5").build()); |
|
197 |
assertEquals(builder.build().headers().map().size(),0); |
|
198 |
||
199 |
List<HttpRequest> requests = List.of( |
|
200 |
// same header built from different combinations of the API |
|
201 |
newBuilder(uri).header("A", "B").build(), |
|
202 |
newBuilder(uri).headers("A", "B").build(), |
|
203 |
newBuilder(uri).setHeader("A", "B").build(), |
|
204 |
newBuilder(uri).header("A", "F").setHeader("A", "B").build(), |
|
205 |
newBuilder(uri).headers("A", "F").setHeader("A", "B").build() |
|
206 |
); |
|
207 |
||
208 |
for (HttpRequest r : requests) { |
|
209 |
assertEquals(r.headers().map().size(), 1); |
|
210 |
assertTrue(r.headers().firstValue("A").isPresent()); |
|
211 |
assertEquals(r.headers().firstValue("A").get(), "B"); |
|
212 |
assertEquals(r.headers().allValues("A"), List.of("B")); |
|
213 |
assertEquals(r.headers().allValues("C").size(), 0); |
|
214 |
assertEquals(r.headers().map().get("A"), List.of("B")); |
|
215 |
assertThrows(NFE, () -> r.headers().firstValueAsLong("A")); |
|
216 |
assertFalse(r.headers().firstValue("C").isPresent()); |
|
217 |
// a non-exhaustive list of mutators |
|
218 |
assertThrows(UOE, () -> r.headers().map().put("Z", List.of("Z"))); |
|
219 |
assertThrows(UOE, () -> r.headers().map().remove("A")); |
|
220 |
assertThrows(UOE, () -> r.headers().map().remove("A", "B")); |
|
221 |
assertThrows(UOE, () -> r.headers().map().clear()); |
|
222 |
assertThrows(UOE, () -> r.headers().allValues("A").remove("B")); |
|
223 |
assertThrows(UOE, () -> r.headers().allValues("A").remove(1)); |
|
224 |
assertThrows(UOE, () -> r.headers().allValues("A").clear()); |
|
225 |
assertThrows(UOE, () -> r.headers().allValues("A").add("Z")); |
|
226 |
assertThrows(UOE, () -> r.headers().allValues("A").addAll(List.of("Z"))); |
|
227 |
assertThrows(UOE, () -> r.headers().allValues("A").add(1, "Z")); |
|
228 |
} |
|
229 |
||
230 |
requests = List.of( |
|
231 |
// same headers built from different combinations of the API |
|
232 |
newBuilder(uri).header("A", "B") |
|
233 |
.header("C", "D").build(), |
|
234 |
newBuilder(uri).header("A", "B") |
|
235 |
.headers("C", "D").build(), |
|
236 |
newBuilder(uri).header("A", "B") |
|
237 |
.setHeader("C", "D").build(), |
|
238 |
newBuilder(uri).headers("A", "B") |
|
239 |
.headers("C", "D").build(), |
|
240 |
newBuilder(uri).headers("A", "B") |
|
241 |
.header("C", "D").build(), |
|
242 |
newBuilder(uri).headers("A", "B") |
|
243 |
.setHeader("C", "D").build(), |
|
244 |
newBuilder(uri).setHeader("A", "B") |
|
245 |
.setHeader("C", "D").build(), |
|
246 |
newBuilder(uri).setHeader("A", "B") |
|
247 |
.header("C", "D").build(), |
|
248 |
newBuilder(uri).setHeader("A", "B") |
|
249 |
.headers("C", "D").build(), |
|
250 |
newBuilder(uri).headers("A", "B", "C", "D").build() |
|
251 |
); |
|
252 |
||
253 |
for (HttpRequest r : requests) { |
|
254 |
assertEquals(r.headers().map().size(), 2); |
|
255 |
assertTrue(r.headers().firstValue("A").isPresent()); |
|
256 |
assertEquals(r.headers().firstValue("A").get(), "B"); |
|
257 |
assertEquals(r.headers().allValues("A"), List.of("B")); |
|
258 |
assertTrue(r.headers().firstValue("C").isPresent()); |
|
259 |
assertEquals(r.headers().firstValue("C").get(), "D"); |
|
260 |
assertEquals(r.headers().allValues("C"), List.of("D")); |
|
261 |
assertEquals(r.headers().map().get("C"), List.of("D")); |
|
262 |
assertThrows(NFE, () -> r.headers().firstValueAsLong("C")); |
|
263 |
assertFalse(r.headers().firstValue("E").isPresent()); |
|
264 |
// a smaller non-exhaustive list of mutators |
|
265 |
assertThrows(UOE, () -> r.headers().map().put("Z", List.of("Z"))); |
|
266 |
assertThrows(UOE, () -> r.headers().map().remove("C")); |
|
267 |
assertThrows(UOE, () -> r.headers().allValues("A").remove("B")); |
|
268 |
assertThrows(UOE, () -> r.headers().allValues("A").clear()); |
|
269 |
assertThrows(UOE, () -> r.headers().allValues("C").add("Z")); |
|
270 |
} |
|
271 |
||
272 |
requests = List.of( |
|
273 |
// same multi-value headers built from different combinations of the API |
|
274 |
newBuilder(uri).header("A", "B") |
|
275 |
.header("A", "C").build(), |
|
276 |
newBuilder(uri).header("A", "B") |
|
277 |
.headers("A", "C").build(), |
|
278 |
newBuilder(uri).headers("A", "B") |
|
279 |
.headers("A", "C").build(), |
|
280 |
newBuilder(uri).headers("A", "B") |
|
281 |
.header("A", "C").build(), |
|
282 |
newBuilder(uri).setHeader("A", "B") |
|
283 |
.header("A", "C").build(), |
|
284 |
newBuilder(uri).setHeader("A", "B") |
|
285 |
.headers("A", "C").build(), |
|
286 |
newBuilder(uri).header("A", "D") |
|
287 |
.setHeader("A", "B") |
|
288 |
.headers("A", "C").build(), |
|
289 |
newBuilder(uri).headers("A", "B", "A", "C").build() |
|
290 |
); |
|
291 |
||
292 |
for (HttpRequest r : requests) { |
|
293 |
assertEquals(r.headers().map().size(), 1); |
|
294 |
assertTrue(r.headers().firstValue("A").isPresent()); |
|
295 |
assertTrue(r.headers().allValues("A").containsAll(List.of("B", "C"))); |
|
296 |
assertEquals(r.headers().allValues("C").size(), 0); |
|
297 |
assertEquals(r.headers().map().get("A"), List.of("B", "C")); |
|
298 |
assertThrows(NFE, () -> r.headers().firstValueAsLong("A")); |
|
299 |
assertFalse(r.headers().firstValue("C").isPresent()); |
|
300 |
// a non-exhaustive list of mutators |
|
301 |
assertThrows(UOE, () -> r.headers().map().put("Z", List.of("Z"))); |
|
302 |
assertThrows(UOE, () -> r.headers().map().remove("A")); |
|
303 |
assertThrows(UOE, () -> r.headers().map().remove("A", "B")); |
|
304 |
assertThrows(UOE, () -> r.headers().map().clear()); |
|
305 |
assertThrows(UOE, () -> r.headers().allValues("A").remove("B")); |
|
306 |
assertThrows(UOE, () -> r.headers().allValues("A").remove(1)); |
|
307 |
assertThrows(UOE, () -> r.headers().allValues("A").clear()); |
|
308 |
assertThrows(UOE, () -> r.headers().allValues("A").add("Z")); |
|
309 |
assertThrows(UOE, () -> r.headers().allValues("A").addAll(List.of("Z"))); |
|
310 |
assertThrows(UOE, () -> r.headers().allValues("A").add(1, "Z")); |
|
311 |
} |
|
312 |
} |
|
313 |
||
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
314 |
private static final Set<String> RESTRICTED = Set.of("connection", "content-length", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
315 |
"date", "expect", "from", "host", "origin", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
316 |
"referer", "upgrade", "via", "warning", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
317 |
"Connection", "Content-Length", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
318 |
"DATE", "eXpect", "frOm", "hosT", "origIN", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
319 |
"ReFerer", "upgradE", "vIa", "Warning", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
320 |
"CONNection", "CONTENT-LENGTH", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
321 |
"Date", "EXPECT", "From", "Host", "Origin", |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56033
diff
changeset
|
322 |
"Referer", "Upgrade", "Via", "WARNING"); |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
323 |
|
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
324 |
interface WithHeader { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
325 |
HttpRequest.Builder withHeader(HttpRequest.Builder builder, String name, String value); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
326 |
} |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
327 |
|
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
328 |
@Test |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
329 |
public void testRestricted() throws URISyntaxException { |
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56167
diff
changeset
|
330 |
URI uri = new URI("http://localhost:80/test/"); |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
331 |
Map<String, WithHeader> lambdas = Map.of( |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
332 |
"Builder::header", HttpRequest.Builder::header, |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
333 |
"Builder::headers", (b, n, v) -> b.headers(n,v), |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
334 |
"Builder::setHeader", HttpRequest.Builder::setHeader |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
335 |
); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
336 |
for (Map.Entry<String, WithHeader> e : lambdas.entrySet()) { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
337 |
System.out.println("Testing restricted headers with " + e.getKey()); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
338 |
WithHeader f = e.getValue(); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
339 |
for (String name : RESTRICTED) { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
340 |
String value = name + "-value"; |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
341 |
HttpRequest req = f.withHeader(HttpRequest.newBuilder(uri) |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
342 |
.GET(), "x-" + name, value).build(); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
343 |
String v = req.headers().firstValue("x-" + name).orElseThrow( |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
344 |
() -> new RuntimeException("header x-" + name + " not set")); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
345 |
assertEquals(v, value); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
346 |
try { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
347 |
f.withHeader(HttpRequest.newBuilder(uri) |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
348 |
.GET(), name, value).build(); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
349 |
throw new RuntimeException("Expected IAE not thrown for " + name); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
350 |
} catch (IllegalArgumentException x) { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
351 |
System.out.println("Got expected IAE for " + name + ": " + x); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
352 |
} |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
353 |
} |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
354 |
} |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
355 |
} |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
356 |
|
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
55973
diff
changeset
|
357 |
|
48083 | 358 |
@Test |
359 |
public void testCopy() { |
|
360 |
HttpRequest.Builder builder = newBuilder(uri).expectContinue(true) |
|
361 |
.header("A", "B") |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
362 |
.POST(BodyPublishers.ofString("")) |
48083 | 363 |
.timeout(ofSeconds(30)) |
364 |
.version(HTTP_1_1); |
|
365 |
HttpRequest.Builder copy = builder.copy(); |
|
366 |
assertTrue(builder != copy); |
|
367 |
||
368 |
// modify the original builder before building from the copy |
|
369 |
builder.GET().timeout(ofSeconds(5)).version(HTTP_2).setHeader("A", "C"); |
|
370 |
||
371 |
HttpRequest copyRequest = copy.build(); |
|
372 |
assertEquals(copyRequest.uri(), uri); |
|
373 |
assertEquals(copyRequest.expectContinue(), true); |
|
374 |
assertEquals(copyRequest.headers().map().get("A"), List.of("B")); |
|
375 |
assertEquals(copyRequest.method(), "POST"); |
|
376 |
assertEquals(copyRequest.bodyPublisher().isPresent(), true); |
|
377 |
assertEquals(copyRequest.timeout().get(), ofSeconds(30)); |
|
378 |
assertTrue(copyRequest.version().isPresent()); |
|
379 |
assertEquals(copyRequest.version().get(), HTTP_1_1); |
|
380 |
} |
|
381 |
||
382 |
@Test |
|
383 |
public void testTimeout() { |
|
384 |
HttpRequest.Builder builder = newBuilder(uri); |
|
385 |
assertThrows(IAE, () -> builder.timeout(ZERO)); |
|
386 |
assertThrows(IAE, () -> builder.timeout(ofSeconds(0))); |
|
387 |
assertThrows(IAE, () -> builder.timeout(ofSeconds(-1))); |
|
388 |
assertThrows(IAE, () -> builder.timeout(ofNanos(-100))); |
|
389 |
assertEquals(builder.timeout(ofNanos(15)).build().timeout().get(), ofNanos(15)); |
|
390 |
assertEquals(builder.timeout(ofSeconds(50)).build().timeout().get(), ofSeconds(50)); |
|
391 |
assertEquals(builder.timeout(ofMinutes(30)).build().timeout().get(), ofMinutes(30)); |
|
392 |
} |
|
393 |
||
394 |
@Test |
|
395 |
public void testExpect() { |
|
396 |
HttpRequest.Builder builder = newBuilder(uri); |
|
397 |
assertEquals(builder.build().expectContinue(), false); |
|
398 |
assertEquals(builder.expectContinue(true).build().expectContinue(), true); |
|
399 |
assertEquals(builder.expectContinue(false).build().expectContinue(), false); |
|
400 |
assertEquals(builder.expectContinue(true).build().expectContinue(), true); |
|
401 |
} |
|
402 |
||
403 |
@Test |
|
404 |
public void testEquals() { |
|
405 |
assertNotEquals(newBuilder(URI.create("http://foo.com")), |
|
406 |
newBuilder(URI.create("http://bar.com"))); |
|
407 |
||
408 |
HttpRequest.Builder builder = newBuilder(uri); |
|
409 |
assertEquals(builder.build(), builder.build()); |
|
410 |
assertEquals(builder.build(), newBuilder(uri).build()); |
|
411 |
||
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
412 |
builder.POST(BodyPublishers.noBody()); |
48083 | 413 |
assertEquals(builder.build(), builder.build()); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
414 |
assertEquals(builder.build(), newBuilder(uri).POST(BodyPublishers.noBody()).build()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
415 |
assertEquals(builder.build(), newBuilder(uri).POST(BodyPublishers.ofString("")).build()); |
48083 | 416 |
assertNotEquals(builder.build(), newBuilder(uri).build()); |
417 |
assertNotEquals(builder.build(), newBuilder(uri).GET().build()); |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56089
diff
changeset
|
418 |
assertNotEquals(builder.build(), newBuilder(uri).PUT(BodyPublishers.noBody()).build()); |
48083 | 419 |
|
420 |
builder = newBuilder(uri).header("x", "y"); |
|
421 |
assertEquals(builder.build(), builder.build()); |
|
422 |
assertEquals(builder.build(), newBuilder(uri).header("x", "y").build()); |
|
423 |
assertNotEquals(builder.build(), newBuilder(uri).header("x", "Z").build()); |
|
424 |
assertNotEquals(builder.build(), newBuilder(uri).header("z", "y").build()); |
|
425 |
} |
|
426 |
} |