Search in sources :

Example 16 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project k-9 by k9mail.

the class WebDavFolderTest method folder_can_fetch_sensible_body_data_and_notifies_listener.

@Test
public void folder_can_fetch_sensible_body_data_and_notifies_listener() throws MessagingException, IOException, URISyntaxException {
    setupStoreForMessageFetching();
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer(new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
            HttpResponse httpResponse = mock(HttpResponse.class);
            StatusLine statusLine = mock(StatusLine.class);
            when(httpResponse.getStatusLine()).thenReturn(statusLine);
            when(statusLine.getStatusCode()).thenReturn(200);
            BasicHttpEntity httpEntity = new BasicHttpEntity();
            String body = "";
            httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
            when(httpResponse.getEntity()).thenReturn(httpEntity);
            return httpResponse;
        }
    });
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.BODY_SANE);
    folder.fetch(messages, profile, listener);
    verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25));
    verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FetchProfile(com.fsck.k9.mail.FetchProfile) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Matchers.anyString(org.mockito.Matchers.anyString) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 17 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project RoboZombie by sahan.

the class RequestParamEndpointTest method testBufferedHttpEntity.

/**
	 * <p>Test for a {@link Request} with a <b>buffered</b> entity.</p>
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testBufferedHttpEntity() throws ParseException, IOException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/bufferedhttpentity";
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream("LICENSE.txt");
    InputStream parallelInputStream = classLoader.getResourceAsStream("LICENSE.txt");
    BasicHttpEntity bhe = new BasicHttpEntity();
    bhe.setContent(parallelInputStream);
    stubFor(put(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200)));
    requestEndpoint.bufferedHttpEntity(inputStream);
    verify(putRequestedFor(urlEqualTo(subpath)).withRequestBody(equalTo(EntityUtils.toString(new BufferedHttpEntity(bhe)))));
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.Test)

Example 18 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project XobotOS by xamarin.

the class AndroidHttpClientConnection method receiveResponseEntity.

/**
     * Return the next response entity.
     * @param headers contains values for parsing entity
     * @see HttpClientConnection#receiveResponseEntity(HttpResponse response)
     */
public HttpEntity receiveResponseEntity(final Headers headers) {
    assertOpen();
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = determineLength(headers);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, len));
    }
    String contentTypeHeader = headers.getContentType();
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    String contentEncodingHeader = headers.getContentEncoding();
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }
    return entity;
}
Also used : ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 19 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project XobotOS by xamarin.

the class EntityDeserializer method doDeserialize.

protected BasicHttpEntity doDeserialize(final SessionInputBuffer inbuffer, final HttpMessage message) throws HttpException, IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = this.lenStrategy.determineLength(message);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, len));
    }
    Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }
    return entity;
}
Also used : Header(org.apache.http.Header) ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 20 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project saga-android by AnandChowdhary.

the class HurlStack method entityFromConnection.

/**
     * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.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)27 InputStream (java.io.InputStream)12 IOException (java.io.IOException)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6 IdentityInputStream (org.apache.http.impl.io.IdentityInputStream)6 Test (org.junit.Test)4 FetchProfile (com.fsck.k9.mail.FetchProfile)3 Header (org.apache.http.Header)3 HttpResponse (org.apache.http.HttpResponse)3 StatusLine (org.apache.http.StatusLine)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 HttpContext (org.apache.http.protocol.HttpContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 HttpException (org.apache.http.HttpException)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2