Search in sources :

Example 1 with BasicHttpContext

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

the class ClassicTLSIntegrationTest method testTLSTrustFailure.

@Test
public void testTLSTrustFailure() throws Exception {
    server = ServerBootstrap.bootstrap().setSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT).build()).setSslContext(SSLTestContexts.createServerSSLContext()).setExceptionListener(LoggingExceptionListener.INSTANCE).setStreamListener(LoggingHttp1StreamListener.INSTANCE).register("*", new EchoHandler()).create();
    server.start();
    requester = RequesterBootstrap.bootstrap().setSslContext(SSLContexts.createDefault()).setSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT).build()).setStreamListener(LoggingHttp1StreamListener.INSTANCE).setConnPoolListener(LoggingConnPoolListener.INSTANCE).create();
    final HttpContext context = new BasicHttpContext();
    final HttpHost target = new HttpHost("https", "localhost", server.getLocalPort());
    final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
    Assertions.assertThrows(IOException.class, () -> {
        try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
            EntityUtils.consume(response1.getEntity());
        }
    });
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) 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) BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Test(org.junit.jupiter.api.Test)

Example 2 with BasicHttpContext

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

the class ClassicTLSIntegrationTest method testSSLDisabledByDefault.

@Test
public void testSSLDisabledByDefault() throws Exception {
    server = ServerBootstrap.bootstrap().setSslContext(SSLTestContexts.createServerSSLContext()).setSslSetupHandler(sslParameters -> sslParameters.setProtocols(new String[] { "SSLv3" })).create();
    server.start();
    requester = RequesterBootstrap.bootstrap().setSslContext(SSLTestContexts.createClientSSLContext()).setSocketConfig(SocketConfig.custom().setSoTimeout(TIMEOUT).build()).setStreamListener(LoggingHttp1StreamListener.INSTANCE).setConnPoolListener(LoggingConnPoolListener.INSTANCE).create();
    final HttpContext context = new BasicHttpContext();
    final HttpHost target = new HttpHost("https", "localhost", server.getLocalPort());
    final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
    request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
    Assertions.assertThrows(IOException.class, () -> {
        try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
            EntityUtils.consume(response1.getEntity());
        }
    });
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) 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) BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) HttpHost(org.apache.hc.core5.http.HttpHost) BasicHttpContext(org.apache.hc.core5.http.protocol.BasicHttpContext) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) Test(org.junit.jupiter.api.Test)

Example 3 with BasicHttpContext

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

the class TestStandardInterceptors method testRequestContentNullEntity.

@Test
public void testRequestContentNullEntity() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
    final HttpRequestInterceptor interceptor = RequestContent.INSTANCE;
    interceptor.process(request, request.getEntity(), context);
    final Header header = request.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNull(header);
    Assertions.assertNull(request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING));
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 4 with BasicHttpContext

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

the class TestStandardInterceptors method testRequestConnControlCustom.

@Test
public void testRequestConnControlCustom() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    final Header myheader = new BasicHeader(HttpHeaders.CONNECTION, "close");
    request.addHeader(myheader);
    final HttpRequestInterceptor interceptor = RequestConnControl.INSTANCE;
    interceptor.process(request, request.getEntity(), context);
    final Header header = request.getFirstHeader(HttpHeaders.CONNECTION);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("close", header.getValue());
    Assertions.assertSame(header, myheader);
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 5 with BasicHttpContext

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

the class TestStandardInterceptors method testRequestContentProtocolException.

@Test
public void testRequestContentProtocolException() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/");
    request1.addHeader(new BasicHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"));
    final BasicClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/");
    request2.addHeader(new BasicHeader(HttpHeaders.CONTENT_LENGTH, "12"));
    final HttpRequestInterceptor interceptor = RequestContent.INSTANCE;
    Assertions.assertThrows(ProtocolException.class, () -> interceptor.process(request1, request1.getEntity(), context));
    Assertions.assertThrows(ProtocolException.class, () -> interceptor.process(request2, request2.getEntity(), context));
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)71 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)52 Header (org.apache.hc.core5.http.Header)49 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)49 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)33 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)30 HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)25 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)16 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)13 BasicHttpContext (org.apache.hc.core5.http.protocol.BasicHttpContext)8 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)5 URIAuthority (org.apache.hc.core5.net.URIAuthority)5 IOException (java.io.IOException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SSLSession (javax.net.ssl.SSLSession)2 ContentType (org.apache.hc.core5.http.ContentType)2 HttpStatus (org.apache.hc.core5.http.HttpStatus)2 Method (org.apache.hc.core5.http.Method)2