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();
}
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));
}
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));
}
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());
}
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);
}
Aggregations