Search in sources :

Example 96 with BasicHttpTransportMetrics

use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.

the class TestChunkEncoder method testCodingEmptyBuffer.

@Test
public void testCodingEmptyBuffer() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    encoder.write(CodecTestUtils.wrap("12345"));
    encoder.write(CodecTestUtils.wrap("678"));
    encoder.write(CodecTestUtils.wrap("90"));
    final ByteBuffer empty = ByteBuffer.allocate(100);
    empty.flip();
    encoder.write(empty);
    encoder.write(null);
    encoder.complete();
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", 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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 97 with BasicHttpTransportMetrics

use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.

the class TestChunkEncoder method testChunkNoExceed.

@Test
public void testChunkNoExceed() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    encoder.write(CodecTestUtils.wrap("1234"));
    encoder.complete();
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("4\r\n1234\r\n0\r\n\r\n", 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)

Example 98 with BasicHttpTransportMetrics

use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.

the class TestChunkEncoder method testBufferFragments.

@Test
public void testBufferFragments() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(1024));
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 1024);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics, 1024);
    Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
    Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
    Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
    Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("10\r\n0123456789ABCDEF\r\n10\r\n0123456789ABCDEF\r\n" + "10\r\n0123456789ABCDEF\r\n", 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)

Example 99 with BasicHttpTransportMetrics

use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.

the class TestChunkDecoder method testInvalidInput.

@Test
public void testInvalidInput() throws Exception {
    final String s = "10;key=\"value\"\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\r\n0\r\nFooter1 abcde\r\n\r\n";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    Assertions.assertThrows(NullPointerException.class, () -> decoder.read(null));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Aggregations

BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)99 Test (org.junit.jupiter.api.Test)98 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)51 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)50 ReadableByteChannel (java.nio.channels.ReadableByteChannel)44 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)44 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)44 ByteBuffer (java.nio.ByteBuffer)34 RandomAccessFile (java.io.RandomAccessFile)23 FileChannel (java.nio.channels.FileChannel)23 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)11 OutputStream (java.io.OutputStream)3 Http1Config (org.apache.hc.core5.http.config.Http1Config)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MalformedChunkCodingException (org.apache.hc.core5.http.MalformedChunkCodingException)2 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1