MParticleApiClientImpl extends MParticleBaseClientImpl implements MParticleApiClient { void addMessageSignature(MPConnection request, String message) { try { String date = getHeaderDateString(); request.setRequestProperty("Date", date); request.setRequestProperty(HEADER_SIGNATURE, getHeaderHashString(request, date, message, mApiSecret)); } catch (InvalidKeyException e) { Logger.error("Error signing message."); } catch (NoSuchAlgorithmException e) { Logger.error("Error signing message."); } catch (UnsupportedEncodingException e){ Logger.error("Error signing message."); } } 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 testAddMessageSignature() throws Exception { setup(); PowerMockito.mockStatic(MPUtility.class); Mockito.when(MPUtility.hmacSha256Encode(Mockito.anyString(), Mockito.anyString())).thenReturn("encoded"); ArgumentCaptor<String> headerCapture = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> headerValueCapture = ArgumentCaptor.forClass(String.class); client.addMessageSignature(mockConnection, "this is a sample batch"); Mockito.verify(mockConnection, Mockito.times(2)).setRequestProperty(headerCapture.capture(), headerValueCapture.capture()); List<String> headerKeys = headerCapture.getAllValues(); List<String> headerValues = headerValueCapture.getAllValues(); int dateIndex = headerKeys.indexOf("Date"); assertTrue(headerKeys.toString(), dateIndex >= 0); String dateValue = headerValues.get(dateIndex); assertNotNull(dateValue); assertTrue(dateValue.length() > 0); int signatureIndex = headerKeys.indexOf("x-mp-signature"); assertTrue(headerValues.toString(), signatureIndex >= 0); String signatureValue = headerValues.get(signatureIndex); assertNotNull(signatureValue); assertTrue(signatureValue.length() > 0); }