use of org.apache.hc.core5.http.io.SessionInputBuffer 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.io.SessionInputBuffer 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.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestRequestParser method testBasicMessageParsingTooManyLeadingEmptyLines.
@Test
public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
final String s = "\r\n" + "\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());
Assertions.assertThrows(RequestHeaderFieldsTooLargeException.class, () -> parser.parse(inBuffer, inputStream));
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestResponseParser method testBasicMessageParsingTooManyLeadingEmptyLines.
@Test
public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
final String s = "\r\n" + "\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Server: whatever\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 DefaultHttpResponseParser parser = new DefaultHttpResponseParser(Http1Config.custom().setMaxEmptyLineCount(3).build());
Assertions.assertThrows(MessageConstraintException.class, () -> parser.parse(inBuffer, inputStream));
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestResponseParser method testBasicMessageParsing.
@Test
public void testBasicMessageParsing() throws Exception {
final String s = "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "Date: some date\r\n" + "Set-Cookie: c1=stuff\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 DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
Assertions.assertEquals(200, httpresponse.getCode());
Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
final Header[] headers = httpresponse.getHeaders();
Assertions.assertEquals(3, headers.length);
}
Aggregations