use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder method testTrailers.
@Test
public void testTrailers() throws IOException {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics, 0);
encoder.write(CodecTestUtils.wrap("1"));
encoder.write(CodecTestUtils.wrap("23"));
encoder.complete(Arrays.asList(new BasicHeader("E", ""), new BasicHeader("Y", "Z")));
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertTrue(encoder.isCompleted());
Assertions.assertEquals("1\r\n1\r\n2\r\n23\r\n0\r\nE: \r\nY: Z\r\n\r\n", s);
Assertions.assertEquals("[chunk-coded; completed: true]", encoder.toString());
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder method testLimitedChannel.
// See HTTPCORE-239
@Test
public void testLimitedChannel() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(16, 16);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
// fill up the channel
channel.write(CodecTestUtils.wrap("0123456789ABCDEF"));
// fill up the out buffer
outbuf.write(CodecTestUtils.wrap("0123456789ABCDEF"));
final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
Assertions.assertEquals(0, encoder.write(src));
Assertions.assertEquals(0, encoder.write(src));
Assertions.assertEquals(0, encoder.write(src));
// should not be able to copy any bytes, until we flush the channel and buffer
channel.reset();
outbuf.flush(channel);
channel.reset();
Assertions.assertEquals(10, encoder.write(src));
channel.flush();
Assertions.assertEquals(6, encoder.write(src));
channel.flush();
Assertions.assertEquals(0, encoder.write(src));
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("4\r\n0123\r\n4\r\n4567\r\n2\r\n89\r\n4\r\nABCD\r\n2\r\nEF\r\n", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testUnmappableInputActionReport.
@Test
public void testUnmappableInputActionReport() throws Exception {
final String s = "This text contains a circumflex \u0302 !!!";
final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
chbuffer.append(s);
Assertions.assertThrows(CharacterCodingException.class, () -> outbuffer.writeLine(chbuffer, outputStream));
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLine.
@Test
public void testMultibyteCodedReadWriteLine() throws Exception {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final String s2 = constructString(RUSSIAN_HELLO);
final String s3 = "Like hello and stuff";
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
for (int i = 0; i < 10; i++) {
chbuffer.clear();
chbuffer.append(s1);
outbuffer.writeLine(chbuffer, outputStream);
chbuffer.clear();
chbuffer.append(s2);
outbuffer.writeLine(chbuffer, outputStream);
chbuffer.clear();
chbuffer.append(s3);
outbuffer.writeLine(chbuffer, outputStream);
}
outbuffer.flush(outputStream);
final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
final long expected = ((s1.getBytes(StandardCharsets.UTF_8).length + 2) + (s2.getBytes(StandardCharsets.UTF_8).length + 2) + (s3.getBytes(StandardCharsets.UTF_8).length + 2)) * 10;
Assertions.assertEquals(expected, bytesWritten);
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
for (int i = 0; i < 10; i++) {
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s1, chbuffer.toString());
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s2, chbuffer.toString());
chbuffer.clear();
inBuffer.readLine(chbuffer, inputStream);
Assertions.assertEquals(s3, chbuffer.toString());
}
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
chbuffer.clear();
Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
Assertions.assertEquals(expected, bytesRead);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestSessionInOutBuffers method testUnmappableInputActionReplace.
@Test
public void testUnmappableInputActionReplace() throws Exception {
final String s = "This text contains a circumflex \u0302 !!!";
final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
chbuffer.append(s);
outbuffer.writeLine(chbuffer, outputStream);
outbuffer.flush(outputStream);
final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
Assertions.assertEquals("This text contains a circumflex ? !!!\r\n", result);
}
Aggregations