use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class TestHPackCoding method testHuffmanDecodingRFC7541Examples.
@Test
public void testHuffmanDecodingRFC7541Examples() throws Exception {
final ByteBuffer src = createByteBuffer(0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff);
final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
HPackDecoder.decodeHuffman(buffer, src);
Assertions.assertEquals(new String(buffer.array(), 0, buffer.length(), StandardCharsets.US_ASCII), "www.example.com");
Assertions.assertFalse(src.hasRemaining(), "Decoding completed");
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class TestHPackCoding method testIntegerEncodingRFC7541Examples.
@Test
public void testIntegerEncodingRFC7541Examples() throws Exception {
final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
HPackEncoder.encodeInt(buffer, 5, 10, 0x0);
Assertions.assertEquals(1, buffer.length());
Assertions.assertEquals(0b00001010, buffer.byteAt(0) & 0xFF);
buffer.clear();
HPackEncoder.encodeInt(buffer, 5, 1337, 0x0);
Assertions.assertEquals(3, buffer.length());
Assertions.assertEquals(0b00011111, buffer.byteAt(0) & 0xFF);
Assertions.assertEquals(0b10011010, buffer.byteAt(1) & 0xFF);
Assertions.assertEquals(0b00001010, buffer.byteAt(2) & 0xFF);
buffer.clear();
HPackEncoder.encodeInt(buffer, 8, 42, 0x0);
Assertions.assertEquals(1, buffer.length());
Assertions.assertEquals(0b00101010, buffer.byteAt(0) & 0xFF);
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class TestHPackCoding method testIntegerCoding.
@Test
public void testIntegerCoding() throws Exception {
final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
for (int n = 4; n <= 8; n++) {
buffer.clear();
HPackEncoder.encodeInt(buffer, n, 10, 0x0);
Assertions.assertEquals(10, HPackDecoder.decodeInt(wrap(buffer), n));
buffer.clear();
HPackEncoder.encodeInt(buffer, n, 123456, 0x0);
Assertions.assertEquals(123456, HPackDecoder.decodeInt(wrap(buffer), n));
buffer.clear();
HPackEncoder.encodeInt(buffer, n, Integer.MAX_VALUE, 0x0);
Assertions.assertEquals(Integer.MAX_VALUE, HPackDecoder.decodeInt(wrap(buffer), n));
}
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class TestHPackCoding method testIndexedHeaderEncodingRFC7541Examples.
@Test
public void testIndexedHeaderEncodingRFC7541Examples() throws Exception {
final OutboundDynamicTable dynamicTable = new OutboundDynamicTable();
final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
final ByteArrayBuffer buf = new ByteArrayBuffer(128);
encoder.encodeIndex(buf, 2);
final byte[] expected = createByteArray(0x82);
Assertions.assertArrayEquals(expected, buf.toByteArray());
}
use of org.apache.hc.core5.util.ByteArrayBuffer in project httpcomponents-core by apache.
the class TestHPackCoding method testRequestEncodingWithHuffmanRFC7541Examples.
@Test
public void testRequestEncodingWithHuffmanRFC7541Examples() throws Exception {
final OutboundDynamicTable dynamicTable = new OutboundDynamicTable();
final HPackEncoder encoder = new HPackEncoder(dynamicTable, StandardCharsets.US_ASCII);
final ByteArrayBuffer buf = new ByteArrayBuffer(256);
final List<Header> headers1 = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":path", "/"), new BasicHeader(":authority", "www.example.com"));
encoder.encodeHeaders(buf, headers1, false, true);
final byte[] expected1 = createByteArray(0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff);
Assertions.assertArrayEquals(expected1, buf.toByteArray());
Assertions.assertEquals(1, dynamicTable.dynamicLength());
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), dynamicTable.getDynamicEntry(0));
Assertions.assertEquals(57, dynamicTable.getCurrentSize());
final List<Header> headers2 = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":path", "/"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("cache-control", "no-cache"));
buf.clear();
encoder.encodeHeaders(buf, headers2, false, true);
final byte[] expected2 = createByteArray(0x82, 0x86, 0x84, 0xbe, 0x58, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf);
Assertions.assertArrayEquals(expected2, buf.toByteArray());
Assertions.assertEquals(2, dynamicTable.dynamicLength());
assertHeaderEquals(new BasicHeader("cache-control", "no-cache"), dynamicTable.getDynamicEntry(0));
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), dynamicTable.getDynamicEntry(1));
Assertions.assertEquals(110, dynamicTable.getCurrentSize());
final List<Header> headers3 = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "https"), new BasicHeader(":path", "/index.html"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom-key", "custom-value"));
buf.clear();
encoder.encodeHeaders(buf, headers3, false, true);
final byte[] expected3 = createByteArray(0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf);
Assertions.assertArrayEquals(expected3, buf.toByteArray());
Assertions.assertEquals(3, dynamicTable.dynamicLength());
assertHeaderEquals(new BasicHeader("custom-key", "custom-value"), dynamicTable.getDynamicEntry(0));
assertHeaderEquals(new BasicHeader("cache-control", "no-cache"), dynamicTable.getDynamicEntry(1));
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), dynamicTable.getDynamicEntry(2));
Assertions.assertEquals(164, dynamicTable.getCurrentSize());
}
Aggregations