|
6 | 6 | import static org.hamcrest.MatcherAssert.assertThat; |
7 | 7 |
|
8 | 8 | import java.io.InputStream; |
| 9 | +import java.util.List; |
9 | 10 | import java.util.Map; |
10 | 11 | import java.util.Optional; |
11 | 12 | import java.util.function.Supplier; |
12 | 13 | import java.io.ByteArrayInputStream; |
13 | 14 |
|
| 15 | +import org.hamcrest.Matchers; |
14 | 16 | import org.junit.jupiter.api.BeforeEach; |
15 | 17 | import org.junit.jupiter.api.DisplayName; |
16 | 18 | import org.junit.jupiter.api.Test; |
|
20 | 22 | import com._4point.aem.docservices.rest_services.client.RestClient; |
21 | 23 | import com._4point.aem.docservices.rest_services.client.RestClient.ContentType; |
22 | 24 | import com._4point.aem.docservices.rest_services.client.RestClient.GetRequest; |
| 25 | +import com._4point.aem.docservices.rest_services.client.RestClient.HttpHeaders; |
| 26 | +import com._4point.aem.docservices.rest_services.client.RestClient.Cookies; |
| 27 | +import com._4point.aem.docservices.rest_services.client.RestClient.HttpHeader; |
23 | 28 | import com._4point.aem.docservices.rest_services.client.RestClient.MultipartPayload; |
24 | 29 | import com._4point.aem.docservices.rest_services.client.RestClient.Response; |
25 | 30 | import com._4point.aem.docservices.rest_services.client.RestClient.RestClientException; |
@@ -141,6 +146,107 @@ void testPostToServer_DocumentResponseWithHeader() throws Exception { |
141 | 146 | ); |
142 | 147 | } |
143 | 148 |
|
| 149 | + @DisplayName("PostToServer with 1 part and return 1 header with 3 values in the response") |
| 150 | + @Test |
| 151 | + void testPostToServer_DocumentResponseWithMultipleHeaders() throws Exception { |
| 152 | + // Given |
| 153 | + stubFor(post(ENDPOINT).willReturn(okForContentType(ContentType.APPLICATION_PDF.contentType(), MOCK_PDF_BYTES) |
| 154 | + .withHeader(SAMPLE_HEADER.toUpperCase(), SAMPLE_HEADER_VALUE + "_3") |
| 155 | + .withHeader(SAMPLE_HEADER, SAMPLE_HEADER_VALUE + "_2") |
| 156 | + .withHeader(SAMPLE_HEADER, SAMPLE_HEADER_VALUE) |
| 157 | + )); |
| 158 | + |
| 159 | + // When |
| 160 | + Response response = postToServerBuilder().performPostToServer(FIELD1_NAME, FIELD1_DATA).orElseThrow(); |
| 161 | + |
| 162 | + // Then |
| 163 | + assertEquals(ContentType.APPLICATION_PDF, response.contentType()); |
| 164 | + assertEquals(MOCK_PDF_BYTES, new String(response.data().readAllBytes())); |
| 165 | + assertThat(response.retrieveHeader(SAMPLE_HEADER).orElseThrow(), anyOf(Matchers.equalTo(SAMPLE_HEADER_VALUE), Matchers.equalTo(SAMPLE_HEADER_VALUE + "_3"))); // Should retrieve the first header value or 3rd, not the 2nd |
| 166 | + assertThat(response.retrieveHeader(SAMPLE_HEADER.toUpperCase()).orElseThrow(), anyOf(Matchers.equalTo(SAMPLE_HEADER_VALUE), Matchers.equalTo(SAMPLE_HEADER_VALUE + "_3"))); // Uppercase version should also retrieve the first header value or 3rd, not the 2nd |
| 167 | + |
| 168 | + HttpHeaders headers = response.headers(); |
| 169 | + List<HttpHeader> headersList = headers.getHeaders(SAMPLE_HEADER); |
| 170 | + switch (headers.caseHandling()) { |
| 171 | + case DOWNSHIFTS -> assertThat(headersList, containsInAnyOrder(equalTo(new HttpHeader(SAMPLE_HEADER.toLowerCase(), SAMPLE_HEADER_VALUE)), |
| 172 | + equalTo(new HttpHeader(SAMPLE_HEADER.toLowerCase(), SAMPLE_HEADER_VALUE + "_2")), |
| 173 | + equalTo(new HttpHeader(SAMPLE_HEADER.toLowerCase(), SAMPLE_HEADER_VALUE + "_3")) |
| 174 | + ) |
| 175 | + ); |
| 176 | + case UPSHIFTS -> assertThat(headersList, containsInAnyOrder(equalTo(new HttpHeader(SAMPLE_HEADER.toUpperCase(), SAMPLE_HEADER_VALUE)), |
| 177 | + equalTo(new HttpHeader(SAMPLE_HEADER.toUpperCase(), SAMPLE_HEADER_VALUE + "_2")), |
| 178 | + equalTo(new HttpHeader(SAMPLE_HEADER.toUpperCase(), SAMPLE_HEADER_VALUE + "_3")) |
| 179 | + ) |
| 180 | + ); |
| 181 | + case PRESERVES_CASE -> assertThat(headersList, containsInAnyOrder(equalTo(new HttpHeader(SAMPLE_HEADER, SAMPLE_HEADER_VALUE)), |
| 182 | + equalTo(new HttpHeader(SAMPLE_HEADER, SAMPLE_HEADER_VALUE + "_2")), |
| 183 | + equalTo(new HttpHeader(SAMPLE_HEADER.toUpperCase(), SAMPLE_HEADER_VALUE + "_3")) |
| 184 | + ) |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + verify(postRequestedFor(urlEqualTo(ENDPOINT)) |
| 189 | + .withAllRequestBodyParts(aMultipart(FIELD1_NAME).withBody(equalTo(FIELD1_DATA))) |
| 190 | + .withHeader(RestClient.CORRELATION_ID_HTTP_HDR, equalTo(CORRELATION_ID_TEXT)) |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + @DisplayName("PostToServer with 1 part and return no cookies in the response") |
| 195 | + @Test |
| 196 | + void testPostToServer_DocumentResponseWithNoCookies() throws Exception { |
| 197 | + // Given |
| 198 | + final String COOKIES_KEY = "Set-Cookie"; |
| 199 | + stubFor(post(ENDPOINT).willReturn(okForContentType(ContentType.APPLICATION_PDF.contentType(), MOCK_PDF_BYTES) |
| 200 | + )); |
| 201 | + |
| 202 | + // When |
| 203 | + Response response = postToServerBuilder().performPostToServer(FIELD1_NAME, FIELD1_DATA).orElseThrow(); |
| 204 | + |
| 205 | + // 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 | + |
| 211 | + Cookies cookies = response.getCookies(); |
| 212 | + assertFalse(cookies.isPresent()); |
| 213 | + assertTrue(cookies.isEmpty()); |
| 214 | + |
| 215 | + verify(postRequestedFor(urlEqualTo(ENDPOINT)) |
| 216 | + .withAllRequestBodyParts(aMultipart(FIELD1_NAME).withBody(equalTo(FIELD1_DATA))) |
| 217 | + .withHeader(RestClient.CORRELATION_ID_HTTP_HDR, equalTo(CORRELATION_ID_TEXT)) |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + @DisplayName("PostToServer with 1 part and return 1 set-cookie in the response") |
| 222 | + @Test |
| 223 | + void testPostToServer_DocumentResponseWithCookie() throws Exception { |
| 224 | + // Given |
| 225 | + final String COOKIES_KEY = "Set-Cookie"; |
| 226 | + final String COOKIES_VALUE = "cookie1=value1; cookie2=value2; HttpOnly"; |
| 227 | + stubFor(post(ENDPOINT).willReturn(okForContentType(ContentType.APPLICATION_PDF.contentType(), MOCK_PDF_BYTES) |
| 228 | + .withHeader(COOKIES_KEY, COOKIES_VALUE) |
| 229 | + )); |
| 230 | + |
| 231 | + // When |
| 232 | + Response response = postToServerBuilder().performPostToServer(FIELD1_NAME, FIELD1_DATA).orElseThrow(); |
| 233 | + |
| 234 | + // 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 | + |
| 240 | + Cookies cookies = response.getCookies(); |
| 241 | + assertTrue(cookies.isPresent()); |
| 242 | + assertFalse(cookies.isEmpty()); |
| 243 | + |
| 244 | + verify(postRequestedFor(urlEqualTo(ENDPOINT)) |
| 245 | + .withAllRequestBodyParts(aMultipart(FIELD1_NAME).withBody(equalTo(FIELD1_DATA))) |
| 246 | + .withHeader(RestClient.CORRELATION_ID_HTTP_HDR, equalTo(CORRELATION_ID_TEXT)) |
| 247 | + ); |
| 248 | + } |
| 249 | + |
144 | 250 | @DisplayName("PostToServer with 1 header and 1 part using byte array data") |
145 | 251 | @Test |
146 | 252 | void testPostToServer_DocumentResponseFromByteArray() throws Exception { |
|
0 commit comments