HttpClient { public void get(URI uri, Path output, Map<String, ? extends Credentials> credentials, HttpTransferListener listener) throws IOException, HttpError { final Path parent = output.getParent(); final Path tmp = parent.resolve(output.getFileName() + ".part"); Files.createDirectories(parent); try (InputStream in = this.get(uri, credentials, listener)) { Files.copy(in, tmp, REPLACE_EXISTING); } Files.move(tmp, output, REPLACE_EXISTING, ATOMIC_MOVE); } HttpClient(OkHttpClient okHttpClient); static HttpClient newHttpClient(); static HttpClient newHttpClient(OkHttpClient okHttpClient); void get(URI uri, Path output, Map<String, ? extends Credentials> credentials, HttpTransferListener listener); InputStream get(URI uri, Map<String, ? extends Credentials> credentials, HttpTransferListener listener); void partialGet(URI uri, List<ContentRange> ranges, Map<String, ? extends Credentials> credentials,
      RangeReceiver receiver, RangeTransferListener listener);  }
@Test public void testTransferListener() throws IOException, HttpError { final URI uri = URI.create("http: final byte[] data = new byte[17]; final ResponseBody body = mock(ResponseBody.class); when(body.contentLength()).thenReturn((long) data.length); when(body.source()).thenReturn(mock(BufferedSource.class)); final InputStream inputStream = new ByteArrayInputStream(data); when(body.byteStream()).thenReturn(inputStream); final Request request = new Request.Builder().url(uri.toString()).build(); final Response response = new Response.Builder().protocol(HTTP_1_1).body(body).request(request).code(200).build(); final Call mockCall = mock(Call.class); when(mockCall.execute()).thenReturn(response); final OkHttpClient mockHttpClient = mock(OkHttpClient.class); when(mockHttpClient.newCall(any(Request.class))).thenReturn(mockCall); final EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); final InputStream in = new HttpClient(mockHttpClient).get(uri, Collections.<String, Credentials>emptyMap(), listener); final byte[] b = new byte[8]; assertEquals(0, in.read()); assertEquals(8, in.read(b)); assertEquals(8, in.read(b, 0, 8)); assertEquals(-1, in.read()); in.close(); final List<Event> events = ImmutableList.of(new Initialized(new Request.Builder().url(uri.toString()).build()), new Started(uri, data.length), new Transferred(1), new Transferred(8), new Transferred(8), Closed.INSTANCE); assertEquals(events, listener.getEventLog()); }
@Test public void testChallenges() throws IOException, HttpError { final URI uri = URI.create("https: final Map<String, Credentials> credentials = ImmutableMap.of("host", new Credentials("jdoe", "secret")); final MockOkHttpClient okHttpClient = new MockOkHttpClient(); final HttpClient httpClient = new HttpClient(okHttpClient); final HttpTransferListener listener = mock(HttpTransferListener.class); okHttpClient.setNewCall(challengeSequence(uri, credentials)); httpClient.get(uri, credentials, listener); okHttpClient.setNewCall(newAuthorizedRequestCall(uri, credentials)); httpClient.get(uri, credentials, listener); final URI httpUri = URI.create("http: okHttpClient.setNewCall(challengeSequence(httpUri, credentials)); httpClient.get(httpUri, credentials, listener); }