Search in sources :

Example 51 with BasicHttpEntity

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());
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 52 with BasicHttpEntity

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");
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 53 with BasicHttpEntity

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()));
}
Also used : StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.Test)

Example 54 with BasicHttpEntity

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();
}
Also used : Locale(java.util.Locale) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) URI(java.net.URI) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 55 with BasicHttpEntity

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;
}
Also used : InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)139 ByteArrayInputStream (java.io.ByteArrayInputStream)104 Test (org.junit.Test)89 InputStream (java.io.InputStream)60 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)15 StatusLine (org.apache.http.StatusLine)12 BasicStatusLine (org.apache.http.message.BasicStatusLine)11 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)9 HttpException (org.apache.http.HttpException)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpContext (org.apache.http.protocol.HttpContext)8 URISyntaxException (java.net.URISyntaxException)7 Map (java.util.Map)7 URI (java.net.URI)6 Header (org.apache.http.Header)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6