use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testTooLongFooter.
@Test
public void testTooLongFooter() throws Exception {
final String s = "10\r\n1234567890123456\r\n" + "0\r\nFooter1: looooooooooooooooooooooooooooooooooooooooooooooooooooooog\r\n\r\n";
final ReadableByteChannel channel1 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(1024, 256, 0);
final BasicHttpTransportMetrics metrics1 = new BasicHttpTransportMetrics();
final ChunkDecoder decoder1 = new ChunkDecoder(channel1, inbuf1, metrics1);
final ByteBuffer dst = ByteBuffer.allocate(1024);
final int bytesRead = decoder1.read(dst);
Assertions.assertEquals(16, bytesRead);
Assertions.assertEquals("1234567890123456", CodecTestUtils.convert(dst));
final List<? extends Header> trailers = decoder1.getTrailers();
Assertions.assertNotNull(trailers);
Assertions.assertEquals(1, trailers.size());
final ReadableByteChannel channel2 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(1024, 256, 25, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics2 = new BasicHttpTransportMetrics();
final ChunkDecoder decoder2 = new ChunkDecoder(channel2, inbuf2, metrics2);
dst.clear();
Assertions.assertThrows(MessageConstraintException.class, () -> decoder2.read(dst));
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedFooters.
@Test
public void testMalformedFooters() 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);
final ByteBuffer dst = ByteBuffer.allocate(1024);
Assertions.assertThrows(IOException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testIncompleteChunkDecoding.
@Test
public void testIncompleteChunkDecoding() throws Exception {
final String[] chunks = { "10;", "key=\"value\"\r", "\n123456789012345", "6\r\n5\r\n12", "345\r\n6\r", "\nabcdef\r", "\n0\r\nFoot", "er1: abcde\r\nFooter2: f", "ghij\r\n\r\n" };
final ReadableByteChannel channel = new ReadableByteChannelMock(chunks, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ByteBuffer dst = ByteBuffer.allocate(1024);
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
int bytesRead = 0;
while (dst.hasRemaining() && !decoder.isCompleted()) {
final int i = decoder.read(dst);
if (i > 0) {
bytesRead += i;
}
}
Assertions.assertEquals(27, bytesRead);
Assertions.assertEquals("123456789012345612345abcdef", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
final List<? extends Header> trailers = decoder.getTrailers();
Assertions.assertEquals(2, trailers.size());
Assertions.assertEquals("Footer1", trailers.get(0).getName());
Assertions.assertEquals("abcde", trailers.get(0).getValue());
Assertions.assertEquals("Footer2", trailers.get(1).getName());
Assertions.assertEquals("fghij", trailers.get(1).getValue());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityDecoder method testDecodingFromSessionBuffer.
@Test
public void testDecodingFromSessionBuffer() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
inbuf.fill(channel);
Assertions.assertEquals(6, inbuf.length());
final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(6, bytesRead);
Assertions.assertEquals("stuff;", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
// doesn't count if from session buffer
Assertions.assertEquals(0, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(10, bytesRead);
Assertions.assertEquals("more stuff", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(10, metrics.getBytesTransferred());
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityDecoder method testDecodingFileWithBufferedSessionData.
@Test
public void testDecodingFileWithBufferedSessionData() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
final int i = inbuf.fill(channel);
Assertions.assertEquals(7, i);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long pos = 0;
while (!decoder.isCompleted()) {
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
// count everything except the initial 7 bytes that went to the session buffer
Assertions.assertEquals(testfile.length() - 7, metrics.getBytesTransferred());
}
Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
Aggregations