Search in sources :

Example 11 with Method

use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsConnectPresentScheme.

@Test
public void testConvertFromFieldsConnectPresentScheme() throws Exception {
    final List<Header> headers = Arrays.asList(new BasicHeader(":method", "CONNECT"), 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), "Header ':scheme' must not be set for CONNECT request");
}
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 12 with Method

use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsConnect.

@Test
public void testConvertFromFieldsConnect() throws Exception {
    final List<Header> headers = Arrays.asList(new BasicHeader(":method", "CONNECT"), new BasicHeader(":authority", "www.example.com"), new BasicHeader("custom", "value"));
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    converter.convert(headers);
}
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 13 with Method

use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromMessageBasic.

@Test
public void testConvertFromMessageBasic() throws Exception {
    final HttpRequest request = new BasicHttpRequest("GET", new HttpHost("host"), "/");
    request.addHeader("custom123", "Value");
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    final List<Header> headers = converter.convert(request);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(5, headers.size());
    final Header header1 = headers.get(0);
    Assertions.assertEquals(":method", header1.getName());
    Assertions.assertEquals("GET", header1.getValue());
    final Header header2 = headers.get(1);
    Assertions.assertEquals(":scheme", header2.getName());
    Assertions.assertEquals("http", header2.getValue());
    final Header header3 = headers.get(2);
    Assertions.assertEquals(":authority", header3.getName());
    Assertions.assertEquals("host", header3.getValue());
    final Header header4 = headers.get(3);
    Assertions.assertEquals(":path", header4.getName());
    Assertions.assertEquals("/", header4.getValue());
    final Header header5 = headers.get(4);
    Assertions.assertEquals("custom123", header5.getName());
    Assertions.assertEquals("Value", header5.getValue());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 14 with Method

use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsBasic.

@Test
public void testConvertFromFieldsBasic() 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("custom123", "value"));
    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("custom123", allHeaders[0].getName());
    Assertions.assertEquals("value", 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)

Example 15 with Method

use of org.apache.hc.core5.http.Method in project httpcomponents-core by apache.

the class TestDefaultH2RequestConverter method testConvertFromFieldsUpperCaseHeaderName.

@Test
public void testConvertFromFieldsUpperCaseHeaderName() 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("custom", "value"));
    final DefaultH2RequestConverter converter = new DefaultH2RequestConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(headers), "Header name ':Path' is invalid (header name contains uppercase characters)");
}
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)

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