HurlStack extends BaseHttpStack { @Override public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<>(); map.putAll(additionalHeaders); map.putAll(request.getHeaders()); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); boolean keepConnectionOpen = false; try { for (String headerName : map.keySet()) { connection.setRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); int responseCode = connection.getResponseCode(); if (responseCode == -1) { throw new IOException("Could not retrieve response code from HttpUrlConnection."); } if (!hasResponseBody(request.getMethod(), responseCode)) { return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields())); } keepConnectionOpen = true; return new HttpResponse( responseCode, convertHeaders(connection.getHeaderFields()), connection.getContentLength(), new UrlConnectionInputStream(connection)); } finally { if (!keepConnectionOpen) { connection.disconnect(); } } } HurlStack();  HurlStack(UrlRewriter urlRewriter);  HurlStack(UrlRewriter urlRewriter, SSLSocketFactory sslSocketFactory); @Override HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders);  }
@Test public void executeRequestClosesConnection_connectionError() throws Exception { when(mMockConnection.getResponseCode()).thenThrow(new SocketTimeoutException()); try { mHurlStack.executeRequest( new TestRequest.Get(), Collections.<String, String>emptyMap()); fail("Should have thrown exception"); } catch (IOException e) { verify(mMockConnection).disconnect(); } }
@Test public void executeRequestClosesConnection_invalidResponseCode() throws Exception { when(mMockConnection.getResponseCode()).thenReturn(-1); try { mHurlStack.executeRequest( new TestRequest.Get(), Collections.<String, String>emptyMap()); fail("Should have thrown exception"); } catch (IOException e) { verify(mMockConnection).disconnect(); } }
@Test public void executeRequestClosesConnection_noResponseBody() throws Exception { when(mMockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_NO_CONTENT); mHurlStack.executeRequest(new TestRequest.Get(), Collections.<String, String>emptyMap()); verify(mMockConnection).disconnect(); }
@Test public void executeRequestClosesConnection_hasResponseBody() throws Exception { when(mMockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); when(mMockConnection.getInputStream()) .thenReturn(new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8))); HttpResponse response = mHurlStack.executeRequest( new TestRequest.Get(), Collections.<String, String>emptyMap()); verify(mMockConnection, never()).disconnect(); response.getContent().close(); verify(mMockConnection).disconnect(); }