2323import static com .mastercard .developer .test .TestUtils .assertPayloadEquals ;
2424import static com .mastercard .developer .test .TestUtils .getTestFieldLevelEncryptionConfigBuilder ;
2525import static org .hamcrest .core .Is .isA ;
26+ import static org .junit .Assert .*;
2627import static org .mockito .Mockito .*;
2728
2829@ RunWith (MockitoJUnitRunner .class )
@@ -57,7 +58,7 @@ public void testIntercept_ShouldEncryptRequestPayloadAndUpdateContentLengthHeade
5758 String encryptedPayload = encryptedPayloadStream .toString (StandardCharsets .UTF_8 .name ());
5859 Assert .assertFalse (encryptedPayload .contains ("foo" ));
5960 Assert .assertTrue (encryptedPayload .contains ("encryptedFoo" ));
60- Assert . assertEquals (encryptedPayload .length (), httpHeaders .getContentLength ().intValue ());
61+ assertEquals (encryptedPayload .length (), httpHeaders .getContentLength ().intValue ());
6162 }
6263
6364 @ Test
@@ -117,6 +118,7 @@ public void testInterceptResponse_ShouldDecryptResponsePayloadAndUpdateContentLe
117118 .build ();
118119 HttpResponse response = mock (HttpResponse .class );
119120 HttpHeaders httpHeaders = new HttpHeaders ();
121+ httpHeaders .setContentLength (100l );
120122 when (response .parseAsString ()).thenReturn (encryptedPayload );
121123 when (response .getHeaders ()).thenReturn (httpHeaders );
122124
@@ -130,7 +132,7 @@ public void testInterceptResponse_ShouldDecryptResponsePayloadAndUpdateContentLe
130132 InputStream payloadInputStream = (InputStream ) contentField .get (response );
131133 String payload = IOUtils .toString (payloadInputStream , StandardCharsets .UTF_8 );
132134 assertPayloadEquals ("{\" data\" :\" string\" }" , payload );
133- Assert . assertEquals (payload .length (), httpHeaders .getContentLength ().intValue ());
135+ assertEquals (payload .length (), httpHeaders .getContentLength ().intValue ());
134136 }
135137
136138 @ Test
@@ -176,4 +178,88 @@ public void testInterceptResponse_ShouldThrowIOException_WhenDecryptionFails() t
176178 HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor (config );
177179 instanceUnderTest .interceptResponse (response );
178180 }
181+
182+ @ Test
183+ public void testIntercept_ShouldEncryptRequestPayloadAndAddEncryptionHttpHeaders_WhenRequestedInConfig () throws Exception {
184+
185+ // GIVEN
186+ FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder ()
187+ .withEncryptionPath ("$.foo" , "$.encryptedFoo" )
188+ .withIvHeaderName ("x-iv" )
189+ .withEncryptedKeyHeaderName ("x-encrypted-key" )
190+ .withOaepPaddingDigestAlgorithmHeaderName ("x-oaep-padding-digest-algorithm" )
191+ .withEncryptionCertificateFingerprintHeaderName ("x-encryption-certificate-fingerprint" )
192+ .withEncryptionKeyFingerprintHeaderName ("x-encryption-key-fingerprint" )
193+ .build ();
194+ HttpRequest request = mock (HttpRequest .class );
195+ HttpHeaders httpHeaders = new HttpHeaders ();
196+ when (request .getContent ()).thenReturn (new ByteArrayContent (JSON_TYPE , "{\" foo\" :\" bar\" }" .getBytes ()));
197+ when (request .getHeaders ()).thenReturn (httpHeaders );
198+
199+ // WHEN
200+ HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor (config );
201+ instanceUnderTest .intercept (request );
202+
203+ // THEN
204+ ArgumentCaptor <HttpContent > contentCaptor = ArgumentCaptor .forClass (HttpContent .class );
205+ verify (request ).setContent (contentCaptor .capture ());
206+ ByteArrayOutputStream encryptedPayloadStream = new ByteArrayOutputStream ();
207+ contentCaptor .getValue ().writeTo (encryptedPayloadStream );
208+ String encryptedPayload = encryptedPayloadStream .toString (StandardCharsets .UTF_8 .name ());
209+ Assert .assertFalse (encryptedPayload .contains ("foo" ));
210+ Assert .assertTrue (encryptedPayload .contains ("encryptedFoo" ));
211+ assertEquals (encryptedPayload .length (), httpHeaders .getContentLength ().intValue ());
212+ assertNotNull (httpHeaders .get ("x-iv" ));
213+ assertNotNull (httpHeaders .get ("x-encrypted-key" ));
214+ assertEquals ("SHA256" , httpHeaders .get ("x-oaep-padding-digest-algorithm" ));
215+ assertEquals ("80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279" , httpHeaders .get ("x-encryption-certificate-fingerprint" ));
216+ assertEquals ("761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79" , httpHeaders .get ("x-encryption-key-fingerprint" ));
217+ }
218+
219+ @ Test
220+ public void testInterceptResponse_ShouldDecryptResponsePayloadAndRemoveEncryptionHttpHeaders_WhenRequestedInConfig () throws Exception {
221+
222+ // GIVEN
223+ String encryptedPayload = "{" +
224+ " \" encryptedData\" : {" +
225+ " \" encryptedValue\" : \" 21d754bdb4567d35d58720c9f8364075\" " +
226+ " }" +
227+ "}" ;
228+ FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder ()
229+ .withDecryptionPath ("$.encryptedData" , "$.data" )
230+ .withIvHeaderName ("x-iv" )
231+ .withEncryptedKeyHeaderName ("x-encrypted-key" )
232+ .withOaepPaddingDigestAlgorithmHeaderName ("x-oaep-padding-digest-algorithm" )
233+ .withEncryptionCertificateFingerprintHeaderName ("x-encryption-certificate-fingerprint" )
234+ .withEncryptionKeyFingerprintHeaderName ("x-encryption-key-fingerprint" )
235+ .build ();
236+
237+ HttpResponse response = mock (HttpResponse .class );
238+ HttpHeaders httpHeaders = new HttpHeaders ();
239+ httpHeaders .set ("x-iv" , "a32059c51607d0d02e823faecda5fb15" );
240+ httpHeaders .set ("x-encrypted-key" , "a31cfe7a7981b72428c013270619554c1d645c04b9d51c7eaf996f55749ef62fd7c7f8d334f95913be41ae38c46d192670fd1acb84ebb85a00cd997f1a9a3f782229c7bf5f0fdf49fe404452d7ed4fd41fbb95b787d25893fbf3d2c75673cecc8799bbe3dd7eb4fe6d3f744b377572cdf8aba1617194e10475b6cd6a8dd4fb8264f8f51534d8f7ac7c10b4ce9c44d15066724b03a0ab0edd512f9e6521fdb5841cd6964e457d6b4a0e45ba4aac4e77d6bbe383d6147e751fa88bc26278bb9690f9ee84b17123b887be2dcef0873f4f9f2c895d90e23456fafb01b99885e31f01a3188f0ad47edf22999cc1d0ddaf49e1407375117b5d66f1f185f2b57078d255" );
241+ httpHeaders .set ("x-oaep-padding-digest-algorithm" , "SHA256" );
242+ httpHeaders .set ("x-encryption-key-fingerprint" , "761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79" );
243+ httpHeaders .set ("x-encryption-certificate-fingerprint" , "80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279" );
244+ httpHeaders .setContentLength (100l );
245+ when (response .parseAsString ()).thenReturn (encryptedPayload );
246+ when (response .getHeaders ()).thenReturn (httpHeaders );
247+
248+ // WHEN
249+ HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor (config );
250+ instanceUnderTest .interceptResponse (response );
251+
252+ // THEN
253+ Field contentField = response .getClass ().getDeclaredField ("content" );
254+ contentField .setAccessible (true );
255+ InputStream payloadInputStream = (InputStream ) contentField .get (response );
256+ String payload = IOUtils .toString (payloadInputStream , StandardCharsets .UTF_8 );
257+ assertPayloadEquals ("{\" data\" :\" string\" }" , payload );
258+ assertEquals (payload .length (), httpHeaders .getContentLength ().intValue ());
259+ assertNull (response .getHeaders ().get ("x-iv" ));
260+ assertNull (response .getHeaders ().get ("x-encrypted-key" ));
261+ assertNull (response .getHeaders ().get ("x-oaep-padding-digest-algorithm" ));
262+ assertNull (response .getHeaders ().get ("x-encryption-key-fingerprint" ));
263+ assertNull (response .getHeaders ().get ("x-encryption-certificate-fingerprint" ));
264+ }
179265}
0 commit comments