Expose remoteAddress from ServerHttpRequest
Issue: SPR-15200
This commit is contained in:
@@ -48,10 +48,11 @@ public class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
@Override
|
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
URI uri = request.getURI();
|
||||
assertNotNull("Request URI host must not be null", uri.getHost());
|
||||
assertNotEquals("Request URI port must not be undefined", -1, uri.getPort());
|
||||
assertEquals("Request URI path is not valid", "/foo", uri.getPath());
|
||||
assertEquals("Request URI query is not valid", "param=bar", uri.getQuery());
|
||||
assertNotNull(uri.getHost());
|
||||
assertNotEquals(-1, uri.getPort());
|
||||
assertNotNull(request.getRemoteAddress());
|
||||
assertEquals("/foo", uri.getPath());
|
||||
assertEquals("param=bar", uri.getQuery());
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package org.springframework.mock.http.server.reactive.test;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -57,17 +59,21 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private final MultiValueMap<String, HttpCookie> cookies;
|
||||
|
||||
private final InetSocketAddress remoteAddress;
|
||||
|
||||
private final Flux<DataBuffer> body;
|
||||
|
||||
|
||||
private MockServerHttpRequest(HttpMethod httpMethod, URI uri, String contextPath,
|
||||
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
|
||||
InetSocketAddress remoteAddress,
|
||||
Publisher<? extends DataBuffer> body) {
|
||||
|
||||
super(uri, headers);
|
||||
this.httpMethod = httpMethod;
|
||||
this.contextPath = (contextPath != null ? contextPath : "");
|
||||
this.cookies = cookies;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.body = Flux.from(body);
|
||||
}
|
||||
|
||||
@@ -82,6 +88,11 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> getRemoteAddress() {
|
||||
return Optional.ofNullable(this.remoteAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> getBody() {
|
||||
return this.body;
|
||||
@@ -198,6 +209,11 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
*/
|
||||
B contextPath(String contextPath);
|
||||
|
||||
/**
|
||||
* Set the remote address to return.
|
||||
*/
|
||||
B remoteAddress(InetSocketAddress remoteAddress);
|
||||
|
||||
/**
|
||||
* Add one or more cookies.
|
||||
*/
|
||||
@@ -318,6 +334,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
private InetSocketAddress remoteAddress;
|
||||
|
||||
|
||||
public DefaultBodyBuilder(HttpMethod method, URI url) {
|
||||
this.method = method;
|
||||
@@ -330,6 +348,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder remoteAddress(InetSocketAddress remoteAddress) {
|
||||
this.remoteAddress = remoteAddress;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder cookie(String path, HttpCookie... cookies) {
|
||||
this.cookies.put(path, Arrays.asList(cookies));
|
||||
@@ -395,7 +419,7 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
||||
@Override
|
||||
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
|
||||
return new MockServerHttpRequest(this.method, this.url, this.contextPath,
|
||||
this.headers, this.cookies, body);
|
||||
this.headers, this.cookies, this.remoteAddress, body);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user