-
Notifications
You must be signed in to change notification settings - Fork 12
/
IntegrationTests.java
834 lines (753 loc) · 32.1 KB
/
IntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
* Copyright (c) 2019, 2020 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.mizosoft.methanol.blackbox;
import static com.github.mizosoft.methanol.MoreBodyHandlers.decoding;
import static com.github.mizosoft.methanol.MoreBodyHandlers.fromAsyncSubscriber;
import static com.github.mizosoft.methanol.MoreBodyHandlers.ofByteChannel;
import static com.github.mizosoft.methanol.MoreBodyHandlers.ofDeferredObject;
import static com.github.mizosoft.methanol.MoreBodyHandlers.ofObject;
import static com.github.mizosoft.methanol.MoreBodyHandlers.ofReader;
import static com.github.mizosoft.methanol.MoreBodyHandlers.withReadTimeout;
import static com.github.mizosoft.methanol.MoreBodyPublishers.ofMediaType;
import static com.github.mizosoft.methanol.testutils.TestUtils.gunzip;
import static com.github.mizosoft.methanol.testutils.TestUtils.headers;
import static com.github.mizosoft.methanol.testutils.TestUtils.lines;
import static com.github.mizosoft.methanol.testutils.TestUtils.load;
import static com.github.mizosoft.methanol.testutils.TestUtils.loadAscii;
import static com.github.mizosoft.methanol.testutils.TestUtils.zlibUnwrap;
import static java.net.http.HttpRequest.BodyPublishers.fromPublisher;
import static java.net.http.HttpResponse.BodyHandlers.discarding;
import static java.net.http.HttpResponse.BodyHandlers.ofString;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.mizosoft.methanol.BodyDecoder;
import com.github.mizosoft.methanol.HttpReadTimeoutException;
import com.github.mizosoft.methanol.MediaType;
import com.github.mizosoft.methanol.MoreBodyPublishers;
import com.github.mizosoft.methanol.MultipartBodyPublisher;
import com.github.mizosoft.methanol.MultipartBodyPublisher.Part;
import com.github.mizosoft.methanol.MutableRequest;
import com.github.mizosoft.methanol.TypeRef;
import com.github.mizosoft.methanol.WritableBodyPublisher;
import com.github.mizosoft.methanol.blackbox.Bruh.BruhMoment;
import com.github.mizosoft.methanol.blackbox.Bruh.BruhMoments;
import com.github.mizosoft.methanol.testutils.BuffIterator;
import com.github.mizosoft.methanol.testutils.MockGzipMember;
import com.github.mizosoft.methanol.testutils.MockGzipMember.CorruptionMode;
import com.github.mizosoft.methanol.testutils.RegistryFileTypeDetector;
import com.github.mizosoft.methanol.testutils.ServiceLoggerHelper;
import com.github.mizosoft.methanol.testutils.TestUtils;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpResponse.BodySubscriber;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.GZIPOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBContextFactory;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.Buffer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.reactivestreams.FlowAdapters;
import org.reactivestreams.example.unicast.AsyncIterablePublisher;
import reactor.core.publisher.Flux;
class IntegrationTests {
private static final Base64.Decoder BASE64_DEC = Base64.getDecoder();
private static final String poem =
"Roses are red,\n"
+ "Violets are blue,\n"
+ "I hope my tests pass\n"
+ "I really hope they do";
private static final String epicArtCourseXmlUtf8 =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<course type=\"ART\">"
+ "<enrolled-students>"
+ "<student>"
+ "<name>Leonardo Da Vinci</name>"
+ "</student>"
+ "<student>"
+ "<name>Michelangelo</name>"
+ "</student>"
+ "</enrolled-students>"
+ "</course>";
private static final Course epicArtCourse =
new Course(Type.ART, List.of(new Student("Leonardo Da Vinci"), new Student("Michelangelo")));
private static String lotsOfText;
private static Map<String, String> poemEncodings;
private static Map<String, byte[]> lotsOfTextEncodings;
private static String lotsOfJson;
private static List<Map<String, Object>> lotsOfJsonDecoded;
private static BruhMoments bruhMoments;
private MockWebServer server;
private HttpClient client;
private Executor executor;
private ScheduledExecutorService scheduler;
@BeforeAll
static void readTestData() {
Class<?> cls = IntegrationTests.class;
lotsOfText = loadAscii(cls, "/payload/alice.txt");
lotsOfJson = loadAscii(cls, "/payload/lots_of_json.json");
poemEncodings = Map.of(
"gzip", "H4sIAAAAAAAAAAvKL04tVkgsSlUoSk3R4QrLzM9JLYEIJOWUpupweSpk5BekKuRWKpSkFgNlChKLi4GCRamJOTmVELmSjNRKhZR8APk5jwRLAAAA",
"deflate", "eJwLyi9OLVZILEpVKEpN0eEKy8zPSS2BCCTllKbqcHkqZOQXpCrkViqUpBYDZQoSi4uBgkWpiTk5lRC5kozUSoWUfADV9hoM",
"br", "G0oAgIyUq+1omSRlpW7rK2n013L4gqBcPovgkAP2L7As4eaBQLABJ9oyDOFfbZk54qgCr956gL4JUBefr6J90wYuSdm+PwU=",
"badzip", "");
lotsOfTextEncodings = Map.of(
"gzip", load(cls, "/payload/alice.gz"),
"deflate", load(cls, "/payload/alice.zz"),
"br", load(cls, "/payload/alice.br"),
"badzip", new byte[0]);
var mapper = new JsonMapper();
var type = new TypeRef<List<Map<String, Object>>>() {};
try {
lotsOfJsonDecoded = mapper.readerFor(mapper.constructType(type.type()))
.readValue(lotsOfJson);
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
bruhMoments = BruhMoments.newBuilder()
.addMoments(0, BruhMoment.newBuilder().setMessage("bruh"))
.addMoments(1, BruhMoment.newBuilder().setMessage("bbruuuhhh"))
.addMoments(2, BruhMoment.newBuilder().setMessage("bbrrruuuuuuuhhhhhhhh!!??"))
.build();
}
private static ServiceLoggerHelper loggerHelper;
@BeforeAll
static void turnOffServiceLogger() {
// Do not log service loader failures.
loggerHelper = new ServiceLoggerHelper();
loggerHelper.turnOff();
}
@AfterAll
static void resetServiceLogger() {
loggerHelper.reset();
}
@BeforeAll
static void registerJaxbImplementation() {
System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY, MoxyJaxbContextFactory.class.getName());
}
@BeforeEach
void setUpLifecycle() throws IOException {
server = new MockWebServer();
server.start();
var builder = HttpClient.newBuilder();
client = builder.build();
executor = Executors.newFixedThreadPool(8);
scheduler = Executors.newSingleThreadScheduledExecutor();
}
@AfterEach
void tearDownLifecycle() throws IOException {
server.shutdown();
TestUtils.shutdown(executor, scheduler);
}
private void assertDecodes(String encoding, String expected, byte[] compressed) throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(compressed))
.setHeader("Content-Encoding", encoding));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofString()));
assertLinesMatch(lines(expected), lines(response.body()));
}
private void assertDecodesSmall(String encoding) throws Exception {
var compressed = BASE64_DEC.decode(poemEncodings.get(encoding));
assertDecodes(encoding, poem, compressed);
// Test deflate body without zlib wrapping
if (encoding.equals("deflate")) {
assertDecodes(encoding, poem, zlibUnwrap(compressed));
}
}
private void assertDecodesLarge(String encoding) throws Exception {
var compressed = lotsOfTextEncodings.get(encoding);
assertDecodes(encoding, lotsOfText, compressed);
// Test deflate body without zlib wrapping
if (encoding.equals("deflate")) {
assertDecodes(encoding, lotsOfText, zlibUnwrap(compressed));
}
}
@Test
void decoding_gzip() throws Exception {
assertDecodesSmall("gzip");
assertDecodesLarge("gzip");
}
@Test
void decoding_deflate() throws Exception {
assertDecodesSmall("deflate");
assertDecodesLarge("deflate");
}
@Test
void decoding_brotli() throws Exception {
assumeTrue(BodyDecoder.Factory.getFactory("br").isPresent());
assertDecodesSmall("br");
assertDecodesLarge("br");
}
@Test
void decoding_concatenatedGzip() throws Exception {
var firstMember = lotsOfTextEncodings.get("gzip");
var secondMember = BASE64_DEC.decode(poemEncodings.get("gzip"));
var thirdMember = MockGzipMember.newBuilder()
.addComment(55)
.addFileName(555)
.addExtraField(5555)
.addHeaderChecksum()
.setText()
.data(lotsOfText.getBytes(US_ASCII))
.build()
.getBytes();
var buffer = new Buffer()
.write(firstMember)
.write(secondMember)
.write(thirdMember);
server.enqueue(new MockResponse()
.setBody(buffer)
.setHeader("Content-Encoding", "gzip"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofString()));
assertLinesMatch(lines(lotsOfText + poem + lotsOfText), lines(response.body()));
}
@Test
void decoding_corruptConcatenatedGzip() {
var firstMember = lotsOfTextEncodings.get("gzip");
var secondMember = MockGzipMember.newBuilder()
.data(poem.getBytes(US_ASCII))
.corrupt(CorruptionMode.FLG, 0xE0) // add reserved flag
.build()
.getBytes();
var buffer = new Buffer()
.write(firstMember)
.write(secondMember);
server.enqueue(new MockResponse()
.setHeader("Content-Encoding", "gzip")
.setBody(buffer));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var ex = assertThrows(IOException.class, () -> client.send(request, decoding(ofString())));
// If a concat member has a corrupt header, it is considered trailing garbage (end of gzip stream)
assertEquals("gzip stream finished prematurely", ex.getMessage());
// Will suppress read header failure
var suppressed = ex.getCause().getSuppressed();
assertEquals(1, suppressed.length);
var suppressedIoe = suppressed[0];
assertTrue(suppressedIoe instanceof IOException);
assertEquals("unsupported flags: 0xe0", suppressedIoe.getMessage());
}
@Test
void decoding_badZip() {
assertThrows(IOException.class, () -> assertDecodesSmall("badzip"));
}
@Test
void decoding_unsupported() {
server.enqueue(new MockResponse().setHeader("Content-Encoding", "alienzip"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var ioe = assertThrows(IOException.class, () -> client.send(request, decoding(ofString())));
assertTrue(ioe.getCause() instanceof UnsupportedOperationException, ioe.toString());
}
@Test
void decoding_nestedHandlerGetsNoLengthOrEncoding() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(BASE64_DEC.decode(poemEncodings.get("gzip"))))
.setHeader("Content-Encoding", "gzip"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var headers = new AtomicReference<HttpHeaders>();
var response = client.send(request, decoding(info -> {
headers.set(info.headers());
return BodyHandlers.ofString().apply(info);
}));
assertEquals(poem, response.body());
var headersMap = headers.get().map();
assertFalse(headersMap.containsKey("Content-Encoding"));
assertFalse(headersMap.containsKey("Content-Length"));
}
@Test
void decoding_noEncoding() throws Exception {
server.enqueue(new MockResponse().setBody(poem));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofString()));
assertEquals(poem, response.body());
}
@Test
void ofByteChannel_throttledGzippedWithExecutor() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(lotsOfTextEncodings.get("gzip")))
.setHeader("Content-Encoding", "gzip")
.throttleBody(16 * 1024, 100, TimeUnit.MILLISECONDS)); // 100 MS every 16 KB
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofByteChannel(), executor));
try (var responseReader = new BufferedReader(Channels.newReader(response.body(), US_ASCII))) {
var expectedReader = new BufferedReader(new StringReader(lotsOfText));
String expectedLine;
while ((expectedLine = expectedReader.readLine()) != null) {
assertEquals(expectedLine, responseReader.readLine());
}
assertNull(responseReader.readLine());
}
}
@Test
@Disabled // MockWebServer never interrupts throttled body in shutdown
void ofByteChannel_interruptThrottled() {
var amountToRead = 128;
server.enqueue(new MockResponse()
.setBody(lotsOfText)
.throttleBody(amountToRead, Long.MAX_VALUE, TimeUnit.MILLISECONDS));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var throwableFuture = new CompletableFuture<Throwable>();
client.sendAsync(request, ofByteChannel())
.thenAccept(res -> {
try {
var channel = res.body();
var buff = ByteBuffer.allocate(amountToRead);
// this loop should execute once
while (channel.read(buff.rewind()) != -1) {
var readerThread = Thread.currentThread();
new Thread(readerThread::interrupt).start();
}
} catch (Throwable t) {
throwableFuture.complete(t);
}
});
var throwable = throwableFuture.join();
assertTrue(throwable instanceof ClosedByInterruptException, String.valueOf(throwable));
}
@Test
void ofReader_gzipped() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(lotsOfTextEncodings.get("gzip")))
.setHeader("Content-Encoding", "gzip"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofReader()));
try (var responseReader = new BufferedReader(response.body())) {
var expectedReader = new BufferedReader(new StringReader(lotsOfText));
String expectedLine;
while ((expectedLine = expectedReader.readLine()) != null) {
assertEquals(expectedLine, responseReader.readLine());
}
assertNull(responseReader.readLine());
}
}
@Test
void ofObject_protobuf() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(bruhMoments.toByteArray()))
.addHeader("Content-Type", "application/x-protobuf"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, ofObject(BruhMoments.class));
assertEquals(bruhMoments, response.body());
}
@Test
void ofObject_gzippedDeferredProtobuf() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(gzip(bruhMoments.toByteArray())))
.addHeader("Content-Encoding", "gzip")
.addHeader("Content-Type", "application/octet-stream"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, decoding(ofDeferredObject(BruhMoments.class)));
assertEquals(bruhMoments, response.body().get());
}
@Test
void ofObject_gzippedJsonWithExecutor() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(gzip(lotsOfJson.getBytes(US_ASCII))))
.addHeader("Content-Encoding", "gzip")
.addHeader("Content-Type", "application/json"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var type = new TypeRef<List<Map<String, Object>>>() {};
var response = client.send(request, decoding(ofObject(type), executor));
assertEquals(lotsOfJsonDecoded, response.body());
}
@Test
void ofObject_deferredJson() throws Exception {
server.enqueue(new MockResponse()
.setBody(lotsOfJson)
.addHeader("Content-Type", "application/json"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var type = new TypeRef<List<Map<String, Object>>>() {};
var response = client.send(request, ofObject(type));
assertEquals(lotsOfJsonDecoded, response.body());
}
@Test
void ofObject_unsupported() {
server.enqueue(new MockResponse()
.setBody("heuhuehue")
.setHeader("Content-Type", "application/x-bruh"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var ioe = assertThrows(IOException.class, () -> client.send(request, ofObject(String.class)));
assertTrue(ioe.getCause() instanceof UnsupportedOperationException, ioe.toString());
}
@Test
void ofObject_stringDecoder() throws Exception {
server.enqueue(new MockResponse()
.setBody(poem)
.addHeader("Content-Type", "text/plain"));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(request, ofObject(String.class));
assertEquals(poem, response.body());
}
@Test
void ofObject_jsonFlux() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(gzip(lotsOfJson.getBytes(UTF_8))))
.addHeader("Content-Encoding", "gzip")
.addHeader("Content-Type", "application/json")
.throttleBody(8 * 1024, 100, TimeUnit.MILLISECONDS));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(
request, decoding(ofObject(new TypeRef<Flux<Map<String, Object>>>() {})));
var listMono = response.body().collectList();
assertEquals(Optional.of(lotsOfJsonDecoded), listMono.blockOptional());
}
@Test
void ofObject_uploadFlux() throws Exception {
server.enqueue(new MockResponse());
var flux = Flux.fromIterable(lotsOfJsonDecoded);
var request = MutableRequest.POST(
server.url("/").uri(), MoreBodyPublishers.ofObject(flux, MediaType.APPLICATION_JSON));
client.sendAsync(request, discarding());
var recordedRequest = server.takeRequest();
var uploaded = recordedRequest.getBody().readUtf8();
var expected = JacksonProviders.configuredMapper.writeValueAsString(lotsOfJsonDecoded);
assertEquals(expected, uploaded);
}
@Test
void ofObject_downloadXml() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(gzip(epicArtCourseXmlUtf8.getBytes(UTF_8))))
.addHeader("Content-Encoding", "gzip")
.addHeader("Content-Type", "application/xml"));
var request = MutableRequest.GET(server.url("/").uri());
var response = client.send(request, decoding(ofObject(new TypeRef<Course>() {})));
assertEquals(epicArtCourse, response.body());
}
@Test
void ofObject_uploadXml() throws Exception {
server.enqueue(new MockResponse());
var request = MutableRequest.POST(
server.url("/").uri(),
MoreBodyPublishers.ofObject(epicArtCourse, MediaType.TEXT_XML.withCharset(UTF_8)));
client.sendAsync(request, discarding());
var recordedRequest = server.takeRequest();
var uploaded = recordedRequest.getBody().readUtf8();
assertEquals(epicArtCourseXmlUtf8, uploaded);
}
@Test
void fromAsyncSubscriber_uncompletedToCompletedBody() throws Exception {
server.enqueue(new MockResponse()
.setBody(okBuffer(new byte[] {1})));
var request = HttpRequest.newBuilder(server.url("/").uri())
.timeout(Duration.ofSeconds(10))
.build();
var deadlockBody = new BodySubscriber<>() {
private final CompletableFuture<Subscription> subscriptionCf = new CompletableFuture<>();
@Override public CompletionStage<Object> getBody() {
return new CompletableFuture<>(); // never completes!
}
@Override public void onSubscribe(Subscription subscription) {
subscriptionCf.complete(subscription);
}
@Override public void onNext(List<ByteBuffer> item) {}
@Override public void onError(Throwable throwable) {}
@Override public void onComplete() {}
};
var response = client.send(request, fromAsyncSubscriber(deadlockBody, body -> body.subscriptionCf));
response.body().cancel(); // this closes the connection
}
@Test
void withReadTimeout_readThroughByteChannel() throws Exception {
var timeoutMillis = 50L;
server.enqueue(new MockResponse()
.setBody(poem)
.throttleBody(0, timeoutMillis * 10, TimeUnit.MILLISECONDS));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(
request, withReadTimeout(ofByteChannel(), Duration.ofMillis(timeoutMillis)));
try (var channel = response.body()) {
assertReadTimeout(channel);
}
}
@Test
void withReadTimeout_readThroughByteChannel_customScheduler() throws Exception {
var timeoutMillis = 50L;
server.enqueue(new MockResponse()
.setBody(poemEncodings.get("gzip"))
.setHeader("Content-Encoding", "gzip")
.throttleBody(0, timeoutMillis * 10, TimeUnit.MILLISECONDS));
var request = HttpRequest.newBuilder(server.url("/").uri()).build();
var response = client.send(
request,
withReadTimeout(
decoding(ofByteChannel(), executor), Duration.ofMillis(timeoutMillis), scheduler));
try (var channel = response.body()) {
assertReadTimeout(channel);
}
}
@Test
void sendFormBody(@TempDir Path tempDir) throws Exception {
var tweet = new Tweet();
tweet.sender = "Albert Einstein";
tweet.content = "Is math related to science?";
var theoryFile = Files.createFile(tempDir.resolve("relativity.theory"));
Files.writeString(theoryFile, "Time is relative bro");
RegistryFileTypeDetector.register("theory", MediaType.parse("application/x-theory"));
var multipartBody = MultipartBodyPublisher.newBuilder()
.boundary("my_awesome_boundary")
.textPart("action", "sendTweet")
.formPart("tweet", MoreBodyPublishers.ofObject(tweet, MediaType.of("application", "json")))
.filePart("attachment", theoryFile)
.build();
var request = HttpRequest.newBuilder(server.url("/").uri())
.header("Content-Type", multipartBody.mediaType().toString())
.POST(multipartBody)
.build();
client.sendAsync(request, discarding());
var sentRequest = server.takeRequest();
assertEquals("multipart/form-data; boundary=my_awesome_boundary",
sentRequest.getHeader("Content-Type"));
var expectedBody =
"--my_awesome_boundary\r\n"
+ "Content-Disposition: form-data; name=\"action\"\r\n"
+ "\r\n"
+ "sendTweet\r\n"
+ "--my_awesome_boundary\r\n"
+ "Content-Disposition: form-data; name=\"tweet\"\r\n"
+ "Content-Type: application/json\r\n"
+ "\r\n"
+ "{\"sender\":\"Albert Einstein\",\"content\":\"Is math related to science?\"}\r\n"
+ "--my_awesome_boundary\r\n"
+ "Content-Disposition: form-data; name=\"attachment\"; filename=\"relativity.theory\"\r\n"
+ "Content-Type: application/x-theory\r\n"
+ "\r\n"
+ "Time is relative bro\r\n"
+ "--my_awesome_boundary--\r\n";
assertEquals(expectedBody, sentRequest.getBody().readUtf8());
}
@Test
void sendEmailBody() throws Exception {
var multipartAlternative = MultipartBodyPublisher.newBuilder()
.mediaType(MediaType.of("multipart", "alternative"))
.boundary("my_cool_boundary")
.part(Part.create(
headers("Content-Transfer-Encoding", "quoted-printable"),
MoreBodyPublishers.ofObject("Hey, that's pretty good", MediaType.of("text", "plain"))))
.part(Part.create(
headers("Content-Transfer-Encoding", "quoted-printable"),
MoreBodyPublishers
.ofObject("<h1>Hey, that's pretty good</h1>", MediaType.of("text", "html"))))
.build();
var attachment = load(getClass(), "/payload/alice.txt");
var attachmentPublisher = FlowAdapters.toFlowPublisher(new AsyncIterablePublisher<>(
() -> new BuffIterator(ByteBuffer.wrap(attachment), 1024), executor));
var multipartMixed = MultipartBodyPublisher.newBuilder()
.mediaType(MediaType.of("multipart", "mixed"))
.boundary("no_boundary_is_like_my_boundary")
.part(Part.create(headers(), multipartAlternative))
.part(Part.create(
headers("Content-Disposition", "attachment; name=\"file_attachment\"; filename=\"alice.txt\""),
ofMediaType(
fromPublisher(attachmentPublisher, attachment.length),
MediaType.of("text", "plain"))))
.build();
var expected_template =
"--no_boundary_is_like_my_boundary\r\n"
+ "Content-Type: multipart/alternative; boundary=my_cool_boundary\r\n"
+ "\r\n"
+ "--my_cool_boundary\r\n"
+ "Content-Transfer-Encoding: quoted-printable\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n"
+ "Hey, that's pretty good\r\n"
+ "--my_cool_boundary\r\n"
+ "Content-Transfer-Encoding: quoted-printable\r\n"
+ "Content-Type: text/html\r\n"
+ "\r\n"
+ "<h1>Hey, that's pretty good</h1>\r\n"
+ "--my_cool_boundary--\r\n"
+ "\r\n"
+ "--no_boundary_is_like_my_boundary\r\n"
+ "Content-Disposition: attachment; name=\"file_attachment\"; filename=\"alice.txt\"\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n"
+ "%s\r\n" // alice goes here
+ "--no_boundary_is_like_my_boundary--\r\n";
var request = HttpRequest.newBuilder(server.url("/").uri())
.header("Content-Type", multipartMixed.mediaType().toString())
.POST(multipartMixed)
.build();
client.sendAsync(request, discarding());
var sentRequest = server.takeRequest();
assertEquals("multipart/mixed; boundary=no_boundary_is_like_my_boundary",
sentRequest.getHeader("Content-Type"));
assertEquals(sentRequest.getHeader("Content-Length"),
Long.toString(multipartMixed.contentLength()));
assertEquals(
String.format(expected_template, loadAscii(getClass(), "/payload/alice.txt")),
sentRequest.getBody().readUtf8());
}
@Test
void sendGzippedRequest() throws Exception {
var body = WritableBodyPublisher.create();
var request = MutableRequest.POST(server.url("/").uri(), body);
client.sendAsync(request, BodyHandlers.discarding());
CompletableFuture.runAsync(() -> {
try (var gzipOut = new GZIPOutputStream(body.outputStream())) {
new ByteArrayInputStream(lotsOfText.getBytes(UTF_8)).transferTo(gzipOut);
} catch (IOException e) {
fail(e);
}
});
var recordedRequest = server.takeRequest();
assertEquals(lotsOfText, new String(gunzip(recordedRequest.getBody().readByteArray()), UTF_8));
}
private static void assertReadTimeout(ReadableByteChannel channel) {
var ioe = assertThrows(IOException.class, () -> channel.read(ByteBuffer.allocate(1)));
assertSame(HttpReadTimeoutException.class, ioe.getCause().getClass());
}
private static Buffer okBuffer(byte[] bytes) {
return new Buffer().write(bytes);
}
private static byte[] gzip(byte[] bytes) {
try {
var outBuffer = new ByteArrayOutputStream();
try (var gzOut = new GZIPOutputStream(outBuffer)) {
gzOut.write(bytes);
}
return outBuffer.toByteArray();
} catch (IOException ioe) {
throw new AssertionError(ioe);
}
}
static final class Tweet {
public String sender;
public String content;
public Tweet() {}
}
@XmlRootElement(name = "course")
private static final class Course {
@XmlAttribute(required = true)
private final Type type;
@XmlElementWrapper(name = "enrolled-students")
@XmlElement(name = "student")
private final List<Student> enrolledStudents;
private Course() {
this(Type.UNKNOWN, new ArrayList<>());
}
Course(Type type, List<Student> enrolledStudents) {
this.type = type;
this.enrolledStudents = enrolledStudents;
}
@Override
public boolean equals(Object obj) {
return obj instanceof Course
&& type == ((Course) obj).type
&& enrolledStudents.equals(((Course) obj).enrolledStudents);
}
@Override
public String toString() {
return "Course[type="
+ type
+ ", enrolledStudents="
+ enrolledStudents + "]";
}
}
private static final class Student {
@XmlElement(required = true)
private final String name;
private Student() {
this("");
}
Student(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
return obj instanceof Student && name.equals(((Student) obj).name);
}
@Override
public String toString() {
return "Student[" + name + "]";
}
}
private enum Type {
UNKNOWN,
ART,
CALCULUS
}
public static class MoxyJaxbContextFactory implements JAXBContextFactory {
public MoxyJaxbContextFactory() {}
@Override
public JAXBContext createContext(Class<?>[] classesToBeBound, Map<String, ?> properties)
throws JAXBException {
return org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(classesToBeBound, properties);
}
@Override
public JAXBContext createContext(String contextPath, ClassLoader classLoader,
Map<String, ?> properties) throws JAXBException {
return org.eclipse.persistence.jaxb.JAXBContextFactory
.createContext(contextPath, classLoader, properties);
}
}
}