Search in sources :

Example 56 with BasicHttpEntity

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

the class WebDavFolderTest method folder_ignores_exception_thrown_when_closing.

@Test
public void folder_ignores_exception_thrown_when_closing() throws MessagingException, IOException {
    setupStoreForMessageFetching();
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(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();
            InputStream mockInputStream = mock(InputStream.class);
            when(mockInputStream.read(any(byte[].class), anyInt(), anyInt())).thenReturn(1).thenReturn(-1);
            doThrow(new IOException("Test")).when(mockInputStream).close();
            httpEntity.setContent(mockInputStream);
            when(httpResponse.getEntity()).thenReturn(httpEntity);
            return httpResponse;
        }
    });
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.BODY_SANE);
    folder.fetch(messages, profile, listener, MAX_DOWNLOAD_SIZE);
    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) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StatusLine(org.apache.http.StatusLine) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 57 with BasicHttpEntity

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

the class WebDavFolderTest method folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies.

@Test
public void folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies() throws MessagingException, IOException, URISyntaxException {
    setupStoreForMessageFetching();
    when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())).thenReturn(mockDataSet);
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(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.FLAGS);
    profile.add(FetchProfile.Item.BODY);
    folder.fetch(messages, profile, listener, MAX_DOWNLOAD_SIZE);
    verify(listener, times(25)).messageStarted(any(String.class), anyInt(), anyInt());
    verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), anyInt());
}
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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 58 with BasicHttpEntity

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

the class WebDavStoreTest method createOkResponseWithForm.

private BasicHttpResponse createOkResponseWithForm() {
    BasicHttpResponse okayResponseWithForm = createOkResponse();
    BasicHttpEntity okayResponseWithFormEntity = new BasicHttpEntity();
    String form = "<form action=\"owaauth.dll\"></form>";
    okayResponseWithFormEntity.setContent(new ByteArrayInputStream(form.getBytes()));
    okayResponseWithForm.setEntity(okayResponseWithFormEntity);
    return okayResponseWithForm;
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 59 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project TaEmCasa by Dionen.

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)

Example 60 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project SmartAndroidSource by jaychou2012.

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