Search in sources :

Example 11 with DefaultHttpProcessor

use of org.apache.hc.core5.http.protocol.DefaultHttpProcessor 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

DefaultHttpProcessor (org.apache.hc.core5.http.protocol.DefaultHttpProcessor)11 Test (org.junit.Test)11 InetSocketAddress (java.net.InetSocketAddress)7 HttpResponse (org.apache.hc.core5.http.HttpResponse)7 Message (org.apache.hc.core5.http.Message)7 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)7 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)7 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)5 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 HttpHost (org.apache.hc.core5.http.HttpHost)4 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)4 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)4 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)4 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)4 RequestValidateHost (org.apache.hc.core5.http.protocol.RequestValidateHost)4 HttpRequest (org.apache.hc.core5.http.HttpRequest)3 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2