Search in sources :

Example 6 with RequestValidateHost

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

the class TestStandardInterceptors method testRequestHttp11MultipleHostHeaders.

@Test
public void testRequestHttp11MultipleHostHeaders() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.addHeader(HttpHeaders.HOST, "blah");
    request.addHeader(HttpHeaders.HOST, "blah");
    final RequestValidateHost interceptor = new RequestValidateHost();
    Assertions.assertThrows(ProtocolException.class, () -> interceptor.process(request, request.getEntity(), context));
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 7 with RequestValidateHost

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

the class TestStandardInterceptors method testRequestHttp10HostHeaderAbsent.

@Test
public void testRequestHttp10HostHeaderAbsent() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.setVersion(HttpVersion.HTTP_1_0);
    final RequestValidateHost interceptor = new RequestValidateHost();
    interceptor.process(request, request.getEntity(), context);
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Test(org.junit.jupiter.api.Test)

Example 8 with RequestValidateHost

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

the class JSSEProviderIntegrationTest method testSimpleGetIdentityTransfer.

@Test
public void testSimpleGetIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    for (int i = 0; i < REQ_NUM; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        try (final ClientSessionEndpoint streamEndpoint = connectFuture.get()) {
            final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
            Assertions.assertNotNull(result);
            final HttpResponse response = result.getHead();
            final String entity = result.getBody();
            Assertions.assertNotNull(response);
            Assertions.assertEquals(200, response.getCode());
            Assertions.assertEquals("Hi there", entity);
        }
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Example 9 with RequestValidateHost

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

the class Http1IntegrationTest method testSimpleGetIdentityTransfer.

@Test
public void testSimpleGetIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    final int reqNo = 5;
    for (int i = 0; i < reqNo; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        streamEndpoint.close();
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        final String entity = result.getBody();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(200, response.getCode());
        Assertions.assertEquals("Hi there", entity);
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Example 10 with RequestValidateHost

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

the class Http1IntegrationTest method testPostIdentityTransfer.

@Test
public void testPostIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    final int reqNo = 5;
    for (int i = 0; i < reqNo; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello"), new MultiLineEntityProducer("Hello", 16 * i)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        streamEndpoint.close();
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        final String entity = result.getBody();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(200, response.getCode());
        Assertions.assertEquals("Hi there", entity);
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Aggregations

BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)6 Test (org.junit.jupiter.api.Test)6 InetSocketAddress (java.net.InetSocketAddress)4 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 Message (org.apache.hc.core5.http.Message)4 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)4 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)4 DefaultHttpProcessor (org.apache.hc.core5.http.protocol.DefaultHttpProcessor)4 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)4 RequestValidateHost (org.apache.hc.core5.http.protocol.RequestValidateHost)4 Test (org.junit.Test)4 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 URIAuthority (org.apache.hc.core5.net.URIAuthority)2 ImmediateResponseExchangeHandler (org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler)1