NetworkFileDownloader implements FileDownloader { @Override public void startDownloading(String url, FileSize fileSize, Callback callback) { canDownload = true; NetworkRequest request = createRequestFrom(url, fileSize); NetworkResponse response = null; try { response = httpClient.execute(request); int responseCode = response.code(); processResponse(callback, response, responseCode, url); } catch (IOException e) { Logger.e(e, "Exception with http request"); callback.onError(e.getMessage()); } finally { try { if (response != null) { response.closeByteStream(); } } catch (IOException e) { Logger.e(e, "Exception while closing the body response"); } } callback.onDownloadFinished(); } NetworkFileDownloader(HttpClient httpClient, NetworkRequestCreator requestCreator); @Override void startDownloading(String url, FileSize fileSize, Callback callback); @Override void stopDownloading();  }
@Test public void emitsBytes_whenDownloading() throws IOException { NetworkRequest downloadRequest = requestCreator.createDownloadRequestWithDownloadedBytesHeader(ANY_RAW_URL, KNOWN_FILE_SIZE.currentSize(), KNOWN_FILE_SIZE.totalSize()); given(httpClient.execute(downloadRequest)).willReturn(RESPONSE_WITH_INPUT_STREAM); networkFileDownloader.startDownloading(ANY_RAW_URL, KNOWN_FILE_SIZE, callback); byte[] expectedBytes = new byte[4096]; expectedBytes[0] = "s".getBytes()[0]; verify(callback).onBytesRead(expectedBytes, 1); }
@Test public void closesByteStream_whenDownloaded() throws IOException { NetworkResponse networkResponse = spy(aNetworkResponse().build()); given(httpClient.execute(requestCreator.createDownloadRequest(ANY_RAW_URL))).willReturn(networkResponse); networkFileDownloader.startDownloading(ANY_RAW_URL, UNKNOWN_FILE_SIZE, callback); verify(networkResponse).closeByteStream(); }
@Test public void emitsError_whenResponseCodeIsInvalid() throws IOException { given(httpClient.execute(requestCreator.createDownloadRequest(ANY_RAW_URL))).willReturn(INVALID_RESPONSE); networkFileDownloader.startDownloading(ANY_RAW_URL, UNKNOWN_FILE_SIZE, callback); String expectedCause = "Request: http: verify(callback).onError(expectedCause); }
@Test public void emitsDownloadFinished_whenResponseCodeIsInvalid() throws IOException { given(httpClient.execute(requestCreator.createDownloadRequest(ANY_RAW_URL))).willReturn(INVALID_RESPONSE); networkFileDownloader.startDownloading(ANY_RAW_URL, UNKNOWN_FILE_SIZE, callback); verify(callback).onDownloadFinished(); }
@Test public void emitsError_whenRequestExecutionFails() throws IOException { IOException ioException = mock(IOException.class); given(ioException.getMessage()).willReturn(MESSAGE); given(httpClient.execute(requestCreator.createDownloadRequest(ANY_RAW_URL))).willThrow(ioException); networkFileDownloader.startDownloading(ANY_RAW_URL, UNKNOWN_FILE_SIZE, callback); verify(callback).onError(MESSAGE); }
@Test public void emitsDownloadFinished_whenRequestExecutionFails() throws IOException { given(httpClient.execute(requestCreator.createDownloadRequest(ANY_RAW_URL))).willThrow(IOException.class); networkFileDownloader.startDownloading(ANY_RAW_URL, UNKNOWN_FILE_SIZE, callback); verify(callback).onDownloadFinished(); }