/** * Creates a Streamable Body which takes a Content-Length. * If the contentLength parameter is -1L a Http Header of Transfer-Encoding: chunked will be set. * Otherwise it will set the Content-Length header to the value provided * * @param publisher Body as a Publisher * @param contentLength Content-Length of the Body */ public ReactiveStreamsBodyGenerator(Publisher<ByteBuf> publisher, long contentLength) { this.publisher = publisher; this.feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator(); this.contentLength = contentLength; }
@BeforeMethod public void setUp() { feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator(); listener = new TestFeedListener(); feedableBodyGenerator.setListener(listener); }
@Test public void readingBytesReturnsFedContentWithoutChunkBoundaries() throws Exception { byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); ByteBuf source = Unpooled.wrappedBuffer(content); ByteBuf target = Unpooled.buffer(1); try { feedableBodyGenerator.feed(source, true); Body body = feedableBodyGenerator.createBody(); assertEquals(readFromBody(body), "Test123".getBytes(StandardCharsets.US_ASCII)); assertEquals(body.transferTo(target), BodyState.STOP); } finally { source.release(); target.release(); } }
@Test public void returnZeroToSuspendStreamWhenNothingIsInQueue() throws Exception { byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); ByteBuf source = Unpooled.wrappedBuffer(content); ByteBuf target = Unpooled.buffer(1); try { feedableBodyGenerator.feed(source, false); Body body = feedableBodyGenerator.createBody(); assertEquals(readFromBody(body), "Test123".getBytes(StandardCharsets.US_ASCII)); assertEquals(body.transferTo(target), BodyState.SUSPEND); } finally { source.release(); target.release(); } }
private void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable { try { try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) { final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator(); Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build(); ListenableFuture<Response> responseFuture = c.executeRequest(r); feed(feedableBodyGenerator, is); waitForAndAssertResponse(responseFuture); } } finally { is.close(); } }