@Test @PrepareForTest({URL.class, MParticleApiClientImpl.class, MPUtility.class}) public void testMessageBatchWhileThrottled() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); try { client.sendMessageBatch(""); }catch (Exception e) { if (e instanceof MParticleApiClientImpl.MPThrottleException) { throw e; } } client.getRequestHandler().setNextRequestTime(MParticleBaseClientImpl.Endpoint.EVENTS, System.currentTimeMillis() + 1000); Exception e = null; try { client.sendMessageBatch(""); } catch (MParticleApiClientImpl.MPThrottleException cfe) { e = cfe; } assertNotNull(e); }
public int sendMessageBatch(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.EVENTS); checkRampValue(); if (mEventUrl == null){ mEventUrl = getUrl(Endpoint.EVENTS); } MPConnection connection = mEventUrl.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); String activeKits = mConfigManager.getActiveModuleIds(); if (!MPUtility.isEmpty(activeKits)) { connection.setRequestProperty(HEADER_KITS, activeKits); } String supportedKits = getSupportedKitString(); if (!MPUtility.isEmpty(supportedKits)) { connection.setRequestProperty(HEADER_BUNDLED_KITS, supportedKits); } addMessageSignature(connection, message); logUpload(message); try { InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), new JSONObject(message), message); } catch (Exception e) { } makeUrlRequest(Endpoint.EVENTS, connection, message, true); Logger.verbose("Upload request attempt:\n" + "URL- " + mEventUrl.toString()); Logger.verbose(message); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { JSONObject response = MPUtility.getJsonResponse(connection); if (InternalListenerManager.isEnabled()) { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), response, responseCode); } Logger.verbose("Upload result response: \n" + connection.getResponseCode() + ": " + connection.getResponseMessage() + "\n" + "response:\n" + response.toString()); parseCookies(response); } else { Logger.error("Upload request failed- " + responseCode + ": " + connection.getResponseMessage()); try { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().getFile(), new JSONObject().put(SdkListener.ERROR_MESSAGE, connection.getResponseMessage()), responseCode); } catch (Exception e) { } } return connection.getResponseCode(); }
MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { public int sendMessageBatch(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.EVENTS); checkRampValue(); if (mEventUrl == null){ mEventUrl = getUrl(Endpoint.EVENTS); } MPConnection connection = mEventUrl.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); String activeKits = mConfigManager.getActiveModuleIds(); if (!MPUtility.isEmpty(activeKits)) { connection.setRequestProperty(HEADER_KITS, activeKits); } String supportedKits = getSupportedKitString(); if (!MPUtility.isEmpty(supportedKits)) { connection.setRequestProperty(HEADER_BUNDLED_KITS, supportedKits); } addMessageSignature(connection, message); logUpload(message); try { InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), new JSONObject(message), message); } catch (Exception e) { } makeUrlRequest(Endpoint.EVENTS, connection, message, true); Logger.verbose("Upload request attempt:\n" + "URL- " + mEventUrl.toString()); Logger.verbose(message); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { JSONObject response = MPUtility.getJsonResponse(connection); if (InternalListenerManager.isEnabled()) { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), response, responseCode); } Logger.verbose("Upload result response: \n" + connection.getResponseCode() + ": " + connection.getResponseMessage() + "\n" + "response:\n" + response.toString()); parseCookies(response); } else { Logger.error("Upload request failed- " + responseCode + ": " + connection.getResponseMessage()); try { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().getFile(), new JSONObject().put(SdkListener.ERROR_MESSAGE, connection.getResponseMessage()), responseCode); } catch (Exception e) { } } return connection.getResponseCode(); } }
MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { public int sendMessageBatch(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.EVENTS); checkRampValue(); if (mEventUrl == null){ mEventUrl = getUrl(Endpoint.EVENTS); } MPConnection connection = mEventUrl.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); String activeKits = mConfigManager.getActiveModuleIds(); if (!MPUtility.isEmpty(activeKits)) { connection.setRequestProperty(HEADER_KITS, activeKits); } String supportedKits = getSupportedKitString(); if (!MPUtility.isEmpty(supportedKits)) { connection.setRequestProperty(HEADER_BUNDLED_KITS, supportedKits); } addMessageSignature(connection, message); logUpload(message); try { InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), new JSONObject(message), message); } catch (Exception e) { } makeUrlRequest(Endpoint.EVENTS, connection, message, true); Logger.verbose("Upload request attempt:\n" + "URL- " + mEventUrl.toString()); Logger.verbose(message); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { JSONObject response = MPUtility.getJsonResponse(connection); if (InternalListenerManager.isEnabled()) { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), response, responseCode); } Logger.verbose("Upload result response: \n" + connection.getResponseCode() + ": " + connection.getResponseMessage() + "\n" + "response:\n" + response.toString()); parseCookies(response); } else { Logger.error("Upload request failed- " + responseCode + ": " + connection.getResponseMessage()); try { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().getFile(), new JSONObject().put(SdkListener.ERROR_MESSAGE, connection.getResponseMessage()), responseCode); } catch (Exception e) { } } return connection.getResponseCode(); } MParticleApiClientImpl(ConfigManager configManager, SharedPreferences sharedPreferences, Context context); }
MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { public int sendMessageBatch(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.EVENTS); checkRampValue(); if (mEventUrl == null){ mEventUrl = getUrl(Endpoint.EVENTS); } MPConnection connection = mEventUrl.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); String activeKits = mConfigManager.getActiveModuleIds(); if (!MPUtility.isEmpty(activeKits)) { connection.setRequestProperty(HEADER_KITS, activeKits); } String supportedKits = getSupportedKitString(); if (!MPUtility.isEmpty(supportedKits)) { connection.setRequestProperty(HEADER_BUNDLED_KITS, supportedKits); } addMessageSignature(connection, message); logUpload(message); try { InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), new JSONObject(message), message); } catch (Exception e) { } makeUrlRequest(Endpoint.EVENTS, connection, message, true); Logger.verbose("Upload request attempt:\n" + "URL- " + mEventUrl.toString()); Logger.verbose(message); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { JSONObject response = MPUtility.getJsonResponse(connection); if (InternalListenerManager.isEnabled()) { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), response, responseCode); } Logger.verbose("Upload result response: \n" + connection.getResponseCode() + ": " + connection.getResponseMessage() + "\n" + "response:\n" + response.toString()); parseCookies(response); } else { Logger.error("Upload request failed- " + responseCode + ": " + connection.getResponseMessage()); try { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().getFile(), new JSONObject().put(SdkListener.ERROR_MESSAGE, connection.getResponseMessage()), responseCode); } catch (Exception e) { } } return connection.getResponseCode(); } 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(); }
MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { public int sendMessageBatch(String message) throws IOException, MPThrottleException, MPRampException { checkThrottleTime(Endpoint.EVENTS); checkRampValue(); if (mEventUrl == null){ mEventUrl = getUrl(Endpoint.EVENTS); } MPConnection connection = mEventUrl.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); String activeKits = mConfigManager.getActiveModuleIds(); if (!MPUtility.isEmpty(activeKits)) { connection.setRequestProperty(HEADER_KITS, activeKits); } String supportedKits = getSupportedKitString(); if (!MPUtility.isEmpty(supportedKits)) { connection.setRequestProperty(HEADER_BUNDLED_KITS, supportedKits); } addMessageSignature(connection, message); logUpload(message); try { InternalListenerManager.getListener().onNetworkRequestStarted(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), new JSONObject(message), message); } catch (Exception e) { } makeUrlRequest(Endpoint.EVENTS, connection, message, true); Logger.verbose("Upload request attempt:\n" + "URL- " + mEventUrl.toString()); Logger.verbose(message); int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { JSONObject response = MPUtility.getJsonResponse(connection); if (InternalListenerManager.isEnabled()) { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().toString(), response, responseCode); } Logger.verbose("Upload result response: \n" + connection.getResponseCode() + ": " + connection.getResponseMessage() + "\n" + "response:\n" + response.toString()); parseCookies(response); } else { Logger.error("Upload request failed- " + responseCode + ": " + connection.getResponseMessage()); try { InternalListenerManager.getListener().onNetworkRequestFinished(SdkListener.Endpoint.EVENTS, connection.getURL().getFile(), new JSONObject().put(SdkListener.ERROR_MESSAGE, connection.getResponseMessage()), responseCode); } catch (Exception e) { } } return connection.getResponseCode(); } 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();  }