Expose response cookies from ExchangeResult

This commit is contained in:
Rossen Stoyanchev 2017-02-20 13:30:17 -05:00
parent 1ddf8ec625
commit d59dc970a0
4 changed files with 41 additions and 17 deletions

View File

@ -261,7 +261,7 @@ class DefaultWebTestClient implements WebTestClient {
}
/**
* ExchangeResult that contains the live {@link ClientResponse}.
* The {@code ExchangeResult} with live, undecoded {@link ClientResponse}.
*/
private class UndecodedExchangeResult extends ExchangeResult {

View File

@ -16,13 +16,14 @@
package org.springframework.test.web.reactive.server;
/**
* {@code ExchangeResult} variant with the response body fully extracted to a
* representation of type {@code <T>}.
* {@code ExchangeResult} sub-class that exposes the response body fully
* extracted to a representation of type {@code <T>}.
*
* @param <T> the response body type
*
* @author Rossen Stoyanchev
* @since 5.0
* @see FluxExchangeResult
*/
public class EntityExchangeResult<T> extends ExchangeResult {

View File

@ -22,22 +22,27 @@ import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ClientResponse;
/**
* Container for the result of an exchange through the {@link WebTestClient}.
* Simple container for request and response details from an exchange performed
* through the {@link WebTestClient}.
*
* <p>This type only exposes the status and response headers that are available
* when the {@link ClientResponse} is first received and before the response
* body has been consumed.
* <p>An {@code ExchangeResult} only exposes the status and the headers from
* the response which is all that's available when a {@link ClientResponse} is
* first created.
*
* <p>The sub-classes {@link EntityExchangeResult} and {@link FluxExchangeResult}
* expose further information about the response body and are returned only
* after the test client has been used to decode and consume the response.
* <p>Sub-types {@link EntityExchangeResult} and {@link FluxExchangeResult}
* further expose the response body either as a fully extracted representation
* or as a {@code Flux} of representations to be consumed.
*
* @author Rossen Stoyanchev
* @since 5.0
* @see EntityExchangeResult
* @see FluxExchangeResult
*/
public class ExchangeResult {
@ -51,21 +56,31 @@ public class ExchangeResult {
private final HttpHeaders responseHeaders;
private final MultiValueMap<String, ResponseCookie> responseCookies;
ExchangeResult(ClientHttpRequest request, ClientResponse response) {
/**
* Constructor used when a {@code ClientResponse} is first created.
*/
protected ExchangeResult(ClientHttpRequest request, ClientResponse response) {
this.method = request.getMethod();
this.url = request.getURI();
this.requestHeaders = request.getHeaders();
this.status = response.statusCode();
this.responseHeaders = response.headers().asHttpHeaders();
this.responseCookies = response.cookies();
}
ExchangeResult(ExchangeResult result) {
this.method = result.getMethod();
this.url = result.getUrl();
this.requestHeaders = result.getRequestHeaders();
this.status = result.getStatus();
this.responseHeaders = result.getResponseHeaders();
/**
* Copy constructor used when the body is decoded or consumed.
*/
protected ExchangeResult(ExchangeResult other) {
this.method = other.getMethod();
this.url = other.getUrl();
this.requestHeaders = other.getRequestHeaders();
this.status = other.getStatus();
this.responseHeaders = other.getResponseHeaders();
this.responseCookies = other.getResponseCookies();
}
@ -104,6 +119,13 @@ public class ExchangeResult {
return this.responseHeaders;
}
/**
* Return response cookies received from the server.
*/
public MultiValueMap<String, ResponseCookie> getResponseCookies() {
return this.responseCookies;
}
/**
* Execute the given Runnable in the context of "this" instance and decorate

View File

@ -26,6 +26,7 @@ import org.springframework.core.ResolvableType;
*
* @author Rossen Stoyanchev
* @since 5.0
* @see EntityExchangeResult
*/
public class FluxExchangeResult<T> extends ExchangeResult {