Search in sources :

Example 1 with HttpClient

use of com.microsoft.azure.mobile.http.HttpClient in project mobile-center-sdk-android by Microsoft.

the class DistributeBeforeApiSuccessTest method disableWhileCheckingRelease.

@Test
public void disableWhileCheckingRelease() throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    ServiceCall firstCall = mock(ServiceCall.class);
    when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(firstCall).thenReturn(mock(ServiceCall.class));
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* The call is only triggered when app is resumed. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    verify(httpClient, never()).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Verify cancel on disabling. */
    verify(firstCall, never()).cancel();
    Distribute.setEnabled(false);
    verify(firstCall).cancel();
    /* No more call on that one. */
    Distribute.setEnabled(true);
    Distribute.setEnabled(false);
    verify(firstCall).cancel();
}
Also used : ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HashMap(java.util.HashMap) HttpClient(com.microsoft.azure.mobile.http.HttpClient) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Matchers.anyString(org.mockito.Matchers.anyString) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with HttpClient

use of com.microsoft.azure.mobile.http.HttpClient in project mobile-center-sdk-android by Microsoft.

the class Distribute method getLatestReleaseDetails.

/**
     * Get latest release details from server.
     *
     * @param updateToken token to secure API call.
     */
@VisibleForTesting
synchronized void getLatestReleaseDetails(@NonNull String updateToken) {
    MobileCenterLog.debug(LOG_TAG, "Get latest release details...");
    HttpClientRetryer retryer = new HttpClientRetryer(new DefaultHttpClient());
    NetworkStateHelper networkStateHelper = NetworkStateHelper.getSharedInstance(mContext);
    HttpClient httpClient = new HttpClientNetworkStateHandler(retryer, networkStateHelper);
    String url = mApiUrl + String.format(GET_LATEST_RELEASE_PATH_FORMAT, mAppSecret, computeReleaseHash(mPackageInfo));
    Map<String, String> headers = new HashMap<>();
    headers.put(HEADER_API_TOKEN, updateToken);
    final Object releaseCallId = mCheckReleaseCallId = new Object();
    mCheckReleaseApiCall = httpClient.callAsync(url, METHOD_GET, headers, new HttpClient.CallTemplate() {

        @Override
        public String buildRequestBody() throws JSONException {
            /* Only GET is used by Distribute service. This method is never getting called. */
            return null;
        }

        @Override
        public void onBeforeCalling(URL url, Map<String, String> headers) {
            if (MobileCenterLog.getLogLevel() <= VERBOSE) {
                /* Log url. */
                String urlString = url.toString().replaceAll(mAppSecret, HttpUtils.hideSecret(mAppSecret));
                MobileCenterLog.verbose(LOG_TAG, "Calling " + urlString + "...");
                /* Log headers. */
                Map<String, String> logHeaders = new HashMap<>(headers);
                String apiToken = logHeaders.get(HEADER_API_TOKEN);
                if (apiToken != null) {
                    logHeaders.put(HEADER_API_TOKEN, HttpUtils.hideSecret(apiToken));
                }
                MobileCenterLog.verbose(LOG_TAG, "Headers: " + logHeaders);
            }
        }
    }, new ServiceCallback() {

        @Override
        public void onCallSucceeded(final String payload) {
            /* onPostExecute is not always called on UI thread due to an old Android bug. */
            HandlerUtils.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        handleApiCallSuccess(releaseCallId, payload, ReleaseDetails.parse(payload));
                    } catch (JSONException e) {
                        onCallFailed(e);
                    }
                }
            });
        }

        @Override
        public void onCallFailed(Exception e) {
            handleApiCallFailure(releaseCallId, e);
        }
    });
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) DefaultHttpClient(com.microsoft.azure.mobile.http.DefaultHttpClient) DEFAULT_API_URL(com.microsoft.azure.mobile.distribute.DistributeConstants.DEFAULT_API_URL) URL(java.net.URL) DEFAULT_INSTALL_URL(com.microsoft.azure.mobile.distribute.DistributeConstants.DEFAULT_INSTALL_URL) JSONException(org.json.JSONException) ActivityNotFoundException(android.content.ActivityNotFoundException) HttpException(com.microsoft.azure.mobile.http.HttpException) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HttpClientRetryer(com.microsoft.azure.mobile.http.HttpClientRetryer) HttpClient(com.microsoft.azure.mobile.http.HttpClient) DefaultHttpClient(com.microsoft.azure.mobile.http.DefaultHttpClient) NetworkStateHelper(com.microsoft.azure.mobile.utils.NetworkStateHelper) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) Map(java.util.Map) HashMap(java.util.HashMap) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

HttpClient (com.microsoft.azure.mobile.http.HttpClient)2 HttpClientNetworkStateHandler (com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)2 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)2 HashMap (java.util.HashMap)2 Activity (android.app.Activity)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 Channel (com.microsoft.azure.mobile.channel.Channel)1 DEFAULT_API_URL (com.microsoft.azure.mobile.distribute.DistributeConstants.DEFAULT_API_URL)1 DEFAULT_INSTALL_URL (com.microsoft.azure.mobile.distribute.DistributeConstants.DEFAULT_INSTALL_URL)1 DefaultHttpClient (com.microsoft.azure.mobile.http.DefaultHttpClient)1 HttpClientRetryer (com.microsoft.azure.mobile.http.HttpClientRetryer)1 HttpException (com.microsoft.azure.mobile.http.HttpException)1 ServiceCall (com.microsoft.azure.mobile.http.ServiceCall)1 NetworkStateHelper (com.microsoft.azure.mobile.utils.NetworkStateHelper)1 URL (java.net.URL)1 Map (java.util.Map)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1