Polish "Add support for documenting request and response cookies"
See gh-592
This commit is contained in:
@@ -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> {
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user