Search in sources :

Example 41 with BasicClassicHttpResponse

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

the class TestHttpRequestExecutor method testExecutionHead.

@Test
public void testExecutionHead() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.HEAD, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(200, "OK"));
    final ClassicHttpResponse response = executor.execute(request, conn, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn).receiveResponseHeader();
    Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) 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) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 42 with BasicClassicHttpResponse

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

the class TestHttpRequestExecutor method testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses.

@Test
public void testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
    request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
    final HttpEntity entity = Mockito.mock(HttpEntity.class);
    request.setEntity(entity);
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
    Mockito.when(conn.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(Boolean.TRUE);
    final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
    final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).sendRequestEntity(request);
    Mockito.verify(conn, Mockito.times(2)).flush();
    Mockito.verify(conn, Mockito.times(2)).isDataAvailable(Timeout.ofMilliseconds(3000));
    Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
    Mockito.verify(conn).receiveResponseEntity(response);
    final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
    Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
    final List<HttpResponse> infos = responseCaptor.getAllValues();
    Assertions.assertNotNull(infos);
    Assertions.assertEquals(2, infos.size());
    final HttpResponse info1 = infos.get(0);
    Assertions.assertNotNull(info1);
    Assertions.assertEquals(110, info1.getCode());
    final HttpResponse info2 = infos.get(1);
    Assertions.assertNotNull(info2);
    Assertions.assertEquals(111, info2.getCode());
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) Timeout(org.apache.hc.core5.util.Timeout) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpResponseInformationCallback(org.apache.hc.core5.http.io.HttpResponseInformationCallback) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 43 with BasicClassicHttpResponse

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

the class TestDefaultBHttpServerConnection method testWriteResponseNoEntity.

@Test
public void testWriteResponseNoEntity() throws Exception {
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Mockito.when(socket.getOutputStream()).thenReturn(outStream);
    conn.bind(socket);
    Assertions.assertEquals(0, conn.getEndpointDetails().getResponseCount());
    final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
    response.addHeader("Server", "test");
    conn.sendResponseHeader(response);
    conn.sendResponseEntity(response);
    conn.flush();
    Assertions.assertEquals(1, conn.getEndpointDetails().getResponseCount());
    final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("HTTP/1.1 200 OK\r\nServer: test\r\n\r\n", s);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 44 with BasicClassicHttpResponse

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

the class TestBHttpConnectionBase method testCreateEntityInputUndefined.

@Test
public void testCreateEntityInputUndefined() throws Exception {
    final InputStream inStream = Mockito.mock(InputStream.class);
    final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK");
    final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.UNDEFINED);
    Assertions.assertNotNull(entity);
    Assertions.assertFalse(entity.isChunked());
    Assertions.assertEquals(-1, entity.getContentLength());
    final InputStream content = entity.getContent();
    Assertions.assertNotNull(content);
    Assertions.assertTrue((content instanceof IdentityInputStream));
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 45 with BasicClassicHttpResponse

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

the class TestStandardInterceptors method testResponseContentEntityContentTypeAndEncoding.

@Test
public void testResponseContentEntityContentTypeAndEncoding() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, ContentType.parseLenient("whatever"), "whatever"));
    final ResponseContent interceptor = new ResponseContent();
    interceptor.process(response, response.getEntity(), context);
    final Header h1 = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
    Assertions.assertNotNull(h1);
    Assertions.assertEquals("whatever", h1.getValue());
    final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
    Assertions.assertNotNull(h2);
    Assertions.assertEquals("whatever", h2.getValue());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)69 Test (org.junit.jupiter.api.Test)57 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)56 Header (org.apache.hc.core5.http.Header)34 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)19 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)14 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpEntity (org.apache.hc.core5.http.HttpEntity)12 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)11 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)10 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)10 BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)9 TimeValue (org.apache.hc.core5.util.TimeValue)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 Timeout (org.apache.hc.core5.util.Timeout)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3