Search in sources :

Example 31 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 32 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 33 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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));
}
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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 34 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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));
}
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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 35 with SessionInputBuffer

use of org.apache.hc.core5.http.io.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());
}
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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)123 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)62 ReadableByteChannel (java.nio.channels.ReadableByteChannel)61 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)61 ByteArrayInputStream (java.io.ByteArrayInputStream)58 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)47 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 ByteBuffer (java.nio.ByteBuffer)30 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)25 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Header (org.apache.hc.core5.http.Header)13 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)10 InputStream (java.io.InputStream)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 InterruptedIOException (java.io.InterruptedIOException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4