Search in sources :

Example 31 with HttpCoreContext

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

the class ClassicIntegrationTest method testHttpPostsWithExpectationVerification.

/**
 * This test case executes a series of simple POST requests that do not
 * meet the target server expectations.
 */
@Test
public void testHttpPostsWithExpectationVerification() throws Exception {
    final int reqNo = 20;
    // Initialize the server-side request handler
    this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("No content")));
    this.server.start(null, null, handler -> new BasicHttpServerExpectationDecorator(handler) {

        @Override
        protected ClassicHttpResponse verify(final ClassicHttpRequest request, final HttpContext context) {
            final Header someheader = request.getFirstHeader("Secret");
            if (someheader != null) {
                final int secretNumber;
                try {
                    secretNumber = Integer.parseInt(someheader.getValue());
                } catch (final NumberFormatException ex) {
                    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_BAD_REQUEST);
                    response.setEntity(new StringEntity(ex.toString()));
                    return response;
                }
                if (secretNumber >= 2) {
                    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
                    response.setEntity(new StringEntity("Wrong secret number", ContentType.TEXT_PLAIN));
                    return response;
                }
            }
            return null;
        }
    });
    this.client.start();
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    for (int r = 0; r < reqNo; r++) {
        final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
        post.addHeader("Secret", Integer.toString(r));
        final byte[] b = new byte[2048];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) ('a' + r);
        }
        post.setEntity(new ByteArrayEntity(b, ContentType.TEXT_PLAIN));
        try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
            final HttpEntity responseEntity = response.getEntity();
            Assertions.assertNotNull(responseEntity);
            EntityUtils.consume(responseEntity);
            if (r >= 2) {
                Assertions.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
            } else {
                Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
            }
        }
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicHttpServerExpectationDecorator(org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator) HttpEntity(org.apache.hc.core5.http.HttpEntity) AbstractHttpEntity(org.apache.hc.core5.http.io.entity.AbstractHttpEntity) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.Test)

Example 32 with HttpCoreContext

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

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

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

Example 35 with HttpCoreContext

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

the class ClassicIntegrationTest method testHttpPostsWithExpectContinue.

/**
 * This test case executes a series of simple POST requests using
 * the 'expect: continue' handshake.
 */
@Test
public void testHttpPostsWithExpectContinue() throws Exception {
    final int reqNo = 20;
    final Random rnd = new Random();
    // Prepare some random data
    final List<byte[]> testData = new ArrayList<>(reqNo);
    for (int i = 0; i < reqNo; i++) {
        final int size = rnd.nextInt(5000);
        final byte[] data = new byte[size];
        rnd.nextBytes(data);
        testData.add(data);
    }
    // Initialize the server-side request handler
    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, true));
        }
    });
    this.server.start();
    this.client.start();
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    for (int r = 0; r < reqNo; r++) {
        final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
        final byte[] data = testData.get(r);
        post.setEntity(new ByteArrayEntity(data, null, true));
        try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
            final byte[] received = EntityUtils.toByteArray(response.getEntity());
            final byte[] expected = testData.get(r);
            Assertions.assertEquals(expected.length, received.length);
            for (int i = 0; i < expected.length; i++) {
                Assertions.assertEquals(expected[i], received[i]);
            }
        }
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Random(java.util.Random) 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) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)56 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)46 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)39 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)33 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)29 HttpHost (org.apache.hc.core5.http.HttpHost)26 Test (org.junit.jupiter.api.Test)25 Test (org.junit.Test)21 HttpEntity (org.apache.hc.core5.http.HttpEntity)16 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)12 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)12 AbstractHttpEntity (org.apache.hc.core5.http.io.entity.AbstractHttpEntity)9 Random (java.util.Random)8 Header (org.apache.hc.core5.http.Header)8 ArrayList (java.util.ArrayList)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)6 ProtocolException (org.apache.hc.core5.http.ProtocolException)6 ContentType (org.apache.hc.core5.http.ContentType)5