Search in sources :

Example 46 with WritableByteChannelMock

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

Example 47 with WritableByteChannelMock

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

the class TestReactiveEntityProducer method testStreamThatEndsNormally.

@Test
public void testStreamThatEndsNormally() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.just(ByteBuffer.wrap(new byte[] { '1', '2', '3' }), ByteBuffer.wrap(new byte[] { '4', '5', '6' }));
    final ReactiveEntityProducer entityProducer = new ReactiveEntityProducer(publisher, CONTENT_LENGTH, CONTENT_TYPE, GZIP_CONTENT_ENCODING);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    entityProducer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen(), "Should be open");
    Assertions.assertEquals("123456", byteChannel.dump(StandardCharsets.US_ASCII));
    entityProducer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen(), "Should be closed");
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    Assertions.assertFalse(entityProducer.isChunked());
    Assertions.assertEquals(GZIP_CONTENT_ENCODING, entityProducer.getContentEncoding());
    Assertions.assertNull(entityProducer.getTrailerNames());
    Assertions.assertEquals(CONTENT_LENGTH, entityProducer.getContentLength());
    Assertions.assertEquals(CONTENT_TYPE.toString(), entityProducer.getContentType());
    Assertions.assertFalse(entityProducer.isRepeatable());
    Assertions.assertEquals(1, entityProducer.available());
    entityProducer.releaseResources();
}
Also used : ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 48 with WritableByteChannelMock

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

the class TestLengthDelimitedEncoder method testCodingCompleted.

@Test
public void testCodingCompleted() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 5);
    encoder.write(CodecTestUtils.wrap("stuff"));
    Assertions.assertThrows(IllegalStateException.class, () -> encoder.write(CodecTestUtils.wrap("more stuff")));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 49 with WritableByteChannelMock

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

the class TestLengthDelimitedEncoder method testBasicCoding.

@Test
public void testBasicCoding() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 16);
    encoder.write(CodecTestUtils.wrap("stuff;"));
    encoder.write(CodecTestUtils.wrap("more stuff"));
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("stuff;more stuff", s);
    Assertions.assertEquals("[content length: 16; pos: 16; completed: true]", encoder.toString());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 50 with WritableByteChannelMock

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

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingTinyFragments.

@Test
public void testCodingFragmentBufferingTinyFragments() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 1);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));
    Mockito.verify(channel, Mockito.times(5)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(3)).flush(channel);
    Assertions.assertEquals(18, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff---more stuff", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) 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