Polish "Add support for documenting request and response cookies"

See gh-592
This commit is contained in:
Andy Wilkinson
2022-03-24 17:32:42 +00:00
parent f72a9f1067
commit d5522f5335
26 changed files with 480 additions and 309 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@ import org.springframework.web.multipart.MultipartFile;
* {@link MockHttpServletRequest}.
*
* @author Andy Wilkinson
* @author Clyde Stubbs
*/
class MockMvcRequestConverter implements RequestConverter<MockHttpServletRequest> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,20 +43,19 @@ class MockMvcResponseConverter implements ResponseConverter<MockHttpServletRespo
@Override
public OperationResponse convert(MockHttpServletResponse mockResponse) {
HttpHeaders headers = extractHeaders(mockResponse);
Collection<ResponseCookie> cookies = extractCookies(mockResponse, headers);
Collection<ResponseCookie> cookies = extractCookies(mockResponse);
return new OperationResponseFactory().create(mockResponse.getStatus(), headers,
mockResponse.getContentAsByteArray(), cookies);
}
private Collection<ResponseCookie> extractCookies(MockHttpServletResponse mockRequest, HttpHeaders headers) {
if (mockRequest.getCookies() == null || mockRequest.getCookies().length == 0) {
private Collection<ResponseCookie> extractCookies(MockHttpServletResponse mockResponse) {
if (mockResponse.getCookies() == null || mockResponse.getCookies().length == 0) {
return Collections.emptyList();
}
List<ResponseCookie> cookies = new ArrayList<>();
for (Cookie servletCookie : mockRequest.getCookies()) {
cookies.add(new ResponseCookie(servletCookie.getName(), servletCookie.getValue()));
for (Cookie cookie : mockResponse.getCookies()) {
cookies.add(new ResponseCookie(cookie.getName(), cookie.getValue()));
}
headers.remove(HttpHeaders.COOKIE);
return cookies;
}

View File

@@ -26,6 +26,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.ResponseCookie;
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,6 +59,9 @@ public class MockMvcResponseConverterTests {
assertThat(operationResponse.getHeaders()).hasSize(1);
assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
assertThat(operationResponse.getCookies()).hasSize(1);
assertThat(operationResponse.getCookies()).first().extracting(ResponseCookie::getName).isEqualTo("name");
assertThat(operationResponse.getCookies()).first().extracting(ResponseCookie::getValue).isEqualTo("value");
}
@Test