use of org.apache.http.entity.BasicHttpEntity in project scribejava by scribejava.
the class OauthAsyncCompletionHandlerTest method shouldReleaseLatchOnSuccess.
@Test
public void shouldReleaseLatchOnSuccess() throws Exception {
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(new byte[0]));
response.setEntity(entity);
handler.completed(response);
assertNotNull(callback.getResponse());
assertNull(callback.getThrowable());
// verify latch is released
assertEquals("All good", handler.getResult());
}
use of org.apache.http.entity.BasicHttpEntity in project JSCover by tntim96.
the class PersistentStaticHttpServer method sendOkContent.
protected void sendOkContent(HttpServerConnection conn) throws IOException, HttpException {
// send a 200 OK with the static content
BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
BasicHttpEntity entity = new BasicHttpEntity();
byte[] message = content.getBytes(Charset.forName("UTF-8"));
entity.setContent(new ByteArrayInputStream(message));
entity.setContentLength(message.length);
response.setEntity(entity);
// force Content-Length header so the client doesn't expect us to close the connection to end the response
response.addHeader("Content-Length", String.valueOf(message.length));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
logger.log(FINE, "Sent 200 OK");
}
use of org.apache.http.entity.BasicHttpEntity in project spring-cloud-netflix by spring-cloud.
the class RibbonApacheHttpResponseTests method testNotNullEntity.
@Test
public void testNotNullEntity() throws Exception {
StatusLine statusLine = mock(StatusLine.class);
given(statusLine.getStatusCode()).willReturn(204);
HttpResponse response = mock(HttpResponse.class);
given(response.getStatusLine()).willReturn(statusLine);
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(new byte[0]));
given(response.getEntity()).willReturn(entity);
RibbonApacheHttpResponse httpResponse = new RibbonApacheHttpResponse(response, URI.create("http://example.com"));
assertThat(httpResponse.isSuccess(), is(true));
assertThat(httpResponse.hasPayload(), is(true));
assertThat(httpResponse.getPayload(), is(notNullValue()));
assertThat(httpResponse.getInputStream(), is(notNullValue()));
}
use of org.apache.http.entity.BasicHttpEntity in project spring-cloud-netflix by spring-cloud.
the class HttpClientStatusCodeExceptionTest method getResponse.
@Test
public void getResponse() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
doReturn(new Locale("en")).when(response).getLocale();
Header foo = new BasicHeader("foo", "bar");
Header[] headers = new Header[] { foo };
doReturn(headers).when(response).getAllHeaders();
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "Success");
doReturn(statusLine).when(response).getStatusLine();
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream("foo".getBytes()));
entity.setContentLength(3);
doReturn(entity).when(response).getEntity();
HttpEntity copiedEntity = HttpClientUtils.createEntity(response);
HttpClientStatusCodeException ex = new HttpClientStatusCodeException("service", response, copiedEntity, new URI("http://service.com"));
assertEquals("en", ex.getResponse().getLocale().toString());
assertArrayEquals(headers, ex.getResponse().getAllHeaders());
assertEquals("Success", ex.getResponse().getStatusLine().getReasonPhrase());
assertEquals(200, ex.getResponse().getStatusLine().getStatusCode());
assertEquals("http", ex.getResponse().getStatusLine().getProtocolVersion().getProtocol());
assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMajor());
assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMinor());
assertEquals("foo", EntityUtils.toString(ex.getResponse().getEntity()));
verify(response, times(1)).close();
}
use of org.apache.http.entity.BasicHttpEntity in project iosched by google.
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