@@ -195,19 +195,13 @@ void testPostToServer_DocumentResponseWithMultipleHeaders() throws Exception {
195195 @ Test
196196 void testPostToServer_DocumentResponseWithNoCookies () throws Exception {
197197 // Given
198- final String COOKIES_KEY = "Set-Cookie" ;
199198 stubFor (post (ENDPOINT ).willReturn (okForContentType (ContentType .APPLICATION_PDF .contentType (), MOCK_PDF_BYTES )
200199 ));
201200
202201 // When
203202 Response response = postToServerBuilder ().performPostToServer (FIELD1_NAME , FIELD1_DATA ).orElseThrow ();
204203
205204 // Then
206- assertEquals (ContentType .APPLICATION_PDF , response .contentType ());
207- assertEquals (MOCK_PDF_BYTES , new String (response .data ().readAllBytes ()));
208- assertTrue (response .retrieveHeader (COOKIES_KEY ).isEmpty ()); // Should not be present
209- assertTrue (response .retrieveHeader (COOKIES_KEY .toUpperCase ()).isEmpty ()); // Should not be present
210-
211205 Cookies cookies = response .getCookies ();
212206 assertFalse (cookies .isPresent ());
213207 assertTrue (cookies .isEmpty ());
@@ -232,11 +226,6 @@ void testPostToServer_DocumentResponseWithCookie() throws Exception {
232226 Response response = postToServerBuilder ().performPostToServer (FIELD1_NAME , FIELD1_DATA ).orElseThrow ();
233227
234228 // Then
235- assertEquals (ContentType .APPLICATION_PDF , response .contentType ());
236- assertEquals (MOCK_PDF_BYTES , new String (response .data ().readAllBytes ()));
237- assertEquals (COOKIES_VALUE , response .retrieveHeader (COOKIES_KEY ).orElseThrow ()); // Should retrieve the first header value, not the 2nd or 3rd one
238- assertEquals (COOKIES_VALUE , response .retrieveHeader (COOKIES_KEY .toUpperCase ()).orElseThrow ()); // Should retrieve the first header value, not the 3rd one
239-
240229 Cookies cookies = response .getCookies ();
241230 assertTrue (cookies .isPresent ());
242231 assertFalse (cookies .isEmpty ());
@@ -296,6 +285,57 @@ void testPostToServer_DocumentResponseFromInputStream() throws Exception {
296285 );
297286 }
298287
288+ @ DisplayName ("PostToServer with 1 part and return 2 set-cookies in the response, send PostToServer with cookies from first response" )
289+ @ Test
290+ void testPostToServer_DocumentResponseWithCookie_PostWithCookies () throws Exception {
291+ // Given
292+ final String COOKIES_KEY = "Set-Cookie" ;
293+ final String COOKIE1_VALUE = "cookie1=value1; HttpOnly" ;
294+ final String COOKIE2_VALUE = "cookie2=value2; HttpOnly" ;
295+ final String POST_HEADER_KEY = "post" ;
296+ final String FIRST_POST_VALUE = "firstPost" ;
297+ final String SECOND_POST_VALUE = "secondPost" ;
298+ stubFor (post (ENDPOINT )
299+ .withHeader (POST_HEADER_KEY , equalTo (FIRST_POST_VALUE ))
300+ .willReturn (okForContentType (ContentType .APPLICATION_PDF .contentType (), MOCK_PDF_BYTES )
301+ .withHeader (COOKIES_KEY , COOKIE1_VALUE )
302+ .withHeader (COOKIES_KEY , COOKIE2_VALUE )
303+ ));
304+ stubFor (post (ENDPOINT )
305+ .withHeader (POST_HEADER_KEY , equalTo (SECOND_POST_VALUE ))
306+ .willReturn (okForContentType (ContentType .APPLICATION_PDF .contentType (), MOCK_PDF_BYTES )));
307+
308+ // When
309+ Response response1 = postToServerBuilder ()
310+ .headers (POST_HEADER_KEY , FIRST_POST_VALUE )
311+ .performPostToServer (FIELD1_NAME , FIELD1_DATA ).orElseThrow ();
312+
313+ Response response2 = postToServerBuilder ()
314+ .headers (POST_HEADER_KEY , SECOND_POST_VALUE )
315+ .cookies (response1 .getCookies ())
316+ .performPostToServer (FIELD1_NAME , FIELD1_DATA ).orElseThrow ();
317+
318+ // Then
319+ assertEquals (ContentType .APPLICATION_PDF , response1 .contentType ());
320+ assertEquals (MOCK_PDF_BYTES , new String (response1 .data ().readAllBytes ()));
321+ assertEquals (ContentType .APPLICATION_PDF , response2 .contentType ());
322+ assertEquals (MOCK_PDF_BYTES , new String (response2 .data ().readAllBytes ()));
323+
324+ verify (postRequestedFor (urlEqualTo (ENDPOINT ))
325+ .withAllRequestBodyParts (aMultipart (FIELD1_NAME ).withBody (equalTo (FIELD1_DATA )))
326+ .withHeader (RestClient .CORRELATION_ID_HTTP_HDR , equalTo (CORRELATION_ID_TEXT ))
327+ .withHeader (POST_HEADER_KEY , equalTo (FIRST_POST_VALUE ))
328+ );
329+
330+ verify (postRequestedFor (urlEqualTo (ENDPOINT ))
331+ .withAllRequestBodyParts (aMultipart (FIELD1_NAME ).withBody (equalTo (FIELD1_DATA )))
332+ .withHeader (RestClient .CORRELATION_ID_HTTP_HDR , equalTo (CORRELATION_ID_TEXT ))
333+ .withHeader (POST_HEADER_KEY , equalTo (SECOND_POST_VALUE ))
334+ .withCookie ("cookie1" , equalTo ("value1" ))
335+ .withCookie ("cookie2" , equalTo ("value2" ))
336+ );
337+ }
338+
299339 @ DisplayName ("When AEM returns 500 Internal Server error with no body, postToServer should throw RestClientException." )
300340 @ Test
301341 void testPostToServer_AemReturns500NoBody () throws Exception {
@@ -443,6 +483,7 @@ private PostToServerBuilder postToServerBuilder() {
443483 private class PostToServerBuilder {
444484 private String [] queryParams = new String [0 ];
445485 private String [] headers = new String [0 ];
486+ private Cookies cookies = null ;;
446487
447488 private PostToServerBuilder queryParams (String ...strings ) {
448489 if (strings .length % 2 != 0 ) {
@@ -459,7 +500,12 @@ private PostToServerBuilder headers(String...strings) {
459500 headers = strings ;
460501 return this ;
461502 }
462-
503+
504+ private PostToServerBuilder cookies (Cookies cookies ) {
505+ this .cookies = cookies ;
506+ return this ;
507+ }
508+
463509 private Optional <Response > performPostToServer (String ...strings ) throws RestClientException , Exception {
464510 if (strings .length % 2 != 0 ) {
465511 throw new IllegalArgumentException ("Odd number of Strings passed in, must be even. (" + strings .length + ")." );
@@ -499,7 +545,7 @@ private Optional<Response> performPostToServer(String fieldName, InputStream dat
499545 }
500546 }
501547
502- MultipartPayload .Builder addHeadersAndQueryParams (MultipartPayload .Builder builder ) {
548+ private MultipartPayload .Builder addHeadersAndQueryParams (MultipartPayload .Builder builder ) {
503549 for (int i = 0 ; i < queryParams .length ; i +=2 ) { // Add query Params
504550 builder .queryParam (queryParams [i ], queryParams [i +1 ]);
505551 }
@@ -508,6 +554,10 @@ MultipartPayload.Builder addHeadersAndQueryParams(MultipartPayload.Builder build
508554 builder .addHeader (headers [i ], headers [i +1 ]);
509555 }
510556
557+ if (cookies != null ) {
558+ builder .addCookies (cookies );
559+ }
560+
511561 return builder ;
512562 }
513563 }
@@ -578,6 +628,105 @@ void testGetFromServer_DocumentResponseWithHeader() throws Exception {
578628 );
579629 }
580630
631+ @ DisplayName ("GetFromServer with 2 query parameters and return no cookies in response" )
632+ @ Test
633+ void testGetFromServer_DocumentResponseNoCookies () throws Exception {
634+ // Given
635+ stubFor (get (urlPathEqualTo (ENDPOINT ))
636+ .withQueryParams (Map .of (FIELD1_NAME , equalTo (FIELD1_DATA ), FIELD2_NAME , equalTo (FIELD2_DATA )))
637+ .willReturn (okForContentType (ContentType .TEXT_HTML .contentType (), MOCK_PDF_BYTES )));
638+
639+ // When
640+ Response response = performGetFromServer (FIELD1_NAME , FIELD1_DATA , FIELD2_NAME , FIELD2_DATA ).orElseThrow ();
641+
642+ // Then
643+ Cookies cookies = response .getCookies ();
644+ assertFalse (cookies .isPresent ());
645+ assertTrue (cookies .isEmpty ());
646+
647+ verify (getRequestedFor (urlPathEqualTo (ENDPOINT ))
648+ .withQueryParam (FIELD1_NAME , equalTo (FIELD1_DATA ))
649+ .withQueryParam (FIELD2_NAME , equalTo (FIELD2_DATA ))
650+ );
651+ }
652+
653+ @ DisplayName ("GetFromServer with no query parameters and return cookies in response" )
654+ @ Test
655+ void testGetFromServer_DocumentResponseWithCookies () throws Exception {
656+ // Given
657+ final String COOKIES_KEY = "Set-Cookie" ;
658+ final String COOKIES_VALUE = "cookie1=value1; cookie2=value2; HttpOnly" ;
659+ stubFor (get (ENDPOINT ).willReturn (okForContentType (ContentType .TEXT_HTML .contentType (), MOCK_PDF_BYTES )
660+ .withHeader (COOKIES_KEY , COOKIES_VALUE )
661+ ));
662+
663+ // When
664+ Response response = performGetFromServer ().orElseThrow ();
665+
666+ // Then
667+ Cookies cookies = response .getCookies ();
668+ assertTrue (cookies .isPresent ());
669+ assertFalse (cookies .isEmpty ());
670+
671+
672+ verify (getRequestedFor (urlEqualTo (ENDPOINT ))
673+ .withoutQueryParam (FIELD1_NAME )
674+ );
675+ }
676+
677+ @ DisplayName ("GetFromServer with no query parameters and return cookies in response" )
678+ @ Test
679+ void testGetFromServer_DocumentResponseWithCookies_GetWithCookies () throws Exception {
680+ // Given
681+ final String COOKIES_KEY = "Set-Cookie" ;
682+ final String COOKIE1_VALUE = "cookie1=value1; HttpOnly" ;
683+ final String COOKIE2_VALUE = "cookie2=value2; HttpOnly" ;
684+ final String GET_HEADER_KEY = "get" ;
685+ final String FIRST_GET_VALUE = "firstGet" ;
686+ final String SECOND_GET_VALUE = "secondGet" ;
687+ stubFor (get (ENDPOINT )
688+ .withHeader (GET_HEADER_KEY , equalTo (FIRST_GET_VALUE ))
689+ .willReturn (okForContentType (ContentType .TEXT_HTML .contentType (), MOCK_PDF_BYTES )
690+ .withHeader (COOKIES_KEY , COOKIE1_VALUE )
691+ .withHeader (COOKIES_KEY , COOKIE2_VALUE )
692+ ));
693+ stubFor (get (ENDPOINT )
694+ .withHeader (GET_HEADER_KEY , equalTo (SECOND_GET_VALUE ))
695+ .willReturn (okForContentType (ContentType .TEXT_HTML .contentType (), MOCK_PDF_BYTES )));
696+
697+ // When
698+ Response response1 = underTest .getRequestBuilder ()
699+ .addHeader (GET_HEADER_KEY , FIRST_GET_VALUE )
700+ .build ()
701+ .getFromServer (ContentType .TEXT_HTML )
702+ .orElseThrow ();
703+
704+ Response response2 = underTest .getRequestBuilder ()
705+ .addHeader (GET_HEADER_KEY , SECOND_GET_VALUE )
706+ .addCookies (response1 .getCookies ())
707+ .build ()
708+ .getFromServer (ContentType .TEXT_HTML )
709+ .orElseThrow ();
710+
711+ // Then
712+ assertEquals (ContentType .TEXT_HTML , response1 .contentType ());
713+ assertEquals (MOCK_PDF_BYTES , new String (response1 .data ().readAllBytes ()));
714+ assertEquals (ContentType .TEXT_HTML , response2 .contentType ());
715+ assertEquals (MOCK_PDF_BYTES , new String (response2 .data ().readAllBytes ()));
716+
717+
718+ verify (getRequestedFor (urlEqualTo (ENDPOINT ))
719+ .withoutQueryParam (FIELD1_NAME )
720+ .withHeader (GET_HEADER_KEY , equalTo (FIRST_GET_VALUE ))
721+ );
722+ verify (getRequestedFor (urlEqualTo (ENDPOINT ))
723+ .withoutQueryParam (FIELD1_NAME )
724+ .withHeader (GET_HEADER_KEY , equalTo (SECOND_GET_VALUE ))
725+ .withCookie ("cookie1" , equalTo ("value1" ))
726+ .withCookie ("cookie2" , equalTo ("value2" ))
727+ );
728+ }
729+
581730 @ DisplayName ("GetFromServer with additional path parameter" )
582731 @ ParameterizedTest
583732 @ ValueSource (strings = {"foo" , "/foo" })
0 commit comments