use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.
the class TestHPackCoding method testRequestEncodingWithoutHuffmanRFC7541Examples.
@Test
public void testRequestEncodingWithoutHuffmanRFC7541Examples() 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, false);
final byte[] expected1 = createByteArray(0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d);
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, false);
final byte[] expected2 = createByteArray(0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65);
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, false);
final byte[] expected3 = createByteArray(0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65);
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());
}
use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.
the class TestHPackCoding method testRequestDecodingWithoutHuffmanRFC7541Examples.
@Test
public void testRequestDecodingWithoutHuffmanRFC7541Examples() throws Exception {
final ByteBuffer src1 = createByteBuffer(0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d);
final InboundDynamicTable dynamicTable = new InboundDynamicTable();
final HPackDecoder decoder = new HPackDecoder(dynamicTable, StandardCharsets.US_ASCII);
final List<Header> headers1 = decoder.decodeHeaders(src1);
Assertions.assertEquals(4, headers1.size());
assertHeaderEquals(new BasicHeader(":method", "GET"), headers1.get(0));
assertHeaderEquals(new BasicHeader(":scheme", "http"), headers1.get(1));
assertHeaderEquals(new BasicHeader(":path", "/"), headers1.get(2));
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), headers1.get(3));
Assertions.assertEquals(1, dynamicTable.dynamicLength());
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), dynamicTable.getDynamicEntry(0));
Assertions.assertEquals(57, dynamicTable.getCurrentSize());
final ByteBuffer src2 = createByteBuffer(0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65);
final List<Header> headers2 = decoder.decodeHeaders(src2);
Assertions.assertEquals(5, headers2.size());
assertHeaderEquals(new BasicHeader(":method", "GET"), headers2.get(0));
assertHeaderEquals(new BasicHeader(":scheme", "http"), headers2.get(1));
assertHeaderEquals(new BasicHeader(":path", "/"), headers2.get(2));
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), headers2.get(3));
assertHeaderEquals(new BasicHeader("cache-control", "no-cache"), headers2.get(4));
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 ByteBuffer src3 = createByteBuffer(0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65);
final List<Header> headers3 = decoder.decodeHeaders(src3);
Assertions.assertEquals(5, headers3.size());
assertHeaderEquals(new BasicHeader(":method", "GET"), headers3.get(0));
assertHeaderEquals(new BasicHeader(":scheme", "https"), headers3.get(1));
assertHeaderEquals(new BasicHeader(":path", "/index.html"), headers3.get(2));
assertHeaderEquals(new BasicHeader(":authority", "www.example.com"), headers3.get(3));
assertHeaderEquals(new BasicHeader("custom-key", "custom-value"), headers3.get(4));
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());
}
use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsMissingPath.
@Test
public void testConvertFromFieldsMissingPath() throws Exception {
final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom", "value"));
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(headers), "Mandatory request header ':path' not found");
}
use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsMultipleMethod.
@Test
public void testConvertFromFieldsMultipleMethod() throws Exception {
final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("custom", "value"));
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
Assertions.assertThrows(HttpException.class, () -> converter.convert(headers), "Multiple ':method' request headers are illegal");
}
use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.
the class TestDefaultH2RequestConverter method testConvertFromFieldsTETrailerHeader.
@Test
public void testConvertFromFieldsTETrailerHeader() throws Exception {
final List<Header> headers = Arrays.asList(new BasicHeader(":method", "GET"), new BasicHeader(":scheme", "http"), new BasicHeader(":authority", "www.example.com"), new BasicHeader(":path", "/"), new BasicHeader("te", "trailers"));
final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
final HttpRequest request = converter.convert(headers);
Assertions.assertNotNull(request);
Assertions.assertEquals("GET", request.getMethod());
Assertions.assertEquals("http", request.getScheme());
Assertions.assertEquals(new URIAuthority("www.example.com"), request.getAuthority());
Assertions.assertEquals("/", request.getPath());
final Header[] allHeaders = request.getHeaders();
Assertions.assertEquals(1, allHeaders.length);
Assertions.assertEquals("te", allHeaders[0].getName());
Assertions.assertEquals("trailers", allHeaders[0].getValue());
}
Aggregations