Search in sources :

Example 26 with RawFrame

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

the class TestFrameInOutBuffers method testWriteFrameExceedingLimit.

@Test
public void testWriteFrameExceedingLimit() throws Exception {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(1024);
    final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(new byte[2048]));
    Assertions.assertThrows(H2ConnectionException.class, () -> outbuffer.write(frame, outputStream));
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 27 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame 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 28 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame 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 29 with RawFrame

use of org.apache.hc.core5.http2.frame.RawFrame 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)

Example 30 with RawFrame

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

the class TestFrameInOutBuffers method testWriteFrameExceedingLimit.

@Test
public void testWriteFrameExceedingLimit() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(1024);
    final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(new byte[2048]));
    Assertions.assertThrows(IllegalArgumentException.class, () -> outbuffer.write(frame, writableChannel));
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Test(org.junit.jupiter.api.Test)

Aggregations

RawFrame (org.apache.hc.core5.http2.frame.RawFrame)38 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)15 Header (org.apache.hc.core5.http.Header)10 HttpConnection (org.apache.hc.core5.http.HttpConnection)9 H2StreamListener (org.apache.hc.core5.http2.impl.nio.H2StreamListener)9 HttpResponse (org.apache.hc.core5.http.HttpResponse)8 List (java.util.List)7 HttpAsyncRequester (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)7 H2Config (org.apache.hc.core5.http2.config.H2Config)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Message (org.apache.hc.core5.http.Message)6 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)6 H2ConnectionException (org.apache.hc.core5.http2.H2ConnectionException)6 Map (java.util.Map)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 AsyncClientEndpoint (org.apache.hc.core5.http.nio.AsyncClientEndpoint)5 H2StreamResetException (org.apache.hc.core5.http2.H2StreamResetException)5