MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { @Override public AliasNetworkResponse sendAliasRequest(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.ALIAS); Logger.verbose("Identity alias request:\n" + message); if (mAliasUrl == null) { mAliasUrl = getUrl(Endpoint.ALIAS); } MPConnection connection = mAliasUrl.openConnection(); connection.setConnectTimeout(mConfigManager.getConnectionTimeout()); connection.setReadTimeout(mConfigManager.getConnectionTimeout()); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Content-Encoding", "gzip"); connection.setRequestProperty("User-Agent", mUserAgent); addMessageSignature(connection, message); String url = ""; try { url = connection.getURL().toString(); InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, url, new JSONObject(message), message); } catch (Exception ignore) {} connection = makeUrlRequest(Endpoint.ALIAS, connection, message, false); int responseCode = connection.getResponseCode(); String error = ""; JSONObject response = new JSONObject(); if (responseCode >= 200 && responseCode < 300) { Logger.verbose("Alias Request response: \n " + connection.getResponseCode() + ": " + connection.getResponseMessage()); } else { response = MPUtility.getJsonResponse(connection); if (response != null) { error = response.optString(ALIAS_ERROR_MESSAGE); } Logger.error("Alias Request failed- " + responseCode + ": " + error); } InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, url, response, responseCode); return new AliasNetworkResponse(responseCode, error); } MParticleApiClientImpl(ConfigManager configManager, SharedPreferences sharedPreferences, Context context); @Override void fetchConfig(); void fetchConfig(boolean force); JSONObject fetchAudiences(); int sendMessageBatch(String message); @Override AliasNetworkResponse sendAliasRequest(String message); void setCookies(JSONObject serverCookies); JSONObject getCookies();  }
@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testAliasRequestWhileThrottled() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); try { client.sendAliasRequest(""); } catch (Exception e) { if (e instanceof MParticleApiClientImpl.MPThrottleException) { throw e; } } client.getRequestHandler().setNextRequestTime(MParticleBaseClientImpl.Endpoint.ALIAS, System.currentTimeMillis() + 1000); Exception e = null; try { client.sendAliasRequest(""); } catch (MParticleApiClientImpl.MPThrottleException cfe) { e = cfe; } assertNotNull(e); }