Search in sources :

Example 41 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestBasicAsyncEntityProducer method testTextContent.

@Test
public void testTextContent() throws Exception {
    final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
    Assertions.assertEquals(3, producer.getContentLength());
    Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
    Assertions.assertNull(producer.getContentEncoding());
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 42 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestBasicAsyncEntityProducer method testBinaryContent.

@Test
public void testBinaryContent() throws Exception {
    final AsyncEntityProducer producer = new BasicAsyncEntityProducer(new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY);
    Assertions.assertEquals(3, producer.getContentLength());
    Assertions.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType());
    Assertions.assertNull(producer.getContentEncoding());
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 43 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestAbstractH2StreamMultiplexer method testInputOneFrame.

@Test
public void testInputOneFrame() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
    final byte[] data = new byte[FrameConsts.MIN_FRAME_SIZE];
    for (int i = 0; i < FrameConsts.MIN_FRAME_SIZE; i++) {
        data[i] = (byte) (i % 16);
    }
    final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
    outbuffer.write(frame, writableChannel);
    final byte[] bytes = writableChannel.toByteArray();
    final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(protocolIOSession, DefaultFrameFactory.INSTANCE, StreamIdGenerator.ODD, httpProcessor, CharCodingConfig.DEFAULT, H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE).build(), h2StreamListener);
    Assertions.assertThrows(H2ConnectionException.class, () -> streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
    Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    Assertions.assertThrows(H2ConnectionException.class, () -> {
        int pos = 0;
        int remaining = bytes.length;
        while (remaining > 0) {
            final int chunk = Math.min(2048, remaining);
            streamMultiplexer.onInput(ByteBuffer.wrap(bytes, pos, chunk));
            pos += chunk;
            remaining -= chunk;
        }
        Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    });
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Test(org.junit.jupiter.api.Test)

Example 44 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestAbstractH2StreamMultiplexer method testInputMultipleFrames.

@Test
public void testInputMultipleFrames() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
    final byte[] data = new byte[FrameConsts.MIN_FRAME_SIZE];
    for (int i = 0; i < FrameConsts.MIN_FRAME_SIZE; i++) {
        data[i] = (byte) (i % 16);
    }
    final RawFrame frame1 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
    outbuffer.write(frame1, writableChannel);
    final RawFrame frame2 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
    outbuffer.write(frame2, writableChannel);
    final byte[] bytes = writableChannel.toByteArray();
    final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(protocolIOSession, DefaultFrameFactory.INSTANCE, StreamIdGenerator.ODD, httpProcessor, CharCodingConfig.DEFAULT, H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE).build(), h2StreamListener);
    Assertions.assertThrows(H2ConnectionException.class, () -> streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
    Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    Assertions.assertThrows(H2ConnectionException.class, () -> {
        int pos = 0;
        int remaining = bytes.length;
        while (remaining > 0) {
            final int chunk = Math.min(4096, remaining);
            streamMultiplexer.onInput(ByteBuffer.wrap(bytes, pos, chunk));
            pos += chunk;
            remaining -= chunk;
        }
        Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
    });
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Test(org.junit.jupiter.api.Test)

Example 45 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestFrameInOutBuffers method testPartialFrameWrite.

@Test
public void testPartialFrameWrite() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024, FrameConsts.HEAD_LEN + 10);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
    final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), FrameFlag.END_STREAM.getValue(), 5, ByteBuffer.wrap(new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }));
    outbuffer.write(frame, writableChannel);
    Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }, writableChannel.toByteArray());
    Assertions.assertFalse(outbuffer.isEmpty());
    outbuffer.flush(writableChannel);
    Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }, writableChannel.toByteArray());
    writableChannel.flush();
    outbuffer.flush(writableChannel);
    Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 }, writableChannel.toByteArray());
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)79 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)70 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)52 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)50 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)20 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)14 RandomAccessFile (java.io.RandomAccessFile)11 FileChannel (java.nio.channels.FileChannel)11 ByteBuffer (java.nio.ByteBuffer)10 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)10 WritableByteChannelMock (org.apache.hc.core5.http2.WritableByteChannelMock)5 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)5 Charset (java.nio.charset.Charset)3 Path (java.nio.file.Path)2 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)2 ExecutorService (java.util.concurrent.ExecutorService)1 Header (org.apache.hc.core5.http.Header)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 ReadableByteChannelMock (org.apache.hc.core5.http2.ReadableByteChannelMock)1