com.android.volley.mock.MockHttpStack Java Examples

The following examples show how to use com.android.volley.mock.MockHttpStack. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void otherClientError() throws Exception {
    for (int i = 400; i <= 499; i++) {
        if (i == 401 || i == 403) {
            // covered above.
            continue;
        }
        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 other 400 errors.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #2
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void forbidden() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
            403, "Forbidden");
    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));
}
 
Example #3
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #4
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void otherClientError() throws Exception {
    for (int i = 400; i <= 499; i++) {
        if (i == 401 || i == 403) {
            // covered above.
            continue;
        }
        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 other 400 errors.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #5
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void headersAndPostParams() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    InputStream responseStream =
            new ByteArrayInputStream("foobar".getBytes(StandardCharsets.UTF_8));
    HttpResponse fakeResponse =
            new HttpResponse(200, Collections.<Header>emptyList(), 6, responseStream);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.etag = "foobar";
    entry.lastModified = 1503102002000L;
    request.setCacheEntry(entry);
    httpNetwork.performRequest(request);
    assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
    assertEquals("foobar", mockHttpStack.getLastHeaders().get("If-None-Match"));
    assertEquals(
            "Sat, 19 Aug 2017 00:20:02 GMT",
            mockHttpStack.getLastHeaders().get("If-Modified-Since"));
    assertEquals(
            "requestpost=foo&",
            new String(mockHttpStack.getLastPostBody(), StandardCharsets.UTF_8));
}
 
Example #6
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_disableRetries() throws Exception {
    for (int i = 500; i <= 599; 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 any 500 error w/ HTTP 500 retries turned off (the default).
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #7
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void forbidden() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
            403, "Forbidden");
    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));
}
 
Example #9
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #10
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(
                new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #11
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse =
                new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork =
                new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #12
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_disableRetries() throws Exception {
    for (int i = 500; i <= 599; 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 any 500 error w/ HTTP 500 retries turned off (the default).
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #13
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #14
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void forbidden() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
            403, "Forbidden");
    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));
}
 
Example #15
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #16
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void otherClientError() throws Exception {
    for (int i = 400; i <= 499; i++) {
        if (i == 401 || i == 403) {
            // covered above.
            continue;
        }
        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 other 400 errors.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #17
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse =
                new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork =
                new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #18
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_disableRetries() throws Exception {
    for (int i = 500; i <= 599; 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 any 500 error w/ HTTP 500 retries turned off (the default).
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #19
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #20
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #21
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void noConnectionDefault() 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));
}
 
Example #22
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void noConnectionRetry() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    mockHttpStack.setExceptionToThrow(new IOException());
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    request.setShouldRetryConnectionErrors(true);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
        // expected
    }
    // should retry when there is no connection
    verify(mMockRetryPolicy).retry(any(NoConnectionError.class));
    reset(mMockRetryPolicy);
}
 
Example #23
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void noConnectionNoRetry() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    mockHttpStack.setExceptionToThrow(new IOException());
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    request.setShouldRetryConnectionErrors(false);
    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));
}
 
Example #24
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void serverError_disableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        HttpResponse fakeResponse = new HttpResponse(i, Collections.<Header>emptyList());
        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 any 500 error w/ HTTP 500 retries turned off (the default).
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #25
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        HttpResponse fakeResponse = new HttpResponse(i, Collections.<Header>emptyList());
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #26
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void otherClientError() throws Exception {
    for (int i = 400; i <= 499; i++) {
        if (i == 401 || i == 403) {
            // covered above.
            continue;
        }
        MockHttpStack mockHttpStack = new MockHttpStack();
        HttpResponse fakeResponse = new HttpResponse(i, Collections.<Header>emptyList());
        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 other 400 errors.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #27
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void redirect() throws Exception {
    for (int i = 300; i <= 399; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        HttpResponse fakeResponse = new HttpResponse(i, Collections.<Header>emptyList());
        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);
    }
}
 
Example #28
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void forbidden() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    HttpResponse fakeResponse = new HttpResponse(403, Collections.<Header>emptyList());
    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));
}
 
Example #29
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void unauthorized() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    HttpResponse fakeResponse = new HttpResponse(401, Collections.<Header>emptyList());
    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));
}
 
Example #30
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Test
public void notModified() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    List<Header> headers = new ArrayList<>();
    headers.add(new Header("ServerKeyA", "ServerValueA"));
    headers.add(new Header("ServerKeyB", "ServerValueB"));
    headers.add(new Header("SharedKey", "ServerValueShared"));
    headers.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    headers.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    HttpResponse fakeResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_MODIFIED, headers);
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    Entry entry = new Entry();
    entry.allResponseHeaders = new ArrayList<>();
    entry.allResponseHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    entry.allResponseHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    entry.allResponseHeaders.add(new Header("SharedKey", "CachedValueShared"));
    entry.allResponseHeaders.add(new Header("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1"));
    entry.allResponseHeaders.add(new Header("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2"));
    request.setCacheEntry(entry);
    NetworkResponse response = httpNetwork.performRequest(request);
    List<Header> expectedHeaders = new ArrayList<>();
    // Should have all server headers + cache headers that didn't show up in server response.
    expectedHeaders.add(new Header("ServerKeyA", "ServerValueA"));
    expectedHeaders.add(new Header("ServerKeyB", "ServerValueB"));
    expectedHeaders.add(new Header("SharedKey", "ServerValueShared"));
    expectedHeaders.add(new Header("sharedcaseinsensitivekey", "ServerValueShared1"));
    expectedHeaders.add(new Header("SharedCaseInsensitiveKey", "ServerValueShared2"));
    expectedHeaders.add(new Header("CachedKeyA", "CachedValueA"));
    expectedHeaders.add(new Header("CachedKeyB", "CachedValueB"));
    assertThat(expectedHeaders, containsInAnyOrder(response.allHeaders.toArray(new Header[0])));
}