Search in sources :

Example 1 with HttpRequestInterceptor

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

the class TestChainBuilder method testBuildChain.

@Test
public void testBuildChain() throws Exception {
    final ChainBuilder<HttpRequestInterceptor> cb = new ChainBuilder<>();
    final HttpRequestInterceptor i1 = RequestContent.INSTANCE;
    final HttpRequestInterceptor i2 = RequestTargetHost.INSTANCE;
    final HttpRequestInterceptor i3 = RequestConnControl.INSTANCE;
    final HttpRequestInterceptor i4 = RequestUserAgent.INSTANCE;
    final HttpRequestInterceptor i5 = new RequestExpectContinue();
    cb.addFirst(i1);
    cb.addAllFirst(i2, i3);
    cb.addFirst(null);
    cb.addAllFirst((List<HttpRequestInterceptor>) null);
    cb.addLast(i4);
    cb.addLast(null);
    cb.addAllLast(i5);
    cb.addAllLast((List<HttpRequestInterceptor>) null);
    cb.addFirst(i1);
    cb.addAllLast(i3, i4, i5);
    final LinkedList<HttpRequestInterceptor> list = cb.build();
    Assertions.assertNotNull(list);
    Assertions.assertEquals(5, list.size());
    Assertions.assertSame(i1, list.get(0));
    Assertions.assertSame(i2, list.get(1));
    Assertions.assertSame(i3, list.get(2));
    Assertions.assertSame(i4, list.get(3));
    Assertions.assertSame(i5, list.get(4));
}
Also used : HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 2 with HttpRequestInterceptor

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

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

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

the class TestStandardInterceptors method testRequestDateInvalidInput.

@Test
public void testRequestDateInvalidInput() throws Exception {
    final HttpRequestInterceptor interceptor = RequestDate.INSTANCE;
    Assertions.assertThrows(NullPointerException.class, () -> interceptor.process(null, null, null));
}
Also used : HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 5 with HttpRequestInterceptor

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

HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)34 Test (org.junit.jupiter.api.Test)31 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)26 Header (org.apache.hc.core5.http.Header)21 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)19 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)6 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)4 URIAuthority (org.apache.hc.core5.net.URIAuthority)4 IOException (java.io.IOException)3 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 HttpException (org.apache.hc.core5.http.HttpException)3 HttpRequest (org.apache.hc.core5.http.HttpRequest)3 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)3 ByteBuffer (java.nio.ByteBuffer)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 StatusLine (org.apache.hc.core5.http.message.StatusLine)2 URI (java.net.URI)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1