Search in sources :

Example 26 with SessionInputBuffer

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

the class TestIdentityInputStream method testBasicRead.

@Test
public void testBasicRead() throws Exception {
    final byte[] input = new byte[] { 'a', 'b', 'c' };
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
    final byte[] tmp = new byte[2];
    Assertions.assertEquals(2, in.read(tmp, 0, tmp.length));
    Assertions.assertEquals('a', tmp[0]);
    Assertions.assertEquals('b', tmp[1]);
    Assertions.assertEquals('c', in.read());
    Assertions.assertEquals(-1, in.read(tmp, 0, tmp.length));
    Assertions.assertEquals(-1, in.read());
    Assertions.assertEquals(-1, in.read(tmp, 0, tmp.length));
    Assertions.assertEquals(-1, in.read());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 27 with SessionInputBuffer

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

the class TestMessageParser method testParsingInvalidHeaders.

@Test
public void testParsingInvalidHeaders() throws Exception {
    final String s1 = "    stuff\r\n" + "header1: stuff\r\n" + "\r\n";
    final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s1.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    Assertions.assertThrows(ProtocolException.class, () -> AbstractMessageParser.parseHeaders(inBuffer1, inputStream1, -1, -1, null));
    final String s2 = "  :  stuff\r\n" + "header1: stuff\r\n" + "\r\n";
    final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s2.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    Assertions.assertThrows(ProtocolException.class, () -> AbstractMessageParser.parseHeaders(inBuffer2, inputStream2, -1, -1, null));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 28 with SessionInputBuffer

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

the class TestMessageParser method testMaxHeaderCount.

@Test
public void testMaxHeaderCount() throws Exception {
    final String s = "header1: stuff\r\n" + "header2: stuff \r\n" + "header3: 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, -1, null));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 29 with SessionInputBuffer

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

the class TestMessageParser method testBasicHeaderParsing.

@Test
public void testBasicHeaderParsing() throws Exception {
    final String s = "header1: stuff\r\n" + "header2:  stuff \r\n" + "header3: stuff\r\n" + "     and more stuff\r\n" + "\t and even more stuff\r\n" + "     \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(3, 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());
    Assertions.assertEquals("header3", headers[2].getName());
    Assertions.assertEquals("stuff and more stuff and even more stuff", headers[2].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 30 with SessionInputBuffer

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

the class TestRequestParser method testBasicMessageParsing.

@Test
public void testBasicMessageParsing() throws Exception {
    final String s = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "User-Agent: whatever\r\n" + "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 DefaultHttpRequestParser parser = new DefaultHttpRequestParser();
    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(3, 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)

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