author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 57880 | ff08db52ad92 |
permissions | -rw-r--r-- |
49765 | 1 |
/* |
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
49765 | 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.io.BufferedReader; |
|
25 |
import java.io.ByteArrayInputStream; |
|
26 |
import java.io.ByteArrayOutputStream; |
|
27 |
import java.io.IOException; |
|
28 |
import java.io.InputStreamReader; |
|
29 |
import java.io.StringReader; |
|
30 |
import java.io.UncheckedIOException; |
|
31 |
import java.net.http.HttpResponse.BodySubscriber; |
|
32 |
import java.net.http.HttpResponse.BodySubscribers; |
|
33 |
import java.nio.ByteBuffer; |
|
34 |
import java.nio.charset.MalformedInputException; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.List; |
|
38 |
import java.util.concurrent.CopyOnWriteArrayList; |
|
39 |
import java.util.concurrent.ExecutionException; |
|
40 |
import java.util.concurrent.Flow; |
|
41 |
import java.util.concurrent.SubmissionPublisher; |
|
42 |
import java.util.function.Supplier; |
|
43 |
import java.util.stream.Collectors; |
|
44 |
import java.util.stream.Stream; |
|
45 |
import org.testng.annotations.Test; |
|
46 |
import static java.nio.charset.StandardCharsets.UTF_8; |
|
47 |
import static java.nio.charset.StandardCharsets.UTF_16; |
|
48 |
import static org.testng.Assert.assertEquals; |
|
49 |
||
50 |
/* |
|
51 |
* @test |
|
52 |
* @summary tests for BodySubscribers returned by fromLineSubscriber. |
|
53 |
* In particular tests that surrogate characters are handled |
|
54 |
* correctly. |
|
55 |
* @modules java.net.http java.logging |
|
56 |
* @run testng/othervm LineSubscribersAndSurrogatesTest |
|
57 |
*/ |
|
58 |
||
59 |
public class LineSubscribersAndSurrogatesTest { |
|
60 |
||
61 |
||
62 |
static final Class<NullPointerException> NPE = NullPointerException.class; |
|
63 |
||
64 |
private static final List<String> lines(String text, String eol) { |
|
65 |
if (eol == null) { |
|
66 |
return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList()); |
|
67 |
} else { |
|
68 |
String replaced = text.replace(eol, "|"); |
|
69 |
int i=0; |
|
70 |
while(replaced.endsWith("||")) { |
|
71 |
replaced = replaced.substring(0,replaced.length()-1); |
|
72 |
i++; |
|
73 |
} |
|
74 |
List<String> res = List.of(replaced.split("\\|")); |
|
75 |
if (i > 0) { |
|
76 |
res = new ArrayList<>(res); |
|
77 |
for (int j=0; j<i; j++) res.add(""); |
|
78 |
} |
|
79 |
return res; |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
84 |
public void testIncomplete() throws Exception { |
49765 | 85 |
// Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00 |
86 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
|
87 |
" les\n\n fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\ud801\udc00"; |
|
88 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
89 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
90 |
subscriber, Supplier::get, UTF_8, null); |
|
91 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
92 |
byte[] sbytes = text.getBytes(UTF_8); |
|
93 |
byte[] bytes = Arrays.copyOfRange(sbytes,0, sbytes.length - 1); |
|
94 |
publisher.subscribe(bodySubscriber); |
|
95 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
96 |
for (int i=0; i<bytes.length; i++) { |
|
97 |
// ensure that surrogates are split over several buffers. |
|
98 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
99 |
} |
|
100 |
publisher.close(); |
|
101 |
try { |
|
102 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
103 |
System.out.println("***** Got: " + resp); |
|
104 |
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
|
105 |
BufferedReader reader = new BufferedReader(new InputStreamReader(bais, UTF_8)); |
|
106 |
String resp2 = reader.lines().collect(Collectors.joining("")); |
|
107 |
assertEquals(resp, resp2); |
|
108 |
assertEquals(subscriber.list, List.of("Bient\u00f4t", |
|
109 |
" nous plongerons", |
|
110 |
" dans", |
|
111 |
" les", |
|
112 |
"", |
|
113 |
" fr\u00f4\ud801\udc00des", |
|
114 |
" t\u00e9n\u00e8bres\ufffd")); |
|
115 |
} catch (ExecutionException x) { |
|
116 |
Throwable cause = x.getCause(); |
|
117 |
if (cause instanceof MalformedInputException) { |
|
118 |
throw new RuntimeException("Unexpected MalformedInputException thrown", cause); |
|
119 |
} |
|
120 |
throw x; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
||
125 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
126 |
public void testStringWithFinisherLF() throws Exception { |
49765 | 127 |
// Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00 |
128 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
|
129 |
" les\n\n fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r"; |
|
130 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
131 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
132 |
subscriber, Supplier::get, UTF_8, "\n"); |
|
133 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
134 |
byte[] bytes = text.getBytes(UTF_8); |
|
135 |
publisher.subscribe(bodySubscriber); |
|
136 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
137 |
for (int i=0; i<bytes.length; i++) { |
|
138 |
// ensure that surrogates are split over several buffers. |
|
139 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
140 |
} |
|
141 |
publisher.close(); |
|
142 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
143 |
System.out.println("***** Got: " + resp); |
|
144 |
List<String> expected = List.of("Bient\u00f4t\r", |
|
145 |
" nous plongerons\r", |
|
146 |
" dans\r les", |
|
147 |
"", |
|
148 |
" fr\u00f4\ud801\udc00des\r", |
|
149 |
" t\u00e9n\u00e8bres\r"); |
|
150 |
assertEquals(subscriber.list, expected); |
|
151 |
assertEquals(resp, Stream.of(text.split("\n")).collect(Collectors.joining(""))); |
|
152 |
assertEquals(resp, expected.stream().collect(Collectors.joining(""))); |
|
153 |
assertEquals(subscriber.list, lines(text, "\n")); |
|
154 |
} |
|
155 |
||
156 |
||
157 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
158 |
public void testStringWithFinisherCR() throws Exception { |
49765 | 159 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
160 |
" les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r"; |
|
161 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
162 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
163 |
subscriber, Supplier::get, UTF_8, "\r"); |
|
164 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
165 |
byte[] bytes = text.getBytes(UTF_8); |
|
166 |
publisher.subscribe(bodySubscriber); |
|
167 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
168 |
for (int i=0; i<bytes.length; i++) { |
|
169 |
// ensure that surrogates are split over several buffers. |
|
170 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
171 |
} |
|
172 |
publisher.close(); |
|
173 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
174 |
System.out.println("***** Got: " + resp); |
|
175 |
assertEquals(resp, text.replace("\r", "")); |
|
176 |
assertEquals(subscriber.list, List.of("Bient\u00f4t", |
|
177 |
"\n nous plongerons", |
|
178 |
"\n dans", |
|
179 |
" les fr\u00f4\ud801\udc00des", |
|
180 |
"\n t\u00e9n\u00e8bres", |
|
181 |
"")); |
|
182 |
assertEquals(subscriber.list, lines(text, "\r")); |
|
183 |
} |
|
184 |
||
185 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
186 |
public void testStringWithFinisherCRLF() throws Exception { |
49765 | 187 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
188 |
" les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres"; |
|
189 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
190 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
191 |
subscriber, Supplier::get, UTF_8, "\r\n"); |
|
192 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
193 |
byte[] bytes = text.getBytes(UTF_8); |
|
194 |
publisher.subscribe(bodySubscriber); |
|
195 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
196 |
for (int i=0; i<bytes.length; i++) { |
|
197 |
// ensure that surrogates are split over several buffers. |
|
198 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
199 |
} |
|
200 |
publisher.close(); |
|
201 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
202 |
System.out.println("***** Got: " + resp); |
|
203 |
assertEquals(resp, text.replace("\r\n","")); |
|
204 |
assertEquals(subscriber.list, List.of("Bient\u00f4t", |
|
205 |
" nous plongerons", |
|
206 |
" dans\r les fr\u00f4\ud801\udc00des", |
|
207 |
" t\u00e9n\u00e8bres")); |
|
208 |
assertEquals(subscriber.list, lines(text, "\r\n")); |
|
209 |
} |
|
210 |
||
211 |
||
212 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
213 |
public void testStringWithFinisherBR() throws Exception { |
49765 | 214 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
215 |
" les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres"; |
|
216 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
217 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
218 |
subscriber, Supplier::get, UTF_8, null); |
|
219 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
220 |
byte[] bytes = text.getBytes(UTF_8); |
|
221 |
publisher.subscribe(bodySubscriber); |
|
222 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
223 |
for (int i=0; i<bytes.length; i++) { |
|
224 |
// ensure that surrogates are split over several buffers. |
|
225 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
226 |
} |
|
227 |
publisher.close(); |
|
228 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
229 |
System.out.println("***** Got: " + resp); |
|
230 |
List<String> expected = List.of("Bient\u00f4t", |
|
231 |
" nous plongerons", |
|
232 |
" dans", |
|
233 |
" les", |
|
234 |
"", |
|
235 |
" fr\u00f4\ud801\udc00des", |
|
236 |
" t\u00e9n\u00e8bres"); |
|
237 |
assertEquals(subscriber.list, expected); |
|
238 |
assertEquals(resp, expected.stream().collect(Collectors.joining(""))); |
|
239 |
assertEquals(subscriber.list, lines(text, null)); |
|
240 |
} |
|
241 |
||
242 |
@Test |
|
57880
ff08db52ad92
8230000: some httpclients testng tests run zero test
dfuchs
parents:
49765
diff
changeset
|
243 |
public void testStringWithFinisherBR_UTF_16() throws Exception { |
49765 | 244 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
245 |
" les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r"; |
|
246 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
247 |
BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber( |
|
248 |
subscriber, Supplier::get, UTF_16, null); |
|
249 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
250 |
byte[] bytes = text.getBytes(UTF_16); |
|
251 |
publisher.subscribe(bodySubscriber); |
|
252 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
253 |
for (int i=0; i<bytes.length; i++) { |
|
254 |
// ensure that surrogates are split over several buffers. |
|
255 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
256 |
} |
|
257 |
publisher.close(); |
|
258 |
String resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
259 |
System.out.println("***** Got: " + resp); |
|
260 |
List<String> expected = List.of("Bient\u00f4t", |
|
261 |
" nous plongerons", |
|
262 |
" dans", |
|
263 |
" les", |
|
264 |
"", |
|
265 |
" fr\u00f4\ud801\udc00des", |
|
266 |
" t\u00e9n\u00e8bres", |
|
267 |
""); |
|
268 |
assertEquals(resp, expected.stream().collect(Collectors.joining(""))); |
|
269 |
assertEquals(subscriber.list, expected); |
|
270 |
assertEquals(subscriber.list, lines(text, null)); |
|
271 |
} |
|
272 |
||
273 |
void testStringWithoutFinisherBR() throws Exception { |
|
274 |
String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" + |
|
275 |
" les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres"; |
|
276 |
ObjectSubscriber subscriber = new ObjectSubscriber(); |
|
277 |
BodySubscriber<Void> bodySubscriber = BodySubscribers.fromLineSubscriber(subscriber); |
|
278 |
SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>(); |
|
279 |
byte[] bytes = text.getBytes(UTF_8); |
|
280 |
publisher.subscribe(bodySubscriber); |
|
281 |
System.out.println("Publishing " + bytes.length + " bytes"); |
|
282 |
for (int i = 0; i < bytes.length; i++) { |
|
283 |
// ensure that surrogates are split over several buffers. |
|
284 |
publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1))); |
|
285 |
} |
|
286 |
publisher.close(); |
|
287 |
Void resp = bodySubscriber.getBody().toCompletableFuture().get(); |
|
288 |
System.out.println("***** Got: " + resp); |
|
289 |
List<String> expected = List.of("Bient\u00f4t", |
|
290 |
" nous plongerons", |
|
291 |
" dans", |
|
292 |
" les", |
|
293 |
"", |
|
294 |
" fr\u00f4\ud801\udc00des", |
|
295 |
" t\u00e9n\u00e8bres"); |
|
296 |
assertEquals(subscriber.text, expected.stream().collect(Collectors.joining(""))); |
|
297 |
assertEquals(subscriber.list, expected); |
|
298 |
assertEquals(subscriber.list, lines(text, null)); |
|
299 |
} |
|
300 |
||
301 |
||
302 |
/** An abstract Subscriber that converts all received data into a String. */ |
|
303 |
static abstract class AbstractSubscriber implements Supplier<String> { |
|
304 |
protected final List<Object> list = new CopyOnWriteArrayList<>(); |
|
305 |
protected volatile Flow.Subscription subscription; |
|
306 |
protected final StringBuilder baos = new StringBuilder(); |
|
307 |
protected volatile String text; |
|
308 |
protected volatile RuntimeException error; |
|
309 |
||
310 |
public void onSubscribe(Flow.Subscription subscription) { |
|
311 |
this.subscription = subscription; |
|
312 |
subscription.request(Long.MAX_VALUE); |
|
313 |
} |
|
314 |
public void onError(Throwable throwable) { |
|
315 |
System.out.println(this + " onError: " + throwable); |
|
316 |
error = new RuntimeException(throwable); |
|
317 |
} |
|
318 |
public void onComplete() { |
|
319 |
System.out.println(this + " onComplete"); |
|
320 |
text = baos.toString(); |
|
321 |
} |
|
322 |
@Override public String get() { |
|
323 |
if (error != null) throw error; |
|
324 |
return text; |
|
325 |
} |
|
326 |
public final List<?> list() { |
|
327 |
return list; |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
static class StringSubscriber extends AbstractSubscriber |
|
332 |
implements Flow.Subscriber<String>, Supplier<String> |
|
333 |
{ |
|
334 |
@Override public void onNext(String item) { |
|
335 |
System.out.println(this + " onNext: \"" |
|
336 |
+ item.replace("\n","\\n") |
|
337 |
.replace("\r", "\\r") |
|
338 |
+ "\""); |
|
339 |
baos.append(item); |
|
340 |
list.add(item); |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
static class CharSequenceSubscriber extends AbstractSubscriber |
|
345 |
implements Flow.Subscriber<CharSequence>, Supplier<String> |
|
346 |
{ |
|
347 |
@Override public void onNext(CharSequence item) { |
|
348 |
System.out.println(this + " onNext: \"" |
|
349 |
+ item.toString().replace("\n","\\n") |
|
350 |
.replace("\r", "\\r") |
|
351 |
+ "\""); |
|
352 |
baos.append(item); |
|
353 |
list.add(item); |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
static class ObjectSubscriber extends AbstractSubscriber |
|
358 |
implements Flow.Subscriber<Object>, Supplier<String> |
|
359 |
{ |
|
360 |
@Override public void onNext(Object item) { |
|
361 |
System.out.println(this + " onNext: \"" |
|
362 |
+ item.toString().replace("\n","\\n") |
|
363 |
.replace("\r", "\\r") |
|
364 |
+ "\""); |
|
365 |
baos.append(item); |
|
366 |
list.add(item); |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
||
371 |
static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) { |
|
372 |
try { |
|
373 |
baos.write(ba); |
|
374 |
} catch (IOException e) { |
|
375 |
throw new UncheckedIOException(e); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
} |