|
49 | 49 | import reactor.core.publisher.Mono;
|
50 | 50 | import reactor.test.StepVerifier;
|
51 | 51 |
|
| 52 | +import java.io.ByteArrayInputStream; |
52 | 53 | import java.nio.ByteBuffer;
|
53 | 54 | import java.nio.channels.AsynchronousFileChannel;
|
54 | 55 | import java.nio.charset.StandardCharsets;
|
@@ -446,6 +447,13 @@ HttpBinJSON putBodyAndContentLength(@BodyParam(ContentType.APPLICATION_OCTET_STR
|
446 | 447 | Mono<HttpBinJSON> putAsyncBodyAndContentLength(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) Flux<ByteBuffer> body,
|
447 | 448 | @HeaderParam("Content-Length") long contentLength);
|
448 | 449 |
|
| 450 | + @Put("put") |
| 451 | + @ExpectedResponses({200}) |
| 452 | + @UnexpectedResponseExceptionType(MyRestException.class) |
| 453 | + Mono<HttpBinJSON> putAsyncBodyAndContentLength( |
| 454 | + @BodyParam(ContentType.APPLICATION_OCTET_STREAM) BinaryData body, |
| 455 | + @HeaderParam("Content-Length") long contentLength); |
| 456 | + |
449 | 457 | @Put("put")
|
450 | 458 | @ExpectedResponses({201})
|
451 | 459 | HttpBinJSON putWithUnexpectedResponse(@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
|
@@ -592,6 +600,88 @@ public void asyncPutRequestWithBodyAndMoreThanContentLength() {
|
592 | 600 | });
|
593 | 601 | }
|
594 | 602 |
|
| 603 | + @Test |
| 604 | + public void asyncPutRequestWithBinaryDataBodyAndEqualContentLength() { |
| 605 | + Mono<BinaryData> bodyMono = BinaryData.fromFlux( |
| 606 | + Flux.just(ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8)))); |
| 607 | + StepVerifier.create( |
| 608 | + bodyMono.flatMap(body -> |
| 609 | + createService(Service9.class).putAsyncBodyAndContentLength(body, 4L))) |
| 610 | + .assertNext(json -> { |
| 611 | + assertEquals("test", json.data()); |
| 612 | + assertEquals(ContentType.APPLICATION_OCTET_STREAM, json.getHeaderValue("Content-Type")); |
| 613 | + assertEquals("4", json.getHeaderValue("Content-Length")); |
| 614 | + }).verifyComplete(); |
| 615 | + } |
| 616 | + |
| 617 | + @Test |
| 618 | + public void asyncPutRequestWithBinaryDataBodyAndLessThanContentLength() { |
| 619 | + Mono<BinaryData> bodyMono = BinaryData.fromFlux( |
| 620 | + Flux.just(ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8)))); |
| 621 | + StepVerifier.create( |
| 622 | + bodyMono.flatMap(body -> |
| 623 | + createService(Service9.class).putAsyncBodyAndContentLength(body, 5L))) |
| 624 | + .verifyErrorSatisfies(exception -> { |
| 625 | + assertTrue(exception instanceof UnexpectedLengthException |
| 626 | + || (exception.getSuppressed().length > 0 |
| 627 | + && exception.getSuppressed()[0] instanceof UnexpectedLengthException)); |
| 628 | + assertTrue(exception.getMessage().contains("less than")); |
| 629 | + }); |
| 630 | + } |
| 631 | + |
| 632 | + /** |
| 633 | + * LengthValidatingInputStream in rest proxy relies on reader |
| 634 | + * reaching EOF. This test specifically targets InputStream to assert this behavior. |
| 635 | + */ |
| 636 | + @Test |
| 637 | + public void asyncPutRequestWithStreamBinaryDataBodyAndLessThanContentLength() { |
| 638 | + Mono<BinaryData> bodyMono = Mono.just(BinaryData.fromStream( |
| 639 | + new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)))); |
| 640 | + StepVerifier.create( |
| 641 | + bodyMono.flatMap(body -> |
| 642 | + createService(Service9.class).putAsyncBodyAndContentLength(body, 5L))) |
| 643 | + .verifyErrorSatisfies(exception -> { |
| 644 | + assertTrue(exception instanceof UnexpectedLengthException |
| 645 | + || (exception.getSuppressed().length > 0 |
| 646 | + && exception.getSuppressed()[0] instanceof UnexpectedLengthException)); |
| 647 | + assertTrue(exception.getMessage().contains("less than")); |
| 648 | + }); |
| 649 | + } |
| 650 | + |
| 651 | + @Test |
| 652 | + public void asyncPutRequestWithBinaryDataBodyAndMoreThanContentLength() { |
| 653 | + Mono<BinaryData> bodyMono = BinaryData.fromFlux( |
| 654 | + Flux.just(ByteBuffer.wrap("test".getBytes(StandardCharsets.UTF_8)))); |
| 655 | + StepVerifier.create( |
| 656 | + bodyMono.flatMap(body -> |
| 657 | + createService(Service9.class).putAsyncBodyAndContentLength(body, 3L))) |
| 658 | + .verifyErrorSatisfies(exception -> { |
| 659 | + assertTrue(exception instanceof UnexpectedLengthException |
| 660 | + || (exception.getSuppressed().length > 0 |
| 661 | + && exception.getSuppressed()[0] instanceof UnexpectedLengthException)); |
| 662 | + assertTrue(exception.getMessage().contains("more than")); |
| 663 | + }); |
| 664 | + } |
| 665 | + |
| 666 | + /** |
| 667 | + * LengthValidatingInputStream in rest proxy relies on reader |
| 668 | + * reaching EOF. This test specifically targets InputStream to assert this behavior. |
| 669 | + */ |
| 670 | + @Test |
| 671 | + public void asyncPutRequestWithStreamBinaryDataBodyAndMoreThanContentLength() { |
| 672 | + Mono<BinaryData> bodyMono = Mono.just(BinaryData.fromStream( |
| 673 | + new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)))); |
| 674 | + StepVerifier.create( |
| 675 | + bodyMono.flatMap(body -> |
| 676 | + createService(Service9.class).putAsyncBodyAndContentLength(body, 3L))) |
| 677 | + .verifyErrorSatisfies(exception -> { |
| 678 | + assertTrue(exception instanceof UnexpectedLengthException |
| 679 | + || (exception.getSuppressed().length > 0 |
| 680 | + && exception.getSuppressed()[0] instanceof UnexpectedLengthException)); |
| 681 | + assertTrue(exception.getMessage().contains("more than")); |
| 682 | + }); |
| 683 | + } |
| 684 | + |
595 | 685 | @Test
|
596 | 686 | public void syncPutRequestWithUnexpectedResponse() {
|
597 | 687 | HttpResponseException e = assertThrows(HttpResponseException.class,
|
@@ -1593,6 +1683,36 @@ public void segmentUploadTest() throws Exception {
|
1593 | 1683 | assertEquals("quick brown fox", response.getValue().data());
|
1594 | 1684 | }
|
1595 | 1685 |
|
| 1686 | + @Host("http://localhost") |
| 1687 | + @ServiceInterface(name = "FluxUploadService") |
| 1688 | + interface BinaryDataUploadService { |
| 1689 | + @Put("/put") |
| 1690 | + Response<HttpBinJSON> put(@BodyParam("text/plain") BinaryData content, |
| 1691 | + @HeaderParam("Content-Length") long contentLength); |
| 1692 | + } |
| 1693 | + |
| 1694 | + @Test |
| 1695 | + public void binaryDataUploadTest() throws Exception { |
| 1696 | + Path filePath = Paths.get(getClass().getClassLoader().getResource("upload.txt").toURI()); |
| 1697 | + BinaryData data = BinaryData.fromFile(filePath); |
| 1698 | + |
| 1699 | + final HttpClient httpClient = createHttpClient(); |
| 1700 | + // Scenario: Log the body so that body buffering/replay behavior is exercised. |
| 1701 | + // |
| 1702 | + // Order in which policies applied will be the order in which they added to builder |
| 1703 | + // |
| 1704 | + final HttpPipeline httpPipeline = new HttpPipelineBuilder() |
| 1705 | + .httpClient(httpClient) |
| 1706 | + .policies(new PortPolicy(getWireMockPort(), true), |
| 1707 | + new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))) |
| 1708 | + .build(); |
| 1709 | + // |
| 1710 | + Response<HttpBinJSON> response = RestProxy |
| 1711 | + .create(BinaryDataUploadService.class, httpPipeline).put(data, Files.size(filePath)); |
| 1712 | + |
| 1713 | + assertEquals("The quick brown fox jumps over the lazy dog", response.getValue().data()); |
| 1714 | + } |
| 1715 | + |
1596 | 1716 | @Host("{url}")
|
1597 | 1717 | @ServiceInterface(name = "Service22")
|
1598 | 1718 | interface Service22 {
|
|
0 commit comments