Search in sources :

Example 1 with BasicClassicHttpRequest

use of org.apache.hc.core5.http.message.BasicClassicHttpRequest in project httpcomponents-core by apache.

the class ClassicAuthenticationTest method testGetRequestAuthentication.

@Test
public void testGetRequestAuthentication() throws Exception {
    server.start();
    final HttpHost target = new HttpHost("localhost", server.getLocalPort());
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.GET, "/stuff");
    try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
        assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
        final String body1 = EntityUtils.toString(response1.getEntity());
        assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    }
    final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.GET, "/stuff");
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
        assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body1 = EntityUtils.toString(response2.getEntity());
        assertThat(body1, CoreMatchers.equalTo(""));
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) 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 2 with BasicClassicHttpRequest

use of org.apache.hc.core5.http.message.BasicClassicHttpRequest in project httpcomponents-core by apache.

the class ClassicAuthenticationTest method testPostRequestAuthenticationNoExpectContinue.

@Test
public void testPostRequestAuthenticationNoExpectContinue() throws Exception {
    server.start();
    final HttpHost target = new HttpHost("localhost", server.getLocalPort());
    final HttpCoreContext context = HttpCoreContext.create();
    final Random rnd = new Random();
    final byte[] stuff = new byte[10240];
    for (int i = 0; i < stuff.length; i++) {
        stuff[i] = (byte) ('a' + rnd.nextInt(10));
    }
    final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request1.setVersion(HttpVersion.HTTP_1_0);
    request1.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
        assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_UNAUTHORIZED));
        final String body1 = EntityUtils.toString(response1.getEntity());
        assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
    }
    final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
    request2.setVersion(HttpVersion.HTTP_1_0);
    request2.setEntity(new ByteArrayEntity(stuff, ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
        assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
        final String body1 = EntityUtils.toString(response2.getEntity());
        assertThat(body1, CoreMatchers.equalTo(new String(stuff, StandardCharsets.US_ASCII)));
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Random(java.util.Random) 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) Test(org.junit.Test)

Example 3 with BasicClassicHttpRequest

use of org.apache.hc.core5.http.message.BasicClassicHttpRequest in project httpcomponents-core by apache.

the class ClassicIntegrationTest method testHttpPostNoEntity.

@Test
public void testHttpPostNoEntity() 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();
    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 : 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) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Example 4 with BasicClassicHttpRequest

use of org.apache.hc.core5.http.message.BasicClassicHttpRequest in project httpcomponents-core by apache.

the class ClassicIntegrationTest method testHeaderTooLarge.

@Test
public void testHeaderTooLarge() throws Exception {
    this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
    this.server.start(Http1Config.custom().setMaxLineLength(100).build(), null, null);
    this.client.start();
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
    get1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
    try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
        Assertions.assertEquals(431, response1.getCode());
        EntityUtils.consume(response1.getEntity());
    }
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) 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) Test(org.junit.Test)

Example 5 with BasicClassicHttpRequest

use of org.apache.hc.core5.http.message.BasicClassicHttpRequest in project httpcomponents-core by apache.

the class ClassicIntegrationTest method testHeaderTooLargePost.

@Test
public void testHeaderTooLargePost() throws Exception {
    this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
    this.server.start(Http1Config.custom().setMaxLineLength(100).build(), null, null);
    this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, RequestTargetHost.INSTANCE, RequestConnControl.INSTANCE));
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final ClassicHttpRequest post1 = new BasicClassicHttpRequest(Method.POST, "/");
    post1.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);
    }
    post1.setEntity(new ByteArrayEntity(b, ContentType.TEXT_PLAIN));
    try (final ClassicHttpResponse response1 = this.client.execute(host, post1, context)) {
        Assertions.assertEquals(431, response1.getCode());
        EntityUtils.consume(response1.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) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) 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) Test(org.junit.Test)

Aggregations

BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)107 Test (org.junit.jupiter.api.Test)84 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)49 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)45 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)43 Header (org.apache.hc.core5.http.Header)38 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)37 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)34 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)33 HttpHost (org.apache.hc.core5.http.HttpHost)28 HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)26 Test (org.junit.Test)20 HttpEntity (org.apache.hc.core5.http.HttpEntity)16 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)13 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)12 AbstractHttpEntity (org.apache.hc.core5.http.io.entity.AbstractHttpEntity)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Random (java.util.Random)8 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)7