Search in sources :

Example 1 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxWebAuthTest method testFinishWithState.

@Test
public void testFinishWithState() throws Exception {
    String redirectUri = "http://localhost/finish/with/state/test";
    DbxSessionStore sessionStore = new SimpleSessionStore();
    String state = "test-state";
    DbxWebAuth.Request request = DbxWebAuth.newRequestBuilder().withRedirectUri(redirectUri, sessionStore).withState(state).build();
    // simulate a web server that will not keep the DbxWebAuth
    // instance across requests
    String authorizationUrl = new DbxWebAuth(CONFIG, APP).authorize(request);
    String code = "test-code";
    assertNotNull(sessionStore.get());
    DbxAuthFinish expected = new DbxAuthFinish("test-access-token", "test-user-id", state);
    ByteArrayOutputStream body = new ByteArrayOutputStream();
    ByteArrayInputStream responseStream = new ByteArrayInputStream(("{" + "\"token_type\":\"Bearer\"" + ",\"access_token\":\"" + expected.getAccessToken() + "\"" + ",\"uid\":\"" + expected.getUserId() + "\"" + "}").getBytes("UTF-8"));
    HttpRequestor.Response finishResponse = new HttpRequestor.Response(200, responseStream, new HashMap<String, List<String>>());
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    HttpRequestor.Uploader mockUploader = mock(HttpRequestor.Uploader.class);
    ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
    when(mockUploader.getBody()).thenReturn(body);
    when(mockUploader.finish()).thenReturn(finishResponse);
    when(mockRequestor.startPost(anyString(), anyListOf(HttpRequestor.Header.class))).thenReturn(mockUploader);
    DbxRequestConfig mockConfig = CONFIG.copy().withHttpRequestor(mockRequestor).build();
    DbxAuthFinish actual = new DbxWebAuth(mockConfig, APP).finishFromRedirect(redirectUri, sessionStore, params("code", "test-code", "state", extractQueryParam(authorizationUrl, "state")));
    // verify the state param isn't send to the 'oauth2/token' endpoint
    String finishParams = new String(body.toByteArray(), "UTF-8");
    assertNull(toParamsMap(finishParams).get("state"));
    assertNotNull(actual);
    assertEquals(actual.getAccessToken(), expected.getAccessToken());
    assertEquals(actual.getUserId(), expected.getUserId());
    assertEquals(actual.getUrlState(), expected.getUrlState());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 2 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetrySuccess.

@Test
public void testRetrySuccess() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = new FileMetadata("bar.txt", "id:1HkLjqifwMAAAAAAAAAAAQ", new Date(1456169040985L), new Date(1456169040985L), "2e0c38735597", 2091603);
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    // should have only been called 3 times: initial call + 2 retries
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
    assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 3 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetryDownload.

@Test
public void testRetryDownload() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expectedMetadata = new FileMetadata("download_me.txt", "id:KLavC4viCDAAAAAAAAAAAQ", new Date(1456169692501L), new Date(1456169692501L), "341438735597", 2626);
    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);
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 4 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test 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();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    // 503 once, then return 400
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(400));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.users().getCurrentAccount();
    } finally {
        // should only have been called 2 times: initial call + retry
        verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 5 with HttpRequestor

use of com.dropbox.core.http.HttpRequestor in project dropbox-sdk-java by dropbox.

the class DbxClientV1Test method testRetryDisabled.

@Test(expectedExceptions = RetryException.class)
public void testRetryDisabled() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryDisabled().withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // 503 every time
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503));
    try {
        client.getAccountInfo();
    } finally {
        // should only have been called once since we disabled retry
        verify(mockRequestor, times(1)).doGet(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Aggregations

HttpRequestor (com.dropbox.core.http.HttpRequestor)12 Test (org.testng.annotations.Test)12 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)11 FileMetadata (com.dropbox.core.v2.files.FileMetadata)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Date (java.util.Date)3 Metadata (com.dropbox.core.v2.files.Metadata)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Downloader (com.dropbox.core.v1.DbxClientV1.Downloader)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1