Search in sources :

Example 71 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testNonAsciiReadWriteLine.

@Test
public void testNonAsciiReadWriteLine() throws Exception {
    final String s1 = constructString(SWISS_GERMAN_HELLO);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.ISO_8859_1.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();
    outbuffer.writeLine(chbuffer, outputStream);
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    final long expected = ((s1.getBytes(StandardCharsets.ISO_8859_1).length + 2)) * 10 + 2;
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.ISO_8859_1.newDecoder());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        final int len = inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(len, SWISS_GERMAN_HELLO.length);
        Assertions.assertEquals(s1, chbuffer.toString());
    }
    chbuffer.clear();
    Assertions.assertEquals(0, inBuffer.readLine(chbuffer, inputStream));
    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 72 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testMalformedInputActionReport.

@Test
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);
    Assertions.assertThrows(CharacterCodingException.class, () -> inBuffer.readLine(chbuffer, inputStream));
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 73 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testLineLimit3.

// HTTPCORE-472
@Test
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);
    Assertions.assertEquals("012345678", chbuffer.toString());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 74 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testReadWriteBytes.

@Test
public void testReadWriteBytes() throws Exception {
    // make the buffer larger than that of outbuffer
    final byte[] out = new byte[40];
    for (int i = 0; i < out.length; i++) {
        out[i] = (byte) ('0' + i);
    }
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int off = 0;
    int remaining = out.length;
    while (remaining > 0) {
        int chunk = 10;
        if (chunk > remaining) {
            chunk = remaining;
        }
        outbuffer.write(out, off, chunk, outputStream);
        off += chunk;
        remaining -= chunk;
    }
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(out.length, bytesWritten);
    final byte[] tmp = outputStream.toByteArray();
    Assertions.assertEquals(out.length, tmp.length);
    for (int i = 0; i < out.length; i++) {
        Assertions.assertEquals(out[i], tmp[i]);
    }
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
    // these read operations will have no effect
    Assertions.assertEquals(0, inBuffer.read(null, 0, 10, inputStream));
    Assertions.assertEquals(0, inBuffer.read(null, inputStream));
    long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(0, bytesRead);
    final byte[] in = new byte[40];
    off = 0;
    remaining = in.length;
    while (remaining > 0) {
        int chunk = 10;
        if (chunk > remaining) {
            chunk = remaining;
        }
        final int readLen = inBuffer.read(in, off, chunk, inputStream);
        if (readLen == -1) {
            break;
        }
        off += readLen;
        remaining -= readLen;
    }
    for (int i = 0; i < out.length; i++) {
        Assertions.assertEquals(out[i], in[i]);
    }
    Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
    Assertions.assertEquals(-1, inBuffer.read(tmp, inputStream));
    bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(out.length, bytesRead);
}
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 75 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testReadLineFringeCase1.

@Test
public void testReadLineFringeCase1() throws Exception {
    final String s = "abc\r\n";
    final byte[] tmp = s.getBytes(StandardCharsets.US_ASCII);
    final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(128);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
    Assertions.assertEquals('a', inBuffer1.read(inputStream));
    Assertions.assertEquals('b', inBuffer1.read(inputStream));
    Assertions.assertEquals('c', inBuffer1.read(inputStream));
    Assertions.assertEquals('\r', inBuffer1.read(inputStream));
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    Assertions.assertEquals(0, inBuffer1.readLine(chbuffer, inputStream));
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) 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