use of org.apache.http.client.entity.EntityBuilder in project bender by Nextdoor.
the class HttpTransportTest method getMockClientWithResponse.
private HttpClient getMockClientWithResponse(byte[] respPayload, ContentType contentType, int status, boolean compressed) throws IOException {
HttpClient mockClient = mock(HttpClient.class);
HttpResponse mockResponse = mock(HttpResponse.class);
StatusLine mockStatusLine = mock(StatusLine.class);
doReturn("expected failure").when(mockStatusLine).getReasonPhrase();
doReturn(status).when(mockStatusLine).getStatusCode();
doReturn(mockStatusLine).when(mockResponse).getStatusLine();
EntityBuilder eb = EntityBuilder.create().setBinary(respPayload).setContentType(contentType);
HttpEntity he;
if (compressed) {
eb.setContentEncoding("gzip");
he = new GzipDecompressingEntity(eb.build());
} else {
he = eb.build();
}
doReturn(he).when(mockResponse).getEntity();
doReturn(mockResponse).when(mockClient).execute(any(HttpPost.class));
return mockClient;
}
use of org.apache.http.client.entity.EntityBuilder in project openstack4j by ContainX.
the class HttpCommand method execute.
/**
* Executes the command and returns the Response
*
* @return the response
* @throws Exception
*/
public CloseableHttpResponse execute() throws Exception {
EntityBuilder builder = null;
if (request.getEntity() != null) {
if (InputStream.class.isAssignableFrom(request.getEntity().getClass())) {
InputStreamEntity ise = new InputStreamEntity((InputStream) request.getEntity(), ContentType.create(request.getContentType()));
((HttpEntityEnclosingRequestBase) clientReq).setEntity(ise);
} else {
builder = EntityBuilder.create().setContentType(ContentType.create(request.getContentType(), "UTF-8")).setText(ObjectMapperSingleton.getContext(request.getEntity().getClass()).writer().writeValueAsString(request.getEntity()));
}
} else if (request.hasJson()) {
builder = EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).setText(request.getJson());
}
if (builder != null && clientReq instanceof HttpEntityEnclosingRequestBase)
((HttpEntityEnclosingRequestBase) clientReq).setEntity(builder.build());
return client.execute(clientReq);
}
Aggregations