use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testInvalidConstructor.
@Test
public void testInvalidConstructor() {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(null, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(channel, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(channel, outbuf, null, 10));
Assertions.assertThrows(IllegalArgumentException.class, () -> new LengthDelimitedEncoder(channel, outbuf, metrics, -10));
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingNoFragmentBuffering.
@Test
public void testCodingNoFragmentBuffering() throws Exception {
final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
chbuffer.append("header");
outbuf.writeLine(chbuffer);
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 0);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any());
Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
Assertions.assertEquals(13, metrics.getBytesTransferred());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("header\r\nstuff", s);
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testInvalidConstructor.
@Test
public void testInvalidConstructor() {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(null, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(channel, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(channel, inbuf, null, 10));
Assertions.assertThrows(IllegalArgumentException.class, () -> new LengthDelimitedDecoder(channel, inbuf, metrics, -10));
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFileWithOffsetAndBufferedSessionData.
@Test
public void testDecodingFileWithOffsetAndBufferedSessionData() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; ", "more stuff; ", "a lot more stuff!" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 36);
final int i = inbuf.fill(channel);
Assertions.assertEquals(7, i);
final byte[] beginning = "beginning; ".getBytes(StandardCharsets.US_ASCII);
createTempFile();
RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
testfile.write(beginning);
} finally {
testfile.close();
}
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
long pos = beginning.length;
while (!decoder.isCompleted()) {
if (testfile.length() < pos) {
testfile.setLength(pos);
}
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
} finally {
testfile.close();
}
// count everything except the initial 7 bytes that went to the session buffer
Assertions.assertEquals(this.tmpfile.length() - 7 - beginning.length, metrics.getBytesTransferred());
Assertions.assertEquals("beginning; stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testInvalidInput.
@Test
public void testInvalidInput() throws Exception {
final String s = "stuff";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 3);
Assertions.assertThrows(NullPointerException.class, () -> decoder.read(null));
}
Aggregations