Search in sources :

Example 6 with DefaultHttpProcessor

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

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

the class Http1IntegrationTest method testHeaderTooLargePost.

@Test
public void testHeaderTooLargePost() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start(null, Http1Config.custom().setMaxLineLength(100).build());
    client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE), null);
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
    request1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
    final byte[] b = new byte[2048];
    for (int i = 0; i < b.length; i++) {
        b[i] = (byte) ('a' + i % 10);
    }
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(b, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(431, response1.getCode());
    Assertions.assertEquals("Maximum line length limit exceeded", result1.getBody());
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) 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) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) Test(org.junit.Test)

Example 8 with DefaultHttpProcessor

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

the class ClassicIntegrationTest method testAbsentHostHeader.

@Test
public void testAbsentHostHeader() throws Exception {
    // Initialize the server-side request handler
    this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
    this.server.start();
    this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, new RequestConnControl()));
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
    get1.setVersion(HttpVersion.HTTP_1_0);
    try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
        Assertions.assertEquals(200, response1.getCode());
        EntityUtils.consume(response1.getEntity());
    }
    final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest(Method.GET, "/");
    try (final ClassicHttpResponse response2 = this.client.execute(host, get2, context)) {
        Assertions.assertEquals(400, response2.getCode());
        EntityUtils.consume(response2.getEntity());
    }
}
Also used : DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Example 9 with DefaultHttpProcessor

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

the class ClassicIntegrationTest method testHttpPostNoContentLength.

@Test
public void testHttpPostNoContentLength() throws Exception {
    this.server.registerHandler("*", (request, response, context) -> {
        final HttpEntity entity = request.getEntity();
        if (entity != null) {
            final byte[] data = EntityUtils.toByteArray(entity);
            response.setEntity(new ByteArrayEntity(data, null));
        }
    });
    this.server.start();
    this.client.start(new DefaultHttpProcessor(RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE, RequestUserAgent.INSTANCE, new RequestExpectContinue()));
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
    post.setEntity(null);
    try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
        Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
        final byte[] received = EntityUtils.toByteArray(response.getEntity());
        Assertions.assertEquals(0, received.length);
    }
}
Also used : DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) AbstractHttpEntity(org.apache.hc.core5.http.io.entity.AbstractHttpEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) RequestExpectContinue(org.apache.hc.core5.http.protocol.RequestExpectContinue) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Example 10 with DefaultHttpProcessor

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

the class ClassicIntegrationTest method testHttpPostIdentity.

@Test
public void testHttpPostIdentity() throws Exception {
    this.server.registerHandler("*", (request, response, context) -> {
        final HttpEntity entity = request.getEntity();
        if (entity != null) {
            final byte[] data = EntityUtils.toByteArray(entity);
            response.setEntity(new ByteArrayEntity(data, null));
        }
    });
    this.server.start();
    this.client.start(new DefaultHttpProcessor((request, entity, context) -> request.addHeader(HttpHeaders.TRANSFER_ENCODING, "identity"), RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE, RequestUserAgent.INSTANCE, new RequestExpectContinue()));
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
    post.setEntity(null);
    try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
        Assertions.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
    }
}
Also used : DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Arrays(java.util.Arrays) RequestContent(org.apache.hc.core5.http.protocol.RequestContent) Random(java.util.Random) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Http1Config(org.apache.hc.core5.http.config.Http1Config) EnableRuleMigrationSupport(org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport) EntityUtils(org.apache.hc.core5.http.io.entity.EntityUtils) RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) Parameterized(org.junit.runners.Parameterized) Collection(java.util.Collection) SocketConfig(org.apache.hc.core5.http.io.SocketConfig) StandardCharsets(java.nio.charset.StandardCharsets) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) List(java.util.List) SSLTestContexts(org.apache.hc.core5.testing.SSLTestContexts) ExternalResource(org.junit.rules.ExternalResource) HttpEntity(org.apache.hc.core5.http.HttpEntity) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) RequestUserAgent(org.apache.hc.core5.http.protocol.RequestUserAgent) AbstractHttpEntity(org.apache.hc.core5.http.io.entity.AbstractHttpEntity) BasicHttpServerExpectationDecorator(org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) HttpVersion(org.apache.hc.core5.http.HttpVersion) RequestExpectContinue(org.apache.hc.core5.http.protocol.RequestExpectContinue) RequestTargetHost(org.apache.hc.core5.http.protocol.RequestTargetHost) CloseMode(org.apache.hc.core5.io.CloseMode) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) OutputStream(java.io.OutputStream) HttpException(org.apache.hc.core5.http.HttpException) Header(org.apache.hc.core5.http.Header) IOException(java.io.IOException) Test(org.junit.Test) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) InputStreamReader(java.io.InputStreamReader) URIScheme(org.apache.hc.core5.http.URIScheme) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) TimeUnit(java.util.concurrent.TimeUnit) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpHeaders(org.apache.hc.core5.http.HttpHeaders) HttpHost(org.apache.hc.core5.http.HttpHost) Rule(org.junit.Rule) ContentType(org.apache.hc.core5.http.ContentType) Method(org.apache.hc.core5.http.Method) Assertions(org.junit.jupiter.api.Assertions) BufferedReader(java.io.BufferedReader) HttpStatus(org.apache.hc.core5.http.HttpStatus) InputStream(java.io.InputStream) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) AbstractHttpEntity(org.apache.hc.core5.http.io.entity.AbstractHttpEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) RequestExpectContinue(org.apache.hc.core5.http.protocol.RequestExpectContinue) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) 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