Search in sources :

Example 36 with HttpCoreContext

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

the class ClassicIntegrationTest method testSimpleHttpPostsChunked.

/**
 * This test case executes a series of simple POST requests with chunk
 * coded content content.
 */
@Test
public void testSimpleHttpPostsChunked() 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(20000);
        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)

Example 37 with HttpCoreContext

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

the class ClassicIntegrationTest method testHttpContent.

@Test
public void testHttpContent() throws Exception {
    final String[] patterns = { "0123456789ABCDEF", "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-" + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that" };
    // Initialize the server-side request handler
    this.server.registerHandler("*", (request, response, context) -> {
        int n = 1;
        String s = request.getPath();
        if (s.startsWith("/?n=")) {
            s = s.substring(4);
            try {
                n = Integer.parseInt(s);
                if (n <= 0) {
                    throw new HttpException("Invalid request: " + "number of repetitions cannot be negative or zero");
                }
            } catch (final NumberFormatException ex) {
                throw new HttpException("Invalid request: " + "number of repetitions is invalid");
            }
        }
        final HttpEntity entity = request.getEntity();
        if (entity != null) {
            final String line = EntityUtils.toString(entity);
            final ContentType contentType = ContentType.parse(entity.getContentType());
            final Charset charset = ContentType.getCharset(contentType, StandardCharsets.ISO_8859_1);
            response.setEntity(new RepeatingEntity(line, charset, n, n % 2 == 0));
        }
    });
    this.server.start();
    this.client.start();
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    for (final String pattern : patterns) {
        for (int n = 1000; n < 1020; n++) {
            final BasicClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST.name(), "/?n=" + n);
            post.setEntity(new StringEntity(pattern, ContentType.TEXT_PLAIN, n % 2 == 0));
            try (final ClassicHttpResponse response = this.client.execute(host, post, context)) {
                final HttpEntity entity = response.getEntity();
                Assertions.assertNotNull(entity);
                final InputStream inStream = entity.getContent();
                final ContentType contentType = ContentType.parse(entity.getContentType());
                final Charset charset = ContentType.getCharset(contentType, StandardCharsets.ISO_8859_1);
                Assertions.assertNotNull(inStream);
                final BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, charset));
                String line;
                int count = 0;
                while ((line = reader.readLine()) != null) {
                    Assertions.assertEquals(pattern, line);
                    count++;
                }
                Assertions.assertEquals(n, count);
            }
        }
    }
}
Also used : 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) ContentType(org.apache.hc.core5.http.ContentType) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BufferedReader(java.io.BufferedReader) HttpException(org.apache.hc.core5.http.HttpException) Test(org.junit.Test)

Example 38 with HttpCoreContext

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

the class ClassicIntegrationTest method testSimpleBasicHttpRequests.

/**
 * This test case executes a series of simple GET requests
 */
@Test
public void testSimpleBasicHttpRequests() 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) -> {
        String s = request.getPath();
        if (s.startsWith("/?")) {
            s = s.substring(2);
        }
        final int index = Integer.parseInt(s);
        final byte[] data = testData.get(index);
        final ByteArrayEntity entity = new ByteArrayEntity(data, null);
        response.setEntity(entity);
    });
    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 get = new BasicClassicHttpRequest(Method.GET, "/?" + r);
        try (final ClassicHttpResponse response = this.client.execute(host, get, 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) 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)

Example 39 with HttpCoreContext

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

the class ClassicIntegrationTest method testNoContentResponse.

@Test
public void testNoContentResponse() throws Exception {
    final int reqNo = 20;
    // Initialize the server-side request handler
    this.server.registerHandler("*", (request, response, context) -> response.setCode(HttpStatus.SC_NO_CONTENT));
    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 get = new BasicClassicHttpRequest(Method.GET, "/?" + r);
        try (final ClassicHttpResponse response = this.client.execute(host, get, context)) {
            Assertions.assertNull(response.getEntity());
        }
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) 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 40 with HttpCoreContext

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

the class ClassicIntegrationTest method testSimpleHttpPostsWithContentLength.

/**
 * This test case executes a series of simple POST requests with content length
 * delimited content.
 */
@Test
public void testSimpleHttpPostsWithContentLength() 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));
        }
    });
    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));
        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