Search in sources :

Example 86 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.

the class TestMessageParser method testMaxHeaderCountForFoldedHeader.

@Test
public void testMaxHeaderCountForFoldedHeader() throws Exception {
    final String s = "header1: stuff\r\n" + " stuff \r\n" + " 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());
    Assertions.assertThrows(IOException.class, () -> AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, 15, null));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 87 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.

the class TestMessageParser method testParsingHeader.

@Test
public void testParsingHeader() throws Exception {
    final String s = "header1: stuff; param1 = value1; param2 = \"value 2\" \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 Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(1, headers.length);
    Assertions.assertEquals("header1: stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 88 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.

the class TestMessageParser method testParsingMalformedFirstHeader.

@Test
public void testParsingMalformedFirstHeader() throws Exception {
    final String s = "    header1: stuff\r\n" + "header2: stuff \r\n";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(2, headers.length);
    Assertions.assertEquals("header1", headers[0].getName());
    Assertions.assertEquals("stuff", headers[0].getValue());
    Assertions.assertEquals("header2", headers[1].getName());
    Assertions.assertEquals("stuff", headers[1].getValue());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 89 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.

the class TestMessageParser method testEmptyDataStream.

@Test
public void testEmptyDataStream() throws Exception {
    final String s = "";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(0, headers.length);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 90 with SessionInputBuffer

use of org.apache.hc.core5.http.nio.SessionInputBuffer in project httpcomponents-core by apache.

the class TestRequestParser method testMessageParsingTimeout.

@Test
public void testMessageParsingTimeout() throws Exception {
    final String s = "GET \000/ HTTP/1.1\r\000\n" + "Host: loca\000lhost\r\n" + "User-Agent: whatever\r\n" + "Coo\000kie: c1=stuff\r\n" + "\000\r\n";
    final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
    int timeoutCount = 0;
    ClassicHttpRequest httprequest = null;
    for (int i = 0; i < 10; i++) {
        try {
            httprequest = parser.parse(inBuffer, inputStream);
            break;
        } catch (final InterruptedIOException ex) {
            timeoutCount++;
        }
    }
    Assertions.assertNotNull(httprequest);
    Assertions.assertEquals(5, timeoutCount);
    Assertions.assertEquals(Method.GET.name(), httprequest.getMethod());
    Assertions.assertEquals("/", httprequest.getPath());
    final Header[] headers = httprequest.getHeaders();
    Assertions.assertEquals(3, headers.length);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Header(org.apache.hc.core5.http.Header) 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