NetworkFileSizeRequester implements FileSizeRequester { @Override public FileSize requestFileSize(String url) { try { long fileSize = executeRequestFileSize(url); if (fileSize == UNKNOWN_CONTENT_LENGTH || fileSize == ZERO_FILE_SIZE) { return FileSizeCreator.unknownFileSize(); } else { return FileSizeCreator.createFromTotalSize(fileSize); } } catch (IOException e) { Logger.e(e, "Error requesting file size for " + url); } return FileSizeCreator.unknownFileSize(); } NetworkFileSizeRequester(HttpClient httpClient, NetworkRequestCreator requestCreator); @Override FileSize requestFileSize(String url);  }
@Test public void returnsUnknownSize_whenHttpClientErrors() throws IOException { given(httpClient.execute(requestCreator.createFileSizeHeadRequest(ANY_RAW_URL))).willThrow(IOException.class); FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL); assertThat(fileSize).isEqualTo(FileSizeCreator.unknownFileSize()); }
@Test public void returnsUnknownSize_whenResponseIsUnsuccessful() throws IOException { given(httpClient.execute(requestCreator.createFileSizeHeadRequest(ANY_RAW_URL))).willReturn(UNSUCCESSFUL_RESPONSE); given(httpClient.execute(requestCreator.createFileSizeBodyRequest(ANY_RAW_URL))).willReturn(UNSUCCESSFUL_RESPONSE); FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL); assertThat(fileSize).isEqualTo(FileSizeCreator.unknownFileSize()); }
@Test public void returnsFileSize_whenResponseSuccessful() throws IOException { given(httpClient.execute(requestCreator.createFileSizeHeadRequest(ANY_RAW_URL))).willReturn(SUCCESSFUL_RESPONSE); FileSize fileSize = fileSizeRequester.requestFileSize(ANY_RAW_URL); assertThat(fileSize).isEqualTo(FileSizeCreator.createFromTotalSize(FILE_BYTES)); }