MockClientHttpRequest|Response support cookie headers
Issue: SPR-16124
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.mock.http.client.reactive.test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -26,6 +27,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.reactive.AbstractClientHttpRequest;
|
||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||
@@ -114,6 +116,8 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||
|
||||
@Override
|
||||
protected void applyCookies() {
|
||||
getCookies().values().stream().flatMap(Collection::stream)
|
||||
.forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.mock.http.client.reactive.test;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -69,6 +70,11 @@ public class MockClientHttpResponse implements ClientHttpResponse {
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
String headerName = HttpHeaders.SET_COOKIE;
|
||||
if (!getCookies().isEmpty() && this.headers.get(headerName) == null) {
|
||||
getCookies().values().stream().flatMap(Collection::stream)
|
||||
.forEach(cookie -> getHeaders().add(headerName, cookie.toString()));
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@@ -132,4 +138,4 @@ public class MockClientHttpResponse implements ClientHttpResponse {
|
||||
return (charset != null ? charset : StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -480,14 +480,16 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
@Override
|
||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
applyCookies();
|
||||
applyCookiesIfNecessary();
|
||||
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()));
|
||||
private void applyCookiesIfNecessary() {
|
||||
if (this.headers.get(HttpHeaders.COOKIE) == null) {
|
||||
this.cookies.values().stream().flatMap(Collection::stream)
|
||||
.forEach(cookie -> this.headers.add(HttpHeaders.COOKIE, cookie.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user