Search in sources :

Example 1 with Body

use of org.asynchttpclient.request.body.Body in project async-http-client by AsyncHttpClient.

the class FeedableBodyGeneratorTest method returnZeroToSuspendStreamWhenNothingIsInQueue.

@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();
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 2 with Body

use of org.asynchttpclient.request.body.Body in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testSingleRead.

@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();
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 3 with Body

use of org.asynchttpclient.request.body.Body in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testMultipleReads.

@Test(groups = "standalone")
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);
    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");
}
Also used : ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 4 with Body

use of org.asynchttpclient.request.body.Body in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testSingleRead.

@Test(groups = "standalone")
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);
    // 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");
}
Also used : ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Example 5 with Body

use of org.asynchttpclient.request.body.Body in project async-http-client by AsyncHttpClient.

the class ByteArrayBodyGeneratorTest method testMultipleReads.

@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();
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Body(org.asynchttpclient.request.body.Body) Test(org.testng.annotations.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)6 Body (org.asynchttpclient.request.body.Body)6 Test (org.testng.annotations.Test)6 ByteArrayBodyGenerator (org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator)2