Search in sources :

Example 1 with MockHttpStack

use of com.android.volley.mock.MockHttpStack in project TaEmCasa by Dionen.

the class BasicNetworkTest method connectTimeout.

@Test
public void connectTimeout() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    mockHttpStack.setExceptionToThrow(new ConnectTimeoutException());
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should retry connection timeouts
    verify(mMockRetryPolicy).retry(any(TimeoutError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) TimeoutError(com.android.volley.TimeoutError) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) Test(org.junit.Test)

Example 2 with MockHttpStack

use of com.android.volley.mock.MockHttpStack in project TaEmCasa by Dionen.

the class BasicNetworkTest method redirect.

@Test
public void redirect() throws Exception {
    for (int i = 300; i <= 399; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
        // expected
        }
        // should not retry 300 responses.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 3 with MockHttpStack

use of com.android.volley.mock.MockHttpStack in project TaEmCasa by Dionen.

the class BasicNetworkTest method noConnection.

@Test
public void noConnection() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    mockHttpStack.setExceptionToThrow(new IOException());
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should not retry when there is no connection
    verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with MockHttpStack

use of com.android.volley.mock.MockHttpStack in project TaEmCasa by Dionen.

the class BasicNetworkTest method unauthorized.

@Test
public void unauthorized() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 401, "Unauthorized");
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should retry in case it's an auth failure.
    verify(mMockRetryPolicy).retry(any(AuthFailureError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) AuthFailureError(com.android.volley.AuthFailureError) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 5 with MockHttpStack

use of com.android.volley.mock.MockHttpStack in project TaEmCasa by Dionen.

the class BasicNetworkTest method socketTimeout.

@Test
public void socketTimeout() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    mockHttpStack.setExceptionToThrow(new SocketTimeoutException());
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should retry socket timeouts
    verify(mMockRetryPolicy).retry(any(TimeoutError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutError(com.android.volley.TimeoutError) Test(org.junit.Test)

Aggregations

MockHttpStack (com.android.volley.mock.MockHttpStack)13 Test (org.junit.Test)12 ProtocolVersion (org.apache.http.ProtocolVersion)10 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)10 VolleyError (com.android.volley.VolleyError)9 StringEntity (org.apache.http.entity.StringEntity)4 NetworkResponse (com.android.volley.NetworkResponse)3 Request (com.android.volley.Request)3 HashMap (java.util.HashMap)3 AuthFailureError (com.android.volley.AuthFailureError)2 TimeoutError (com.android.volley.TimeoutError)2 ServerError (com.android.volley.ServerError)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)1