@Test public void testAvailable() throws IOException { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 3L); Assert.assertEquals(0, in.available()); in.read(); Assert.assertEquals(2, in.available()); in.close(); }
@Test(expected=CharacterCodingException.class) public void testMalformedInputActionReport() throws Exception { final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1); final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder); final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); inBuffer.readLine(chbuffer, inputStream); }
@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); Assert.assertNotNull(headers); Assert.assertEquals(0, headers.length); }
@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); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.length); Assert.assertEquals("header1: stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString()); }
@Test(expected = NoHttpResponseException.class) public void testConnectionClosedException() throws Exception { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(); parser.parse(inBuffer, inputStream); }
@Test public void testMalformedInputActionReplace() throws Exception { final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1); final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder); final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); inBuffer.readLine(chbuffer, inputStream); Assert.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", chbuffer.toString()); }
@Test(expected = MalformedChunkCodingException.class) public void testCorruptChunkedInputStreamNegativeSize() throws IOException { final String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); in.read(); in.close(); }
@Test(expected = ConnectionClosedException.class) public void testConnectionClosedException() throws Exception { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(); parser.parse(inBuffer, inputStream); }
@Test public void testMalformedInputActionIgnore() throws Exception { final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1); final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder); final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); inBuffer.readLine(chbuffer, inputStream); Assert.assertEquals("Grezi_zm", chbuffer.toString()); }
@Test(expected=MalformedChunkCodingException.class) public void testCorruptChunkedInputStreamMissingLF() throws IOException { final String s = "5\r01234\r\n5\r\n56789\r\n0\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); in.read(); in.close(); }
@Test(expected = MalformedChunkCodingException.class) public void testCorruptChunkedInputStreamInvalidSize() throws IOException { final String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); in.read(); in.close(); }
@Test //HTTPCORE-472 public void testLineLimit3() throws Exception { final String s = "012345678\r\nblaaaaaaaaaaaaaaaaaah"; final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII); final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(128); final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp); final CharArrayBuffer chbuffer = new CharArrayBuffer(16); inBuffer1.readLine(chbuffer, inputStream); Assert.assertEquals("012345678", chbuffer.toString()); }
@Test(expected = MalformedChunkCodingException.class) public void testCorruptChunkedInputStreamInvalidFooter() throws IOException { final String s = "1\r\n0\r\n0\r\nstuff\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); in.read(); in.read(); in.close(); }
@Test public void testCorruptChunkedInputStreamClose() throws IOException { final String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); try { in.read(); Assert.fail("MalformedChunkCodingException expected"); } catch (final MalformedChunkCodingException ex) { } in.close(); }
@Test(expected=ConnectionClosedException.class) public void testChunkedInputStreamNoClosingChunk() throws IOException { final String s = "5\r\n01234\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); final byte[] tmp = new byte[5]; Assert.assertEquals(5, in.read(tmp)); in.read(); in.close(); }
@Test(expected=MalformedChunkCodingException.class) public void testCorruptChunkedInputStreamTruncatedCRLF() throws IOException { final String s = "5\r\n01234"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); final byte[] tmp = new byte[5]; Assert.assertEquals(5, in.read(tmp)); in.read(); in.close(); }
@Test(expected = TruncatedChunkException.class) public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException { final String s = "3\r\n12"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); final byte[] buffer = new byte[300]; Assert.assertEquals(2, in.read(buffer)); in.read(buffer); in.close(); }
@Test public void testAvailable() throws IOException { final String s = "5\r\n12345\r\n0\r\n"; final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); Assert.assertEquals(0, in.available()); in.read(); Assert.assertEquals(4, in.available()); in.close(); }
@Test public void testAvailable() throws Exception { final byte[] input = new byte[] {'a', 'b', 'c'}; final ByteArrayInputStream inputStream = new ByteArrayInputStream(input); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, 1024, null); final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream); in.read(); Assert.assertEquals(2, in.available()); in.close(); }
@Test public void testAvailableInStream() throws Exception { final byte[] input = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}; final ByteArrayInputStream inputStream = new ByteArrayInputStream(input); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, 1024, null); final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream); final byte[] tmp = new byte[3]; Assert.assertEquals(3, in.read(tmp)); Assert.assertEquals(3, in.available()); in.close(); }