use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestRequestParser method testConnectionClosedException.
@Test
public void testConnectionClosedException() throws Exception {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
final ClassicHttpRequest request = parser.parse(inBuffer, inputStream);
Assertions.assertNull(request);
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestRequestParser method testBasicMessageParsingLeadingEmptyLines.
@Test
public void testBasicMessageParsingLeadingEmptyLines() throws Exception {
final String s = "\r\n" + "\r\n" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(Http1Config.custom().setMaxEmptyLineCount(3).build());
final ClassicHttpRequest httprequest = parser.parse(inBuffer, inputStream);
Assertions.assertEquals(Method.GET.name(), httprequest.getMethod());
Assertions.assertEquals("/", httprequest.getPath());
final Header[] headers = httprequest.getHeaders();
Assertions.assertEquals(1, headers.length);
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedChunk.
@Test
public void testMalformedChunk() throws Exception {
final String s = "5\r\n01234----------------------------------------------------------" + "-----------------------------------------------------------------------------" + "-----------------------------------------------------------------------------";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(32, 32, 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(MalformedChunkCodingException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedChunkTruncatedChunk.
@Test
public void testMalformedChunkTruncatedChunk() throws Exception {
final String s = "3\r\n12";
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.assertEquals(2, decoder.read(dst));
Assertions.assertThrows(TruncatedChunkException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkDecoder method testReadingWitSmallBuffer.
@Test
public void testReadingWitSmallBuffer() throws Exception {
final String s = "10\r\n1234567890123456\r\n" + "40\r\n12345678901234561234567890123456" + "12345678901234561234567890123456\r\n0\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);
final ByteBuffer tmp = ByteBuffer.allocate(10);
int bytesRead = 0;
while (dst.hasRemaining() && !decoder.isCompleted()) {
final int i = decoder.read(tmp);
if (i > 0) {
bytesRead += i;
tmp.flip();
dst.put(tmp);
tmp.compact();
}
}
Assertions.assertEquals(80, bytesRead);
Assertions.assertEquals("12345678901234561234567890123456" + "12345678901234561234567890123456" + "1234567890123456", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
}
Aggregations