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());
}
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());
}
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));
}
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);
}
}
}
}
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);
}
}
}
}
Aggregations