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.
@@ -33,12 +33,13 @@ import org.springframework.util.StringUtils;
* {@link ExchangeResult}.
*
* @author Andy Wilkinson
* @author Clyde Stubbs
*/
class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult> {
@Override
public OperationResponse convert(ExchangeResult result) {
Collection<ResponseCookie> cookies = extractCookies(result, result.getResponseHeaders());
Collection<ResponseCookie> cookies = extractCookies(result);
return new OperationResponseFactory().create(result.getStatus().value(), extractHeaders(result),
result.getResponseBodyContent(), cookies);
}
@@ -74,14 +75,9 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
return header.toString();
}
private Collection<ResponseCookie> extractCookies(ExchangeResult result, HttpHeaders headers) {
List<String> cookieHeaders = headers.get(HttpHeaders.COOKIE);
if (cookieHeaders == null) {
return result.getResponseCookies().values().stream().flatMap(List::stream).map(this::createResponseCookie)
.collect(Collectors.toSet());
}
headers.remove(HttpHeaders.COOKIE);
return cookieHeaders.stream().map(this::createResponseCookie).collect(Collectors.toList());
private Collection<ResponseCookie> extractCookies(ExchangeResult result) {
return result.getResponseCookies().values().stream().flatMap(List::stream).map(this::createResponseCookie)
.collect(Collectors.toSet());
}
private void appendIfAvailable(StringBuilder header, String value) {
@@ -101,9 +97,4 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
}
}
private ResponseCookie createResponseCookie(String header) {
String[] components = header.split("=");
return new ResponseCookie(components[0], components[1]);
}
}

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.
@@ -23,8 +23,8 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.ResponseCookie;
import org.springframework.test.web.reactive.server.ExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RouterFunctions;
@@ -62,7 +62,8 @@ public class WebTestClientResponseConverterTests {
ExchangeResult result = WebTestClient
.bindToRouterFunction(RouterFunctions.route(GET("/foo"),
(req) -> ServerResponse.ok()
.cookie(ResponseCookie.from("name", "value").domain("localhost").httpOnly(true).build())
.cookie(org.springframework.http.ResponseCookie.from("name", "value")
.domain("localhost").httpOnly(true).build())
.build()))
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
.returnResult();
@@ -70,6 +71,9 @@ public class WebTestClientResponseConverterTests {
assertThat(response.getHeaders()).hasSize(1);
assertThat(response.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
assertThat(response.getCookies()).hasSize(1);
assertThat(response.getCookies()).first().extracting(ResponseCookie::getName).isEqualTo("name");
assertThat(response.getCookies()).first().extracting(ResponseCookie::getValue).isEqualTo("value");
}
}