use of org.apache.http.entity.BasicHttpEntity in project blueocean-plugin by jenkinsci.
the class HttpResponseTest method testWithoutErrorDetails.
@Test
public void testWithoutErrorDetails() {
org.apache.http.HttpResponse rawHttpResponse = Mockito.mock(org.apache.http.HttpResponse.class);
when(rawHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new HttpVersion(1, 1), 400, ""));
when(rawHttpResponse.getEntity()).thenReturn(new BasicHttpEntity() {
@Override
public InputStream getContent() throws IllegalStateException {
return new ByteArrayInputStream("{\"errors\": [{\"message\": \"msg\", \"exceptionName\": \"name\"}]}".getBytes());
}
});
HttpResponse httpResponse = new HttpResponse(rawHttpResponse);
assertThrows(ServiceException.class, () -> {
try {
httpResponse.getContent();
} catch (ServiceException e) {
assertTrue(e.toJson().contains("no error details"));
throw e;
}
});
}
use of org.apache.http.entity.BasicHttpEntity in project blueocean-plugin by jenkinsci.
the class HttpResponseTest method testWithErrorDetails.
@Test
public void testWithErrorDetails() {
org.apache.http.HttpResponse rawHttpResponse = Mockito.mock(org.apache.http.HttpResponse.class);
when(rawHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new HttpVersion(1, 1), 400, ""));
when(rawHttpResponse.getEntity()).thenReturn(new BasicHttpEntity() {
@Override
public InputStream getContent() throws IllegalStateException {
return new ByteArrayInputStream("{\"errors\": [{\"message\": \"msg\", \"exceptionName\": \"name\", \"details\":[\"fake-detail\"]}]}".getBytes());
}
});
HttpResponse httpResponse = new HttpResponse(rawHttpResponse);
assertThrows(ServiceException.class, () -> {
try {
httpResponse.getContent();
} catch (ServiceException e) {
assertTrue(e.toJson().contains("fake-detail"));
throw e;
}
});
}
use of org.apache.http.entity.BasicHttpEntity in project spring-boot by spring-projects.
the class ElasticsearchRestHealthIndicatorTests method elasticsearchWithYellowStatusIsUp.
@Test
void elasticsearchWithYellowStatusIsUp() throws IOException {
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "yellow").getBytes()));
Response response = mock(Response.class);
StatusLine statusLine = mock(StatusLine.class);
given(statusLine.getStatusCode()).willReturn(200);
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
}
use of org.apache.http.entity.BasicHttpEntity in project spring-boot by spring-projects.
the class ElasticsearchRestHealthIndicatorTests method elasticsearchIsUp.
@Test
void elasticsearchIsUp() throws IOException {
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "green").getBytes()));
Response response = mock(Response.class);
StatusLine statusLine = mock(StatusLine.class);
given(statusLine.getStatusCode()).willReturn(200);
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertHealthDetailsWithStatus(health.getDetails(), "green");
}
use of org.apache.http.entity.BasicHttpEntity in project android-volley by mcxiaoke.
the class HurlStack method entityFromConnection.
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
Aggregations