author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 55973 | 4d9b002587db |
child 56167 | 96fa4f49a9ff |
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) 2016, 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.net.URI; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
25 |
import java.net.http.HttpClient; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
import java.time.Duration; |
48083 | 27 |
import java.util.Arrays; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.util.function.BiFunction; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.util.function.Function; |
48083 | 30 |
import java.util.function.Supplier; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.util.stream.Collectors; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.util.stream.Stream; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
33 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
34 |
import static java.net.http.HttpRequest.BodyPublisher.fromString; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
35 |
import static java.net.http.HttpRequest.BodyPublisher.noBody; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
* @test |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
* @bug 8170064 |
48083 | 40 |
* @summary HttpRequest[.Builder] API and behaviour checks |
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 |
public class HttpRequestBuilderTest { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
static final URI TEST_URI = URI.create("http://www.foo.com/"); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
public static void main(String[] args) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
48083 | 49 |
test0("newBuilder().build()", |
50 |
() -> HttpRequest.newBuilder().build(), |
|
51 |
IllegalStateException.class); |
|
52 |
||
53 |
test0("newBuilder(null)", |
|
54 |
() -> HttpRequest.newBuilder(null), |
|
55 |
NullPointerException.class); |
|
56 |
||
57 |
test0("newBuilder(URI.create(\"badScheme://www.foo.com/\")", |
|
58 |
() -> HttpRequest.newBuilder(URI.create("badScheme://www.foo.com/")), |
|
59 |
IllegalArgumentException.class); |
|
60 |
||
61 |
test0("newBuilder(URI.create(\"http://www.foo.com:-1/\")", |
|
62 |
() -> HttpRequest.newBuilder(URI.create("http://www.foo.com:-1/")), |
|
63 |
IllegalArgumentException.class); |
|
64 |
||
65 |
test0("newBuilder(URI.create(\"https://www.foo.com:-1/\")", |
|
66 |
() -> HttpRequest.newBuilder(URI.create("https://www.foo.com:-1/")), |
|
67 |
IllegalArgumentException.class); |
|
68 |
||
69 |
test0("newBuilder(" + TEST_URI + ").uri(null)", |
|
70 |
() -> HttpRequest.newBuilder(TEST_URI).uri(null), |
|
71 |
NullPointerException.class); |
|
72 |
||
73 |
test0("newBuilder(uri).build()", |
|
74 |
() -> HttpRequest.newBuilder(TEST_URI).build() |
|
75 |
/* no expected exceptions */ ); |
|
76 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
HttpRequest.Builder builder = HttpRequest.newBuilder(); |
48083 | 78 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
builder = test1("uri", builder, builder::uri, (URI)null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
NullPointerException.class); |
48083 | 81 |
|
82 |
builder = test1("uri", builder, builder::uri, URI.create("http://www.foo.com:-1/"), |
|
83 |
IllegalArgumentException.class); |
|
84 |
||
85 |
builder = test1("uri", builder, builder::uri, URI.create("https://www.foo.com:-1/"), |
|
86 |
IllegalArgumentException.class); |
|
87 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
builder = test2("header", builder, builder::header, (String) null, "bar", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
NullPointerException.class); |
48083 | 90 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
builder = test2("header", builder, builder::header, "foo", (String) null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
NullPointerException.class); |
48083 | 93 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
builder = test2("header", builder, builder::header, (String)null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
(String) null, NullPointerException.class); |
48083 | 96 |
|
97 |
builder = test2("header", builder, builder::header, "", "bar", |
|
98 |
IllegalArgumentException.class); |
|
99 |
||
100 |
builder = test2("header", builder, builder::header, "foo", "\r", |
|
101 |
IllegalArgumentException.class); |
|
102 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
builder = test1("headers", builder, builder::headers, (String[]) null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
NullPointerException.class); |
48083 | 105 |
|
106 |
builder = test1("headers", builder, builder::headers, new String[0], |
|
107 |
IllegalArgumentException.class); |
|
108 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
builder = test1("headers", builder, builder::headers, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
(String[]) new String[] {null, "bar"}, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
NullPointerException.class); |
48083 | 112 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
builder = test1("headers", builder, builder::headers, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
(String[]) new String[] {"foo", null}, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
NullPointerException.class); |
48083 | 116 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
builder = test1("headers", builder, builder::headers, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
(String[]) new String[] {null, null}, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
NullPointerException.class); |
48083 | 120 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
builder = test1("headers", builder, builder::headers, |
48083 | 122 |
(String[]) new String[] {"foo", "bar", null}, |
123 |
NullPointerException.class, |
|
124 |
IllegalArgumentException.class); |
|
125 |
||
126 |
builder = test1("headers", builder, builder::headers, |
|
127 |
(String[]) new String[] {"foo", "bar", null, null}, |
|
128 |
NullPointerException.class); |
|
129 |
||
130 |
builder = test1("headers", builder, builder::headers, |
|
131 |
(String[]) new String[] {"foo", "bar", "baz", null}, |
|
132 |
NullPointerException.class); |
|
133 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
builder = test1("headers", builder, builder::headers, |
48083 | 135 |
(String[]) new String[] {"foo", "bar", "\r", "baz"}, |
136 |
IllegalArgumentException.class); |
|
137 |
||
138 |
builder = test1("headers", builder, builder::headers, |
|
139 |
(String[]) new String[] {"foo", "bar", "baz", "\n"}, |
|
140 |
IllegalArgumentException.class); |
|
141 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
builder = test1("headers", builder, builder::headers, |
48083 | 143 |
(String[]) new String[] {"foo", "bar", "", "baz"}, |
144 |
IllegalArgumentException.class); |
|
145 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
builder = test1("headers", builder, builder::headers, |
48083 | 147 |
(String[]) new String[] {"foo", "bar", null, "baz"}, |
148 |
NullPointerException.class); |
|
149 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
builder = test1("headers", builder, builder::headers, |
48083 | 151 |
(String[]) new String[] {"foo", "bar", "baz"}, |
152 |
IllegalArgumentException.class); |
|
153 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
builder = test1("headers", builder, builder::headers, |
48083 | 155 |
(String[]) new String[] {"foo"}, |
156 |
IllegalArgumentException.class); |
|
157 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
builder = test1("DELETE", builder, builder::DELETE, |
48083 | 159 |
noBody(), null); |
160 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
builder = test1("POST", builder, builder::POST, |
48083 | 162 |
noBody(), null); |
163 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
builder = test1("PUT", builder, builder::PUT, |
48083 | 165 |
noBody(), null); |
166 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
builder = test2("method", builder, builder::method, "GET", |
48083 | 168 |
noBody(), null); |
169 |
||
170 |
builder = test1("DELETE", builder, builder::DELETE, |
|
171 |
(HttpRequest.BodyPublisher)null, |
|
172 |
NullPointerException.class); |
|
173 |
||
174 |
builder = test1("POST", builder, builder::POST, |
|
175 |
(HttpRequest.BodyPublisher)null, |
|
176 |
NullPointerException.class); |
|
177 |
||
178 |
builder = test1("PUT", builder, builder::PUT, |
|
179 |
(HttpRequest.BodyPublisher)null, |
|
180 |
NullPointerException.class); |
|
181 |
||
182 |
builder = test2("method", builder, builder::method, "GET", |
|
183 |
(HttpRequest.BodyPublisher) null, |
|
184 |
NullPointerException.class); |
|
185 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
builder = test2("setHeader", builder, builder::setHeader, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
(String) null, "bar", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
NullPointerException.class); |
48083 | 189 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
builder = test2("setHeader", builder, builder::setHeader, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
"foo", (String) null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
NullPointerException.class); |
48083 | 193 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
builder = test2("setHeader", builder, builder::setHeader, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
(String)null, (String) null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
NullPointerException.class); |
48083 | 197 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
builder = test1("timeout", builder, builder::timeout, |
48083 | 199 |
(Duration)null, |
200 |
NullPointerException.class); |
|
201 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
builder = test1("version", builder, builder::version, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
(HttpClient.Version)null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
NullPointerException.class); |
48083 | 205 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
builder = test2("method", builder, builder::method, null, |
48083 | 207 |
fromString("foo"), |
208 |
NullPointerException.class); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
// see JDK-8170093 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
// |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
// builder = test2("method", builder, builder::method, "foo", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
// HttpRequest.BodyProcessor.fromString("foo"), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
// IllegalArgumentException.class); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
// |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
// builder.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
|
48083 | 217 |
|
218 |
method("newBuilder(TEST_URI).build().method() == GET", |
|
219 |
() -> HttpRequest.newBuilder(TEST_URI), |
|
220 |
"GET"); |
|
221 |
||
222 |
method("newBuilder(TEST_URI).GET().build().method() == GET", |
|
223 |
() -> HttpRequest.newBuilder(TEST_URI).GET(), |
|
224 |
"GET"); |
|
225 |
||
226 |
method("newBuilder(TEST_URI).POST(fromString(\"\")).GET().build().method() == GET", |
|
227 |
() -> HttpRequest.newBuilder(TEST_URI).POST(fromString("")).GET(), |
|
228 |
"GET"); |
|
229 |
||
230 |
method("newBuilder(TEST_URI).PUT(fromString(\"\")).GET().build().method() == GET", |
|
231 |
() -> HttpRequest.newBuilder(TEST_URI).PUT(fromString("")).GET(), |
|
232 |
"GET"); |
|
233 |
||
234 |
method("newBuilder(TEST_URI).DELETE(fromString(\"\")).GET().build().method() == GET", |
|
235 |
() -> HttpRequest.newBuilder(TEST_URI).DELETE(fromString("")).GET(), |
|
236 |
"GET"); |
|
237 |
||
238 |
method("newBuilder(TEST_URI).POST(fromString(\"\")).build().method() == POST", |
|
239 |
() -> HttpRequest.newBuilder(TEST_URI).POST(fromString("")), |
|
240 |
"POST"); |
|
241 |
||
242 |
method("newBuilder(TEST_URI).PUT(fromString(\"\")).build().method() == PUT", |
|
243 |
() -> HttpRequest.newBuilder(TEST_URI).PUT(fromString("")), |
|
244 |
"PUT"); |
|
245 |
||
246 |
method("newBuilder(TEST_URI).DELETE(fromString(\"\")).build().method() == DELETE", |
|
247 |
() -> HttpRequest.newBuilder(TEST_URI).DELETE(fromString("")), |
|
248 |
"DELETE"); |
|
249 |
||
250 |
method("newBuilder(TEST_URI).GET().POST(fromString(\"\")).build().method() == POST", |
|
251 |
() -> HttpRequest.newBuilder(TEST_URI).GET().POST(fromString("")), |
|
252 |
"POST"); |
|
253 |
||
254 |
method("newBuilder(TEST_URI).GET().PUT(fromString(\"\")).build().method() == PUT", |
|
255 |
() -> HttpRequest.newBuilder(TEST_URI).GET().PUT(fromString("")), |
|
256 |
"PUT"); |
|
257 |
||
258 |
method("newBuilder(TEST_URI).GET().DELETE(fromString(\"\")).build().method() == DELETE", |
|
259 |
() -> HttpRequest.newBuilder(TEST_URI).GET().DELETE(fromString("")), |
|
260 |
"DELETE"); |
|
261 |
||
262 |
||
263 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
private static boolean shouldFail(Class<? extends Exception> ...exceptions) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
return exceptions != null && exceptions.length > 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
private static String expectedNames(Class<? extends Exception> ...exceptions) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
return Stream.of(exceptions).map(Class::getSimpleName) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
.collect(Collectors.joining("|")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
private static boolean isExpected(Exception x, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
Class<? extends Exception> ...expected) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
return expected != null && Stream.of(expected) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
.filter(c -> c.isInstance(x)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
278 |
.findAny().isPresent(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
|
48083 | 281 |
static void method(String name, |
282 |
Supplier<HttpRequest.Builder> supplier, |
|
283 |
String expectedMethod) { |
|
284 |
HttpRequest request = supplier.get().build(); |
|
285 |
String method = request.method(); |
|
286 |
if (request.method().equals("GET") && request.bodyPublisher().isPresent()) |
|
287 |
throw new AssertionError("failed: " + name |
|
288 |
+ ". Unexpected body processor for GET: " |
|
289 |
+ request.bodyPublisher().get()); |
|
290 |
||
291 |
if (expectedMethod.equals(method)) { |
|
292 |
System.out.println("success: " + name); |
|
293 |
} else { |
|
294 |
throw new AssertionError("failed: " + name |
|
295 |
+ ". Expected " + expectedMethod + ", got " + method); |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
static void test0(String name, |
|
300 |
Runnable r, |
|
301 |
Class<? extends Exception> ...ex) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
try { |
48083 | 303 |
r.run(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
if (!shouldFail(ex)) { |
48083 | 305 |
System.out.println("success: " + name); |
306 |
return; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
throw new AssertionError("Expected " + expectedNames(ex) |
48083 | 309 |
+ " not raised for " + name); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
} catch (Exception x) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
if (!isExpected(x, ex)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
throw x; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
} else { |
48083 | 315 |
System.out.println("success: " + name + |
316 |
" - Got expected exception: " + x); |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
public static <R,P> R test1(String name, R receiver, Function<P, R> m, P arg, |
|
322 |
Class<? extends Exception> ...ex) { |
|
323 |
String argMessage = arg == null ? "null" : arg.toString(); |
|
324 |
if (arg instanceof String[]) { |
|
325 |
argMessage = Arrays.asList((String[])arg).toString(); |
|
326 |
} |
|
327 |
try { |
|
328 |
R result = m.apply(arg); |
|
329 |
if (!shouldFail(ex)) { |
|
330 |
System.out.println("success: " + name + "(" + argMessage + ")"); |
|
331 |
return result; |
|
332 |
} else { |
|
333 |
throw new AssertionError("Expected " + expectedNames(ex) |
|
334 |
+ " not raised for " + name + "(" + argMessage + ")"); |
|
335 |
} |
|
336 |
} catch (Exception x) { |
|
337 |
if (!isExpected(x, ex)) { |
|
338 |
throw x; |
|
339 |
} else { |
|
340 |
System.out.println("success: " + name + "(" + argMessage + ")" + |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
" - Got expected exception: " + x); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
return receiver; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
public static <R,P1, P2> R test2(String name, R receiver, BiFunction<P1, P2, R> m, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
P1 arg1, P2 arg2, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
Class<? extends Exception> ...ex) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
R result = m.apply(arg1, arg2); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
if (!shouldFail(ex)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
System.out.println("success: " + name + "(" + arg1 + ", " |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
+ arg2 + ")"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
return result; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
throw new AssertionError("Expected " + expectedNames(ex) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
+ " not raised for " |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
+ name + "(" + arg1 +", " + arg2 + ")"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} catch (Exception x) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
if (!isExpected(x, ex)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
throw x; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
System.out.println("success: " + name + "(" + arg1 + ", " |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
+ arg2 + ") - Got expected exception: " + x); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
return receiver; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
372 |
} |