use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class MSALoginActivity method getToken.
/**
* Get initial access token.
*/
private void getToken(final String code) {
Map<String, String> headers = new HashMap<>();
headers.put(DefaultHttpClient.CONTENT_TYPE_KEY, "application/x-www-form-urlencoded");
sHttpClient.callAsync(TOKEN_URL, DefaultHttpClient.METHOD_POST, headers, new HttpClient.CallTemplate() {
@Override
public String buildRequestBody() {
return REDIRECT_URI_PARAM + CLIENT_ID_PARAM + "&grant_type=authorization_code" + "&code=" + code;
}
@Override
public void onBeforeCalling(URL url, Map<String, String> headers) {
AppCenterLog.verbose(AppCenter.LOG_TAG, "Calling " + url + "...");
}
}, new ServiceCallback() {
@Override
public void onCallSucceeded(HttpResponse httpResponse) {
try {
JSONObject response = new JSONObject(httpResponse.getPayload());
String userId = response.getString(USER_ID);
mRefreshToken = response.getString(REFRESH_TOKEN);
mRefreshTokenScope = response.getString(SCOPE);
registerAppCenterAuthentication(userId);
} catch (JSONException e) {
onCallFailed(e);
}
}
@Override
public void onCallFailed(Exception e) {
handleCallFailure(e);
}
});
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method releaseFailureWithDifferentIds.
@Test
public void releaseFailureWithDifferentIds() {
/* Mock we already have token. */
when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
/* Do the call so that ids do not match. */
Distribute.getInstance().getLatestReleaseDetails("mockGroup", "token");
((ServiceCallback) invocation.getArguments()[4]).onCallFailed(new HttpException(new HttpResponse(503)));
return mock(ServiceCall.class);
}
}).thenAnswer(new Answer<ServiceCall>() {
/* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
@Override
public ServiceCall answer(InvocationOnMock invocation) {
return mock(ServiceCall.class);
}
});
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify on failure we don't complete workflow if ids don't match. */
verifyStatic(never());
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method releaseSuccessActivityIsNull.
@Test
public void releaseSuccessActivityIsNull() {
when(SharedPreferencesManager.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
/* Update is more recent. */
when(mReleaseDetails.getVersion()).thenReturn(7);
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) {
/* Do the call so that id had changed. */
Distribute.getInstance().onActivityPaused(mActivity);
((ServiceCallback) invocation.getArguments()[4]).onCallSucceeded(new HttpResponse(200, "mock", Collections.<String, String>emptyMap()));
return mock(ServiceCall.class);
}
}).thenAnswer(new Answer<ServiceCall>() {
/* On second time we don't answer as it's callback from getLatestReleaseDetails above. */
@Override
public ServiceCall answer(InvocationOnMock invocation) {
return mock(ServiceCall.class);
}
});
/* Trigger call. */
start();
Distribute.getInstance().onActivityResumed(mock(Activity.class));
/* Verify on failure we don't complete workflow if ids don't match. */
verifyStatic(never());
SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404urlNotFound.
@Test
public void checkReleaseFailsWithSome404urlNotFound() throws Exception {
/* Mock error parsing. */
mockStatic(ErrorDetails.class);
final String errorPayload = "<html>Not Found</html>";
when(ErrorDetails.parse(errorPayload)).thenThrow(new JSONException("Expected {"));
final Exception exception = new HttpException(new HttpResponse(404, errorPayload));
checkReleaseFailure(exception, times(1));
}
use of com.microsoft.appcenter.http.HttpResponse in project mobile-center-sdk-android by Microsoft.
the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404noRelease.
@Test
public void checkReleaseFailsWithSome404noRelease() throws Exception {
/* Mock error parsing. */
ErrorDetails errorDetails = mock(ErrorDetails.class);
when(errorDetails.getCode()).thenReturn(ErrorDetails.NO_RELEASES_FOR_USER_CODE);
mockStatic(ErrorDetails.class);
String errorPayload = "{code: 'no_releases_for_user'}";
when(ErrorDetails.parse(errorPayload)).thenReturn(errorDetails);
checkReleaseFailure(new HttpException(new HttpResponse(404, errorPayload)), never());
}
Aggregations