use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.
the class DbxClientV2Test method testRetryDownload.
@Test
public void testRetryDownload() throws Exception {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
FileMetadata expectedMetadata = constructFileMetadate();
byte[] expectedBytes = new byte[] { 1, 2, 3, 4 };
// 503 once, then return 200
HttpRequestor.Uploader mockUploader = mockUploader();
when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expectedBytes, serialize(expectedMetadata)));
when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
DbxDownloader<FileMetadata> downloader = client.files().download(expectedMetadata.getId());
// should have been attempted twice
verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
FileMetadata actualMetadata = downloader.download(bout);
byte[] actualBytes = bout.toByteArray();
assertEquals(actualBytes, expectedBytes);
assertEquals(actualMetadata, expectedMetadata);
}
use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.
the class DbxClientV1Test method testRetrySuccess.
@Test
public void testRetrySuccess() throws DbxException, IOException {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
String json = "{\"reset\":true,\"entries\":[],\"cursor\":\"fakeCursor\",\"has_more\":true}";
// 503 twice, then return result
HttpRequestor.Uploader mockUploader = mockUploader();
when(mockUploader.finish()).thenReturn(// no backoff
createEmptyResponse(503)).thenReturn(// backoff 1 sec
createRateLimitResponse(1)).thenReturn(// backoff 2 sec
createRateLimitResponse(2)).thenReturn(createSuccessResponse(json));
when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
long start = System.currentTimeMillis();
DbxDelta<DbxEntry> actual = client.getDelta(null);
long end = System.currentTimeMillis();
// no way easy way to properly test this, but request should
// have taken AT LEAST 3 seconds due to backoff.
assertTrue((end - start) >= 3000L, "duration: " + (end - start) + " millis");
// should have been called 4 times: initial call + 3 retries
verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
assertEquals(actual.reset, true);
assertEquals(actual.cursor, "fakeCursor");
}
use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.
the class DbxClientV1Test method testRetryDownload.
@Test
public void testRetryDownload() throws DbxException, IOException {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
// load File metadata json
InputStream in = getClass().getResourceAsStream("/file-with-photo-info.json");
assertNotNull(in);
String metadataJson = IOUtil.toUtf8String(in);
byte[] expected = new byte[] { 1, 2, 3, 4 };
// 503 once, then return 200
when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expected, "X-Dropbox-Metadata", metadataJson));
Downloader downloader = client.startGetThumbnail(DbxThumbnailSize.w64h64, DbxThumbnailFormat.JPEG, "/foo/bar.jpg", null);
// should have been attempted twice
verify(mockRequestor, times(2)).doGet(anyString(), anyHeaders());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IOUtil.copyStreamToStream(downloader.body, bout);
byte[] actual = bout.toByteArray();
assertEquals(actual, expected);
assertEquals(downloader.metadata.path, "/Photos/Sample Album/Boston City Flow.jpg");
}
use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.
the class DbxClientV1Test method testRetryRetriesExceeded.
@Test(expectedExceptions = RetryException.class)
public void testRetryRetriesExceeded() throws DbxException, IOException {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
// 503 always and forever
when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503));
try {
client.getAccountInfo();
} finally {
// should only have been called 4: initial call + max number of retries (3)
verify(mockRequestor, times(4)).doGet(anyString(), anyHeaders());
}
}
use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.
the class DbxClientV1Test method testRetryOtherFailure.
@Test(expectedExceptions = BadRequestException.class)
public void testRetryOtherFailure() throws DbxException, IOException {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
// 503 once, then return 400
when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(400));
try {
client.getAccountInfo();
} finally {
// should only have been called 2 times: initial call + one retry
verify(mockRequestor, times(2)).doGet(anyString(), anyHeaders());
}
}
Aggregations