Allow to set cookies in ServerResponse

Issue: SPR-16121
This commit is contained in:
Arjen Poutsma
2017-11-07 14:05:27 +01:00
parent 840ef46a9f
commit db00669197
10 changed files with 238 additions and 23 deletions

View File

@@ -39,17 +39,19 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.result.view.ViewResolver;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
@@ -176,6 +178,18 @@ public class DefaultEntityResponseBuilderTests {
.verify();
}
@Test
public void cookies() throws Exception {
MultiValueMap<String, ResponseCookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", ResponseCookie.from("name", "value").build());
Mono<EntityResponse<String>> result =
EntityResponse.fromObject("foo").cookies(cookies -> cookies.addAll(newCookies)).build();
StepVerifier.create(result)
.expectNextMatches(response -> newCookies.equals(response.cookies()))
.expectComplete()
.verify();
}
@Test
public void bodyInserter() throws Exception {
String body = "foo";

View File

@@ -28,8 +28,11 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.reactive.result.view.ViewResolver;
@@ -103,6 +106,19 @@ public class DefaultRenderingResponseTests {
.verify();
}
@Test
public void cookies() throws Exception {
MultiValueMap<String, ResponseCookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", ResponseCookie.from("name", "value").build());
Mono<RenderingResponse> result =
RenderingResponse.create("foo").cookies(cookies -> cookies.addAll(newCookies)).build();
StepVerifier.create(result)
.expectNextMatches(response -> newCookies.equals(response.cookies()))
.expectComplete()
.verify();
}
@Test
public void render() throws Exception {
Map<String, Object> model = Collections.singletonMap("foo", "bar");

View File

@@ -32,7 +32,10 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import static org.junit.Assert.*;
@@ -264,10 +267,25 @@ public class DefaultServerResponseBuilderTests {
}
@Test
public void cookies() throws Exception {
MultiValueMap<String, ResponseCookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", ResponseCookie.from("name", "value").build());
Mono<ServerResponse> result =
ServerResponse.ok().cookies(cookies -> cookies.addAll(newCookies)).build();
StepVerifier.create(result)
.expectNextMatches(response -> newCookies.equals(response.cookies()))
.expectComplete()
.verify();
}
@Test
public void build() throws Exception {
ResponseCookie cookie = ResponseCookie.from("name", "value").build();
Mono<ServerResponse>
result = ServerResponse.status(HttpStatus.CREATED).header("MyKey", "MyValue").build();
result = ServerResponse.status(HttpStatus.CREATED)
.header("MyKey", "MyValue")
.cookie(cookie).build();
ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse();
@@ -278,6 +296,7 @@ public class DefaultServerResponseBuilderTests {
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("MyValue", response.getHeaders().getFirst("MyKey"));
assertEquals("value", response.getCookies().getFirst("name").getValue());
StepVerifier.create(response.getBody()).expectComplete().verify();
}

View File

@@ -25,9 +25,12 @@ import reactor.test.StepVerifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
@@ -197,6 +200,11 @@ public class RouterFunctionsTests {
return new HttpHeaders();
}
@Override
public MultiValueMap<String, ResponseCookie> cookies() {
return new LinkedMultiValueMap<>();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange,
Context context) {
@@ -230,6 +238,11 @@ public class RouterFunctionsTests {
return new HttpHeaders();
}
@Override
public MultiValueMap<String, ResponseCookie> cookies() {
return new LinkedMultiValueMap<>();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange,
Context context) {