Provide simple way to create ServerRequest
This commit introduces support for creating a new `ServerRequest` from an existing instance. This is especially useful when filtering requests in a HandlerFilterFunction. Issue: SPR-16707
This commit is contained in:
@@ -40,6 +40,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRange;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
import org.springframework.http.server.PathContainer;
|
||||
import org.springframework.http.server.RequestPath;
|
||||
@@ -50,7 +51,9 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyExtractor;
|
||||
import org.springframework.web.reactive.function.server.HandlerStrategies;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.util.UriBuilder;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
@@ -91,12 +94,18 @@ public class MockServerRequest implements ServerRequest {
|
||||
@Nullable
|
||||
private final InetSocketAddress remoteAddress;
|
||||
|
||||
private final List<HttpMessageReader<?>> messageReaders;
|
||||
|
||||
@Nullable
|
||||
private final ServerWebExchange exchange;
|
||||
|
||||
|
||||
private MockServerRequest(HttpMethod method, URI uri, String contextPath, MockHeaders headers,
|
||||
MultiValueMap<String, HttpCookie> cookies, @Nullable Object body,
|
||||
Map<String, Object> attributes, MultiValueMap<String, String> queryParams,
|
||||
Map<String, String> pathVariables, @Nullable WebSession session, @Nullable Principal principal,
|
||||
@Nullable InetSocketAddress remoteAddress) {
|
||||
@Nullable InetSocketAddress remoteAddress, List<HttpMessageReader<?>> messageReaders,
|
||||
@Nullable ServerWebExchange exchange) {
|
||||
|
||||
this.method = method;
|
||||
this.uri = uri;
|
||||
@@ -110,6 +119,8 @@ public class MockServerRequest implements ServerRequest {
|
||||
this.session = session;
|
||||
this.principal = principal;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.messageReaders = messageReaders;
|
||||
this.exchange = exchange;
|
||||
}
|
||||
|
||||
|
||||
@@ -153,6 +164,11 @@ public class MockServerRequest implements ServerRequest {
|
||||
return Optional.ofNullable(this.remoteAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpMessageReader<?>> messageReaders() {
|
||||
return this.messageReaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
|
||||
@@ -220,7 +236,6 @@ public class MockServerRequest implements ServerRequest {
|
||||
return Mono.justOrEmpty(this.principal);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<MultiValueMap<String, String>> formData() {
|
||||
@@ -235,6 +250,12 @@ public class MockServerRequest implements ServerRequest {
|
||||
return (Mono<MultiValueMap<String, Part>>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerWebExchange exchange() {
|
||||
Assert.state(this.exchange != null, "No exchange");
|
||||
return this.exchange;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new BuilderImpl();
|
||||
}
|
||||
@@ -282,6 +303,10 @@ public class MockServerRequest implements ServerRequest {
|
||||
|
||||
Builder remoteAddress(InetSocketAddress remoteAddress);
|
||||
|
||||
Builder messageReaders(List<HttpMessageReader<?>> messageReaders);
|
||||
|
||||
Builder exchange(ServerWebExchange exchange);
|
||||
|
||||
MockServerRequest body(Object body);
|
||||
|
||||
MockServerRequest build();
|
||||
@@ -318,6 +343,11 @@ public class MockServerRequest implements ServerRequest {
|
||||
@Nullable
|
||||
private InetSocketAddress remoteAddress;
|
||||
|
||||
private List<HttpMessageReader<?>> messageReaders = HandlerStrategies.withDefaults().messageReaders();
|
||||
|
||||
@Nullable
|
||||
private ServerWebExchange exchange;
|
||||
|
||||
@Override
|
||||
public Builder method(HttpMethod method) {
|
||||
Assert.notNull(method, "'method' must not be null");
|
||||
@@ -440,19 +470,35 @@ public class MockServerRequest implements ServerRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder messageReaders(List<HttpMessageReader<?>> messageReaders) {
|
||||
Assert.notNull(messageReaders, "'messageReaders' must not be null");
|
||||
this.messageReaders = messageReaders;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder exchange(ServerWebExchange exchange) {
|
||||
Assert.notNull(exchange, "'exchange' must not be null");
|
||||
this.exchange = exchange;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerRequest body(Object body) {
|
||||
this.body = body;
|
||||
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
|
||||
this.cookies, this.body, this.attributes, this.queryParams, this.pathVariables,
|
||||
this.session, this.principal, this.remoteAddress);
|
||||
this.session, this.principal, this.remoteAddress, this.messageReaders,
|
||||
this.exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerRequest build() {
|
||||
return new MockServerRequest(this.method, this.uri, this.contextPath, this.headers,
|
||||
this.cookies, null, this.attributes, this.queryParams, this.pathVariables,
|
||||
this.session, this.principal, this.remoteAddress);
|
||||
this.session, this.principal, this.remoteAddress, this.messageReaders,
|
||||
this.exchange);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user