@Test public void testMultipleReads() throws IOException { final int srcArraySize = (3 * chunkSize) + 42; final byte[] srcArray = new byte[srcArraySize]; random.nextBytes(srcArray); final ByteArrayBodyGenerator babGen = new ByteArrayBodyGenerator(srcArray); final Body body = babGen.createBody(); final ByteBuf chunkBuffer = Unpooled.buffer(chunkSize); try { int reads = 0; int bytesRead = 0; while (body.transferTo(chunkBuffer) != BodyState.STOP) { reads += 1; bytesRead += chunkBuffer.readableBytes(); chunkBuffer.clear(); } assertEquals(reads, 4, "reads to drain generator"); assertEquals(bytesRead, srcArraySize, "bytes read"); } finally { chunkBuffer.release(); } } }
@Test public void testNoDirectRequestBodyWithProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get(); assertEquals(r.getStatusCode(), 200); } }
throw new RuntimeException(e); builder.setBody(new ByteArrayBodyGenerator(bodyBytes)); } else if (body instanceof File) { File fileBody = (File) body;
@Test public void testSingleRead() throws IOException { final int srcArraySize = chunkSize - 1; final byte[] srcArray = new byte[srcArraySize]; random.nextBytes(srcArray); final ByteArrayBodyGenerator babGen = new ByteArrayBodyGenerator(srcArray); final Body body = babGen.createBody(); final ByteBuf chunkBuffer = Unpooled.buffer(chunkSize); try { // should take 1 read to get through the srcArray body.transferTo(chunkBuffer); assertEquals(chunkBuffer.readableBytes(), srcArraySize, "bytes read"); chunkBuffer.clear(); assertEquals(body.transferTo(chunkBuffer), BodyState.STOP, "body at EOF"); } finally { chunkBuffer.release(); } }