Search in sources :

Example 6 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testHeaderEntrySizeNonAscii.

@Test
public void testHeaderEntrySizeNonAscii() throws Exception {
    final ByteArrayBuffer buffer = new ByteArrayBuffer(128);
    final Header header = new BasicHeader("hello", constructHelloString(SWISS_GERMAN_HELLO, 1));
    final OutboundDynamicTable outboundTable1 = new OutboundDynamicTable();
    final HPackEncoder encoder1 = new HPackEncoder(outboundTable1, StandardCharsets.ISO_8859_1);
    final InboundDynamicTable inboundTable1 = new InboundDynamicTable();
    final HPackDecoder decoder1 = new HPackDecoder(inboundTable1, StandardCharsets.ISO_8859_1);
    encoder1.setMaxTableSize(48);
    decoder1.setMaxTableSize(48);
    encoder1.encodeHeader(buffer, header);
    assertHeaderEquals(header, decoder1.decodeHeader(wrap(buffer)));
    Assertions.assertEquals(1, outboundTable1.dynamicLength());
    Assertions.assertEquals(1, inboundTable1.dynamicLength());
    assertHeaderEquals(header, outboundTable1.getDynamicEntry(0));
    assertHeaderEquals(header, inboundTable1.getDynamicEntry(0));
    buffer.clear();
    final OutboundDynamicTable outboundTable2 = new OutboundDynamicTable();
    final HPackEncoder encoder2 = new HPackEncoder(outboundTable2, StandardCharsets.UTF_8);
    final InboundDynamicTable inboundTable2 = new InboundDynamicTable();
    final HPackDecoder decoder2 = new HPackDecoder(inboundTable2, StandardCharsets.UTF_8);
    encoder2.setMaxTableSize(48);
    decoder2.setMaxTableSize(48);
    encoder2.encodeHeader(buffer, header);
    assertHeaderEquals(header, decoder2.decodeHeader(wrap(buffer)));
    Assertions.assertEquals(0, outboundTable2.dynamicLength());
    Assertions.assertEquals(0, inboundTable2.dynamicLength());
}
Also used : BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testLiteralHeaderNeverIndexedEncodingRFC7541Examples.

@Test
public void testLiteralHeaderNeverIndexedEncodingRFC7541Examples() throws Exception {
    final OutboundDynamicTable dynamicTable = new OutboundDynamicTable();
    final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
    final ByteArrayBuffer buf = new ByteArrayBuffer(128);
    final Header header = new BasicHeader("password", "secret", true);
    encoder.encodeLiteralHeader(buf, null, header, HPackRepresentation.NEVER_INDEXED, false);
    final byte[] expected = createByteArray(0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74);
    Assertions.assertArrayEquals(expected, buf.toByteArray());
}
Also used : BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testPlainStringDecodingTruncated.

@Test
public void testPlainStringDecodingTruncated() throws Exception {
    final ByteBuffer src = createByteBuffer(0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65);
    final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
    Assertions.assertThrows(HPackException.class, () -> HPackDecoder.decodePlainString(buffer, src));
}
Also used : ByteBuffer(java.nio.ByteBuffer) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 9 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testComplexStringCoding1.

@Test
public void testComplexStringCoding1() throws Exception {
    for (final Charset charset : new Charset[] { StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8, StandardCharsets.UTF_16 }) {
        final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
        final StringBuilder strBuf = new StringBuilder();
        final HPackEncoder encoder = new HPackEncoder(charset);
        final HPackDecoder decoder = new HPackDecoder(charset);
        for (int n = 0; n < 10; n++) {
            final String hello = constructHelloString(SWISS_GERMAN_HELLO, 1 + 10 * n);
            for (final boolean b : new boolean[] { false, true }) {
                buffer.clear();
                encoder.encodeString(buffer, hello, b);
                strBuf.setLength(0);
                decoder.decodeString(wrap(buffer), strBuf);
                final String helloBack = strBuf.toString();
                Assertions.assertEquals(hello, helloBack, "charset: " + charset + "; huffman: " + b);
            }
        }
    }
}
Also used : Charset(java.nio.charset.Charset) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with ByteArrayBuffer

use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.

the class TestHPackCoding method testComplexStringCoding2.

@Test
public void testComplexStringCoding2() throws Exception {
    for (final Charset charset : new Charset[] { Charset.forName("KOI8-R"), StandardCharsets.UTF_8, StandardCharsets.UTF_16 }) {
        final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
        final StringBuilder strBuf = new StringBuilder();
        final HPackEncoder encoder = new HPackEncoder(charset);
        final HPackDecoder decoder = new HPackDecoder(charset);
        for (int n = 0; n < 10; n++) {
            final String hello = constructHelloString(RUSSIAN_HELLO, 1 + 10 * n);
            for (final boolean b : new boolean[] { false, true }) {
                buffer.clear();
                strBuf.setLength(0);
                encoder.encodeString(buffer, hello, b);
                decoder.decodeString(wrap(buffer), strBuf);
                final String helloBack = strBuf.toString();
                Assertions.assertEquals(hello, helloBack, "charset: " + charset + "; huffman: " + b);
            }
        }
    }
}
Also used : Charset(java.nio.charset.Charset) ByteArrayBuffer(org.apache.hc.core5.util.ByteArrayBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayBuffer (org.apache.hc.core5.util.ByteArrayBuffer)37 Test (org.junit.jupiter.api.Test)26 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)11 Header (org.apache.hc.core5.http.Header)10 ByteBuffer (java.nio.ByteBuffer)9 InputStream (java.io.InputStream)4 HttpCacheEntry (org.apache.hc.client5.http.cache.HttpCacheEntry)4 Charset (java.nio.charset.Charset)2 ResourceIOException (org.apache.hc.client5.http.cache.ResourceIOException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)2 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1