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);
}
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);
}
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);
}
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));
}
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());
}
Aggregations