diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java index 476536cc54..d93001ad4b 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java @@ -26,7 +26,9 @@ import reactor.core.publisher.Mono; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseCookie; import org.springframework.http.client.reactive.ClientHttpResponse; +import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; /** @@ -51,6 +53,11 @@ public interface ClientResponse { */ Headers headers(); + /** + * Return cookies of this response. + */ + MultiValueMap cookies(); + /** * Extract the body with the given {@code BodyExtractor}. Unlike {@link #bodyToMono(Class)} and * {@link #bodyToFlux(Class)}; this method does not check for a 4xx or 5xx status code before diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java index 44ab32b5c8..073aa7338d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java @@ -32,8 +32,10 @@ import reactor.core.publisher.Mono; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseCookie; import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.codec.HttpMessageReader; +import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractors; @@ -68,6 +70,11 @@ class DefaultClientResponse implements ClientResponse { return this.headers; } + @Override + public MultiValueMap cookies() { + return this.response.getCookies(); + } + @Override public T body(BodyExtractor extractor) { return extractor.extract(this.response, new BodyExtractor.Context() { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java index aee80b6283..1fb56ddcf3 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java @@ -39,11 +39,15 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRange; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseCookie; import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.HttpMessageReader; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.web.reactive.function.BodyExtractors.toMono; @@ -96,6 +100,18 @@ public class DefaultClientResponseTests { assertEquals(httpHeaders, headers.asHttpHeaders()); } + @Test + public void cookies() throws Exception { + ResponseCookie cookie = ResponseCookie.from("foo", "bar").build(); + MultiValueMap cookies = new LinkedMultiValueMap<>(); + cookies.add("foo", cookie); + + when(mockResponse.getCookies()).thenReturn(cookies); + + assertSame(cookies, defaultClientResponse.cookies()); + } + + @Test public void body() throws Exception { DefaultDataBufferFactory factory = new DefaultDataBufferFactory();