1515import java .io .ByteArrayInputStream ;
1616import java .io .ByteArrayOutputStream ;
1717import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .io .InputStreamReader ;
1820import java .net .HttpURLConnection ;
1921import java .net .URL ;
2022import java .util .HashMap ;
@@ -186,7 +188,8 @@ public void get200() throws Exception {
186188 when (urlConnection .getResponseCode ()).thenReturn (200 );
187189 ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
188190 when (urlConnection .getOutputStream ()).thenReturn (buffer );
189- when (urlConnection .getInputStream ()).thenReturn (new ByteArrayInputStream ("OK" .getBytes ()));
191+ ByteArrayInputStream inputStream = spy (new ByteArrayInputStream ("OK" .getBytes ()));
192+ when (urlConnection .getInputStream ()).thenReturn (inputStream );
190193
191194 /* Configure API client. */
192195 HttpClient .CallTemplate callTemplate = mock (HttpClient .CallTemplate .class );
@@ -208,6 +211,7 @@ public void get200() throws Exception {
208211 verify (urlConnection ).setRequestProperty ("Install-ID" , installId .toString ());
209212 verify (urlConnection , never ()).setDoOutput (true );
210213 verify (urlConnection ).disconnect ();
214+ verify (inputStream ).close ();
211215 verify (callTemplate ).onBeforeCalling (eq (url ), any (Map .class ));
212216 verify (callTemplate , never ()).buildRequestBody ();
213217 httpClient .close ();
@@ -324,6 +328,29 @@ public void failedConnection() throws Exception {
324328 verifyZeroInteractions (serviceCallback );
325329 }
326330
331+ @ Test
332+ public void failedToReadResponse () throws Exception {
333+ URL url = mock (URL .class );
334+ whenNew (URL .class ).withAnyArguments ().thenReturn (url );
335+ IOException exception = new IOException ("mock" );
336+ HttpURLConnection urlConnection = mock (HttpURLConnection .class );
337+ when (url .openConnection ()).thenReturn (urlConnection );
338+ when (urlConnection .getResponseCode ()).thenReturn (200 );
339+ InputStream inputStream = mock (InputStream .class );
340+ when (urlConnection .getInputStream ()).thenReturn (inputStream );
341+ InputStreamReader inputStreamReader = mock (InputStreamReader .class );
342+ whenNew (InputStreamReader .class ).withAnyArguments ().thenReturn (inputStreamReader );
343+ when (inputStreamReader .read (any (char [].class ))).thenThrow (exception );
344+ HttpClient .CallTemplate callTemplate = mock (HttpClient .CallTemplate .class );
345+ ServiceCallback serviceCallback = mock (ServiceCallback .class );
346+ DefaultHttpClient httpClient = new DefaultHttpClient ();
347+ mockCall ();
348+ httpClient .callAsync ("" , "" , new HashMap <String , String >(), callTemplate , serviceCallback );
349+ verify (serviceCallback ).onCallFailed (exception );
350+ verifyZeroInteractions (serviceCallback );
351+ verify (inputStream ).close ();
352+ }
353+
327354 @ Test
328355 public void failedSerialization () throws Exception {
329356
0 commit comments