MockServerHttpRequest and Response set cookie headers
Issue: SPR-15522
This commit is contained in:
@@ -71,4 +71,9 @@ public class HttpCookie {
|
||||
return (this.name.equalsIgnoreCase(otherCookie.getName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name + '=' + this.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.time.Duration;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* An {@code HttpCookie} subclass with the additional attributes allowed in
|
||||
@@ -125,6 +126,34 @@ public final class ResponseCookie extends HttpCookie {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getName()).append('=').append(getValue());
|
||||
if (StringUtils.hasText(getPath())) {
|
||||
sb.append("; Path=").append(getPath());
|
||||
}
|
||||
if (StringUtils.hasText(this.domain)) {
|
||||
sb.append("; Domain=").append(this.domain);
|
||||
}
|
||||
if (!this.maxAge.isNegative()) {
|
||||
sb.append("; Max-Age=").append(this.maxAge);
|
||||
sb.append("; Expires=");
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
long seconds = this.maxAge.getSeconds();
|
||||
headers.setExpires(seconds > 0 ? System.currentTimeMillis() + seconds : 0);
|
||||
sb.append(headers.getFirst(HttpHeaders.EXPIRES));
|
||||
}
|
||||
|
||||
if (this.secure) {
|
||||
sb.append("; Secure");
|
||||
}
|
||||
if (this.httpOnly) {
|
||||
sb.append("; HttpOnly");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory method to obtain a builder for a server-defined cookie that starts
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -231,7 +232,7 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
/**
|
||||
* Add one or more cookies.
|
||||
*/
|
||||
B cookie(String name, HttpCookie... cookie);
|
||||
B cookie(HttpCookie... cookie);
|
||||
|
||||
/**
|
||||
* Add the given cookies.
|
||||
@@ -390,8 +391,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder cookie(String name, HttpCookie... cookies) {
|
||||
this.cookies.put(name, Arrays.asList(cookies));
|
||||
public BodyBuilder cookie(HttpCookie... cookies) {
|
||||
Arrays.stream(cookies).forEach(cookie -> this.cookies.add(cookie.getName(), cookie));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -485,9 +486,15 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
@Override
|
||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
applyCookies();
|
||||
return new MockServerHttpRequest(this.method, this.url, this.contextPath,
|
||||
this.headers, this.cookies, this.remoteAddress, body);
|
||||
}
|
||||
|
||||
private void applyCookies() {
|
||||
this.cookies.values().stream().flatMap(Collection::stream)
|
||||
.forEach(cookie -> this.headers.add(HttpHeaders.COOKIE, cookie.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.mock.http.server.reactive.test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -28,6 +29,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeType;
|
||||
@@ -85,6 +87,8 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse {
|
||||
|
||||
@Override
|
||||
protected void applyCookies() {
|
||||
getCookies().values().stream().flatMap(Collection::stream)
|
||||
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user