Search in sources :

Example 6 with Method

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

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());
}
Also used : BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) ByteBuffer(java.nio.ByteBuffer) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 8 with Method

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");
}
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) Test(org.junit.jupiter.api.Test)

Example 9 with Method

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");
}
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) Test(org.junit.jupiter.api.Test)

Example 10 with Method

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());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Aggregations

Header (org.apache.hc.core5.http.Header)38 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 Test (org.junit.jupiter.api.Test)29 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)16 IOException (java.io.IOException)15 URI (java.net.URI)14 URISyntaxException (java.net.URISyntaxException)14 Map (java.util.Map)14 HttpEntity (org.apache.hc.core5.http.HttpEntity)13 HttpHost (org.apache.hc.core5.http.HttpHost)12 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)10 ContentType (org.apache.hc.core5.http.ContentType)9 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)9 HttpRequest (org.apache.hc.core5.http.HttpRequest)8 HashMap (java.util.HashMap)7 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)7 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)7 SocketTimeoutException (java.net.SocketTimeoutException)6 ArrayList (java.util.ArrayList)6 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)6