BasicNetwork implements Network { @Override public NetworkResponse performRequest(Request<?> request) throws VolleyError { long requestStart = SystemClock.elapsedRealtime(); while (true) { HttpResponse httpResponse = null; byte[] responseContents = null; List<Header> responseHeaders = Collections.emptyList(); try { Map<String, String> additionalRequestHeaders = getCacheHeaders(request.getCacheEntry()); httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders); int statusCode = httpResponse.getStatusCode(); responseHeaders = httpResponse.getHeaders(); if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry == null) { return new NetworkResponse( HttpURLConnection.HTTP_NOT_MODIFIED, null, true, SystemClock.elapsedRealtime() - requestStart, responseHeaders); } List<Header> combinedHeaders = combineHeaders(responseHeaders, entry); return new NetworkResponse( HttpURLConnection.HTTP_NOT_MODIFIED, entry.data, true, SystemClock.elapsedRealtime() - requestStart, combinedHeaders); } InputStream inputStream = httpResponse.getContent(); if (inputStream != null) { responseContents = inputStreamToBytes(inputStream, httpResponse.getContentLength()); } else { responseContents = new byte[0]; } long requestLifetime = SystemClock.elapsedRealtime() - requestStart; logSlowRequests(requestLifetime, request, responseContents, statusCode); if (statusCode < 200 || statusCode > 299) { throw new IOException(); } return new NetworkResponse( statusCode, responseContents, false, SystemClock.elapsedRealtime() - requestStart, responseHeaders); } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError()); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrl(), e); } catch (IOException e) { int statusCode; if (httpResponse != null) { statusCode = httpResponse.getStatusCode(); } else { throw new NoConnectionError(e); } VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl()); NetworkResponse networkResponse; if (responseContents != null) { networkResponse = new NetworkResponse( statusCode, responseContents, false, SystemClock.elapsedRealtime() - requestStart, responseHeaders); if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED || statusCode == HttpURLConnection.HTTP_FORBIDDEN) { attemptRetryOnException( "auth", request, new AuthFailureError(networkResponse)); } else if (statusCode >= 400 && statusCode <= 499) { throw new ClientError(networkResponse); } else if (statusCode >= 500 && statusCode <= 599) { if (request.shouldRetryServerErrors()) { attemptRetryOnException( "server", request, new ServerError(networkResponse)); } else { throw new ServerError(networkResponse); } } else { throw new ServerError(networkResponse); } } else { attemptRetryOnException("network", request, new NetworkError()); } } } } @Deprecated  BasicNetwork(HttpStack httpStack); @Deprecated  BasicNetwork(HttpStack httpStack, ByteArrayPool pool);  BasicNetwork(BaseHttpStack httpStack);  BasicNetwork(BaseHttpStack httpStack, ByteArrayPool pool); @Override NetworkResponse performRequest(Request<?> request);  }
@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)); }
@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<>(); 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]))); }
@Test public void notModified_legacyCache() 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.responseHeaders = new HashMap<>(); entry.responseHeaders.put("CachedKeyA", "CachedValueA"); entry.responseHeaders.put("CachedKeyB", "CachedValueB"); entry.responseHeaders.put("SharedKey", "CachedValueShared"); entry.responseHeaders.put("SHAREDCASEINSENSITIVEKEY", "CachedValueShared1"); entry.responseHeaders.put("shAREDcaSEinSENSITIVEkeY", "CachedValueShared2"); request.setCacheEntry(entry); NetworkResponse response = httpNetwork.performRequest(request); List<Header> expectedHeaders = new ArrayList<>(); 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]))); }
@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) { } verify(mMockRetryPolicy).retry(any(TimeoutError.class)); }
@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) { } verify(mMockRetryPolicy, never()).retry(any(VolleyError.class)); }
@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) { } verify(mMockRetryPolicy).retry(any(AuthFailureError.class)); }
@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) { } verify(mMockRetryPolicy).retry(any(AuthFailureError.class)); }
@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) { } verify(mMockRetryPolicy, never()).retry(any(VolleyError.class)); reset(mMockRetryPolicy); } }
@Test public void otherClientError() throws Exception { for (int i = 400; i <= 499; i++) { if (i == 401 || i == 403) { 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) { } verify(mMockRetryPolicy, never()).retry(any(VolleyError.class)); reset(mMockRetryPolicy); } }
@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) { } verify(mMockRetryPolicy).retry(any(ServerError.class)); reset(mMockRetryPolicy); } }
@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) { } verify(mMockRetryPolicy, never()).retry(any(VolleyError.class)); reset(mMockRetryPolicy); } }