AdaptedHttpStack extends BaseHttpStack { @Override public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { org.apache.http.HttpResponse apacheResp; try { apacheResp = mHttpStack.performRequest(request, additionalHeaders); } catch (ConnectTimeoutException e) { throw new SocketTimeoutException(e.getMessage()); } int statusCode = apacheResp.getStatusLine().getStatusCode(); org.apache.http.Header[] headers = apacheResp.getAllHeaders(); List<Header> headerList = new ArrayList<>(headers.length); for (org.apache.http.Header header : headers) { headerList.add(new Header(header.getName(), header.getValue())); } if (apacheResp.getEntity() == null) { return new HttpResponse(statusCode, headerList); } long contentLength = apacheResp.getEntity().getContentLength(); if ((int) contentLength != contentLength) { throw new IOException("Response too large: " + contentLength); } return new HttpResponse( statusCode, headerList, (int) apacheResp.getEntity().getContentLength(), apacheResp.getEntity().getContent()); } AdaptedHttpStack(HttpStack httpStack); @Override HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders);  }
@Test(expected = SocketTimeoutException.class) public void requestTimeout() throws Exception { when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)) .thenThrow(new ConnectTimeoutException()); mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); }
@Test public void emptyResponse() throws Exception { when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); when(mStatusLine.getStatusCode()).thenReturn(12345); when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); com.android.volley.toolbox.HttpResponse response = mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); assertEquals(12345, response.getStatusCode()); assertEquals(Collections.emptyList(), response.getHeaders()); assertNull(response.getContent()); }
@Test public void nonEmptyResponse() throws Exception { when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); when(mStatusLine.getStatusCode()).thenReturn(12345); when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); when(mHttpResponse.getEntity()).thenReturn(mHttpEntity); when(mHttpEntity.getContentLength()).thenReturn((long) Integer.MAX_VALUE); when(mHttpEntity.getContent()).thenReturn(mContent); com.android.volley.toolbox.HttpResponse response = mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); assertEquals(12345, response.getStatusCode()); assertEquals(Collections.emptyList(), response.getHeaders()); assertEquals(Integer.MAX_VALUE, response.getContentLength()); assertSame(mContent, response.getContent()); }
@Test(expected = IOException.class) public void responseTooBig() throws Exception { when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); when(mStatusLine.getStatusCode()).thenReturn(12345); when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); when(mHttpResponse.getEntity()).thenReturn(mHttpEntity); when(mHttpEntity.getContentLength()).thenReturn(Integer.MAX_VALUE + 1L); when(mHttpEntity.getContent()).thenReturn(mContent); mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); }
@Test public void responseWithHeaders() throws Exception { when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); when(mStatusLine.getStatusCode()).thenReturn(12345); when(mHttpResponse.getAllHeaders()) .thenReturn( new org.apache.http.Header[] { new BasicHeader("header1", "value1_B"), new BasicHeader("header3", "value3"), new BasicHeader("HEADER2", "value2"), new BasicHeader("header1", "value1_A") }); com.android.volley.toolbox.HttpResponse response = mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); assertEquals(12345, response.getStatusCode()); assertNull(response.getContent()); List<Header> expectedHeaders = new ArrayList<>(); expectedHeaders.add(new Header("header1", "value1_B")); expectedHeaders.add(new Header("header3", "value3")); expectedHeaders.add(new Header("HEADER2", "value2")); expectedHeaders.add(new Header("header1", "value1_A")); assertEquals(expectedHeaders, response.getHeaders()); }