Search in sources :

Example 76 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testLineLimit2.

@Test
public void testLineLimit2() throws Exception {
    final String s = "just a line\r\n";
    final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII);
    // no limit
    final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(25);
    final InputStream inputStream1 = new ByteArrayInputStream(tmp);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    inBuffer1.readLine(chbuffer, inputStream1);
    final long bytesRead = inBuffer1.getMetrics().getBytesTransferred();
    Assertions.assertEquals(13, bytesRead);
    // 10 char limit
    final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(25, 10);
    final InputStream inputStream2 = new ByteArrayInputStream(tmp);
    chbuffer.clear();
    Assertions.assertThrows(MessageConstraintException.class, () -> inBuffer2.readLine(chbuffer, inputStream2));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 77 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testBasicBufferProperties.

@Test
public void testBasicBufferProperties() throws Exception {
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
    Assertions.assertEquals(16, inBuffer.capacity());
    Assertions.assertEquals(16, inBuffer.available());
    Assertions.assertEquals(0, inBuffer.length());
    inBuffer.read(inputStream);
    Assertions.assertEquals(14, inBuffer.available());
    Assertions.assertEquals(2, inBuffer.length());
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Assertions.assertEquals(16, outbuffer.capacity());
    Assertions.assertEquals(16, outbuffer.available());
    Assertions.assertEquals(0, outbuffer.length());
    outbuffer.write(new byte[] { 1, 2, 3 }, outputStream);
    Assertions.assertEquals(13, outbuffer.available());
    Assertions.assertEquals(3, outbuffer.length());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 78 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testComplexReadWriteLine.

@Test
public void testComplexReadWriteLine() throws Exception {
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outbuffer.write(new byte[] { 'a', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\r', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\r', '\r', '\n' }, outputStream);
    outbuffer.write(new byte[] { '\n' }, outputStream);
    // these write operations should have no effect
    outbuffer.write(null, outputStream);
    outbuffer.write(null, 0, 12, outputStream);
    outbuffer.flush(outputStream);
    long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8, bytesWritten);
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 14; i++) {
        buffer.append("a");
    }
    final String s1 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2, bytesWritten);
    buffer.setLength(0);
    for (int i = 0; i < 15; i++) {
        buffer.append("a");
    }
    final String s2 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2, bytesWritten);
    buffer.setLength(0);
    for (int i = 0; i < 16; i++) {
        buffer.append("a");
    }
    final String s3 = buffer.toString();
    buffer.append("\r\n");
    outbuffer.write(buffer.toString().getBytes(StandardCharsets.US_ASCII), outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2, bytesWritten);
    outbuffer.write(new byte[] { 'a' }, outputStream);
    outbuffer.flush(outputStream);
    bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2 + 1, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("\r", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("", chbuffer.toString());
    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();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(bytesWritten, bytesRead);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 79 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testBasicReadWriteLineLargeBuffer.

@Test
public void testBasicReadWriteLineLargeBuffer() throws Exception {
    final String[] teststrs = new String[5];
    teststrs[0] = "Hello";
    teststrs[1] = "This string should be much longer than the size of the output buffer " + "which is only 16 bytes for this test";
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 15; i++) {
        buffer.append("123456789 ");
    }
    buffer.append("and stuff like that");
    teststrs[2] = buffer.toString();
    teststrs[3] = "";
    teststrs[4] = "And goodbye";
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (final String teststr : teststrs) {
        chbuffer.clear();
        chbuffer.append(teststr);
        outbuffer.writeLine(chbuffer, outputStream);
    }
    // these write operations should have no effect
    outbuffer.writeLine(null, outputStream);
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    long expected = 0;
    for (final String teststr : teststrs) {
        expected += (teststr.length() + 2);
    }
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(1024);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (final String teststr : teststrs) {
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(teststr, 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);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 80 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testLineLimit.

@Test
public void testLineLimit() throws Exception {
    final String s = "a very looooooooooooooooooooooooooooooooooooooooooong line\r\n";
    final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII);
    // no limit
    final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(5);
    final InputStream inputStream1 = new ByteArrayInputStream(tmp);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    inBuffer1.readLine(chbuffer, inputStream1);
    final long bytesRead = inBuffer1.getMetrics().getBytesTransferred();
    Assertions.assertEquals(60, bytesRead);
    // 15 char limit
    final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(5, 15);
    final InputStream inputStream2 = new ByteArrayInputStream(tmp);
    chbuffer.clear();
    Assertions.assertThrows(MessageConstraintException.class, () -> inBuffer2.readLine(chbuffer, inputStream2));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) 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