Deprecate MockServerRequest
Deprecate MockServerRequest in favor of ServerRequest::create combined with a MockServerWebExchange.
This commit is contained in:
parent
c03cdbac21
commit
3cbea86335
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -45,6 +45,7 @@ import org.springframework.http.server.PathContainer;
|
|||
import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
|
|
@ -62,7 +63,11 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
* @deprecated as of 5.2.5 in favor of
|
||||
* {@link ServerRequest#create(ServerWebExchange, List)} combined with
|
||||
* {@link MockServerWebExchange}.
|
||||
*/
|
||||
@Deprecated
|
||||
public final class MockServerRequest implements ServerRequest {
|
||||
|
||||
private final HttpMethod method;
|
||||
|
|
|
|||
|
|
@ -1,593 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRange;
|
||||
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;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
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.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.util.UriBuilder;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link ServerRequest}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
*/
|
||||
public final class MockServerRequest implements ServerRequest {
|
||||
|
||||
private final HttpMethod method;
|
||||
|
||||
private final URI uri;
|
||||
|
||||
private final RequestPath pathContainer;
|
||||
|
||||
private final MockHeaders headers;
|
||||
|
||||
private final MultiValueMap<String, HttpCookie> cookies;
|
||||
|
||||
@Nullable
|
||||
private final Object body;
|
||||
|
||||
private final Map<String, Object> attributes;
|
||||
|
||||
private final MultiValueMap<String, String> queryParams;
|
||||
|
||||
private final Map<String, String> pathVariables;
|
||||
|
||||
@Nullable
|
||||
private final WebSession session;
|
||||
|
||||
@Nullable
|
||||
private Principal principal;
|
||||
|
||||
@Nullable
|
||||
private final InetSocketAddress remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private final InetSocketAddress localAddress;
|
||||
|
||||
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 localAddress,
|
||||
List<HttpMessageReader<?>> messageReaders, @Nullable ServerWebExchange exchange) {
|
||||
|
||||
this.method = method;
|
||||
this.uri = uri;
|
||||
this.pathContainer = RequestPath.parse(uri, contextPath);
|
||||
this.headers = headers;
|
||||
this.cookies = cookies;
|
||||
this.body = body;
|
||||
this.attributes = attributes;
|
||||
this.queryParams = queryParams;
|
||||
this.pathVariables = pathVariables;
|
||||
this.session = session;
|
||||
this.principal = principal;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.localAddress = localAddress;
|
||||
this.messageReaders = messageReaders;
|
||||
this.exchange = exchange;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpMethod method() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String methodName() {
|
||||
return this.method.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI uri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UriBuilder uriBuilder() {
|
||||
return UriComponentsBuilder.fromUri(this.uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathContainer pathContainer() {
|
||||
return this.pathContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Headers headers() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, HttpCookie> cookies() {
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> remoteAddress() {
|
||||
return Optional.ofNullable(this.remoteAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InetSocketAddress> localAddress() {
|
||||
return Optional.ofNullable(this.localAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpMessageReader<?>> messageReaders() {
|
||||
return this.messageReaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (S) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> S body(BodyExtractor<S, ? super ServerHttpRequest> extractor, Map<String, Object> hints) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (S) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> Mono<S> bodyToMono(Class<? extends S> elementClass) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (Mono<S>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> Mono<S> bodyToMono(ParameterizedTypeReference<S> typeReference) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (Mono<S>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> Flux<S> bodyToFlux(Class<? extends S> elementClass) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (Flux<S>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <S> Flux<S> bodyToFlux(ParameterizedTypeReference<S> typeReference) {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (Flux<S>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> attributes() {
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, String> queryParams() {
|
||||
return CollectionUtils.unmodifiableMultiValueMap(this.queryParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> pathVariables() {
|
||||
return Collections.unmodifiableMap(this.pathVariables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<WebSession> session() {
|
||||
return Mono.justOrEmpty(this.session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<? extends Principal> principal() {
|
||||
return Mono.justOrEmpty(this.principal);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<MultiValueMap<String, String>> formData() {
|
||||
Assert.state(this.body != null, "No body");
|
||||
return (Mono<MultiValueMap<String, String>>) this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<MultiValueMap<String, Part>> multipartData() {
|
||||
Assert.state(this.body != null, "No body");
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link MockServerRequest}.
|
||||
*/
|
||||
public interface Builder {
|
||||
|
||||
Builder method(HttpMethod method);
|
||||
|
||||
Builder uri(URI uri);
|
||||
|
||||
Builder contextPath(String contextPath);
|
||||
|
||||
Builder header(String key, String value);
|
||||
|
||||
Builder headers(HttpHeaders headers);
|
||||
|
||||
Builder cookie(HttpCookie... cookies);
|
||||
|
||||
Builder cookies(MultiValueMap<String, HttpCookie> cookies);
|
||||
|
||||
Builder attribute(String name, Object value);
|
||||
|
||||
Builder attributes(Map<String, Object> attributes);
|
||||
|
||||
Builder queryParam(String key, String value);
|
||||
|
||||
Builder queryParams(MultiValueMap<String, String> queryParams);
|
||||
|
||||
Builder pathVariable(String key, String value);
|
||||
|
||||
Builder pathVariables(Map<String, String> pathVariables);
|
||||
|
||||
Builder session(WebSession session);
|
||||
|
||||
/**
|
||||
* Sets the request {@link Principal}.
|
||||
* @deprecated in favor of {@link #principal(Principal)}
|
||||
*/
|
||||
@Deprecated
|
||||
Builder session(Principal principal);
|
||||
|
||||
Builder principal(Principal principal);
|
||||
|
||||
Builder remoteAddress(InetSocketAddress remoteAddress);
|
||||
|
||||
Builder localAddress(InetSocketAddress localAddress);
|
||||
|
||||
Builder messageReaders(List<HttpMessageReader<?>> messageReaders);
|
||||
|
||||
Builder exchange(ServerWebExchange exchange);
|
||||
|
||||
MockServerRequest body(Object body);
|
||||
|
||||
MockServerRequest build();
|
||||
}
|
||||
|
||||
|
||||
private static class BuilderImpl implements Builder {
|
||||
|
||||
private HttpMethod method = HttpMethod.GET;
|
||||
|
||||
private URI uri = URI.create("http://localhost");
|
||||
|
||||
private String contextPath = "";
|
||||
|
||||
private MockHeaders headers = new MockHeaders(new HttpHeaders());
|
||||
|
||||
private MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
@Nullable
|
||||
private Object body;
|
||||
|
||||
private Map<String, Object> attributes = new ConcurrentHashMap<>();
|
||||
|
||||
private MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||
|
||||
private Map<String, String> pathVariables = new LinkedHashMap<>();
|
||||
|
||||
@Nullable
|
||||
private WebSession session;
|
||||
|
||||
@Nullable
|
||||
private Principal principal;
|
||||
|
||||
@Nullable
|
||||
private InetSocketAddress remoteAddress;
|
||||
|
||||
@Nullable
|
||||
private InetSocketAddress localAddress;
|
||||
|
||||
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");
|
||||
this.method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder uri(URI uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
this.uri = uri;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder contextPath(String contextPath) {
|
||||
Assert.notNull(contextPath, "'contextPath' must not be null");
|
||||
this.contextPath = contextPath;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder cookie(HttpCookie... cookies) {
|
||||
Arrays.stream(cookies).forEach(cookie -> this.cookies.add(cookie.getName(), cookie));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder cookies(MultiValueMap<String, HttpCookie> cookies) {
|
||||
Assert.notNull(cookies, "'cookies' must not be null");
|
||||
this.cookies = cookies;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder header(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.headers.header(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder headers(HttpHeaders headers) {
|
||||
Assert.notNull(headers, "'headers' must not be null");
|
||||
this.headers = new MockHeaders(headers);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder attribute(String name, Object value) {
|
||||
Assert.notNull(name, "'name' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.attributes.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder attributes(Map<String, Object> attributes) {
|
||||
Assert.notNull(attributes, "'attributes' must not be null");
|
||||
this.attributes = attributes;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder queryParam(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.queryParams.add(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder queryParams(MultiValueMap<String, String> queryParams) {
|
||||
Assert.notNull(queryParams, "'queryParams' must not be null");
|
||||
this.queryParams = queryParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder pathVariable(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.pathVariables.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder pathVariables(Map<String, String> pathVariables) {
|
||||
Assert.notNull(pathVariables, "'pathVariables' must not be null");
|
||||
this.pathVariables = pathVariables;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder session(WebSession session) {
|
||||
Assert.notNull(session, "'session' must not be null");
|
||||
this.session = session;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Builder session(Principal principal) {
|
||||
return principal(principal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder principal(Principal principal) {
|
||||
Assert.notNull(principal, "'principal' must not be null");
|
||||
this.principal = principal;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder remoteAddress(InetSocketAddress remoteAddress) {
|
||||
Assert.notNull(remoteAddress, "'remoteAddress' must not be null");
|
||||
this.remoteAddress = remoteAddress;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder localAddress(InetSocketAddress localAddress) {
|
||||
Assert.notNull(localAddress, "'localAddress' must not be null");
|
||||
this.localAddress = localAddress;
|
||||
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.localAddress,
|
||||
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.localAddress,
|
||||
this.messageReaders, this.exchange);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MockHeaders implements Headers {
|
||||
|
||||
private final HttpHeaders headers;
|
||||
|
||||
public MockHeaders(HttpHeaders headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
private HttpHeaders delegate() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
public void header(String key, String value) {
|
||||
this.headers.add(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MediaType> accept() {
|
||||
return delegate().getAccept();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Charset> acceptCharset() {
|
||||
return delegate().getAcceptCharset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Locale.LanguageRange> acceptLanguage() {
|
||||
return delegate().getAcceptLanguage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OptionalLong contentLength() {
|
||||
return toOptionalLong(delegate().getContentLength());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<MediaType> contentType() {
|
||||
return Optional.ofNullable(delegate().getContentType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public InetSocketAddress host() {
|
||||
return delegate().getHost();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HttpRange> range() {
|
||||
return delegate().getRange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> header(String headerName) {
|
||||
List<String> headerValues = delegate().get(headerName);
|
||||
return headerValues != null ? headerValues : Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders asHttpHeaders() {
|
||||
return HttpHeaders.readOnlyHttpHeaders(delegate());
|
||||
}
|
||||
|
||||
private OptionalLong toOptionalLong(long value) {
|
||||
return value != -1 ? OptionalLong.of(value) : OptionalLong.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -18,7 +18,7 @@ package org.springframework.web.reactive.function.server;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -27,6 +27,8 @@ import reactor.test.StepVerifier;
|
|||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
|
|
@ -39,9 +41,8 @@ public class PathResourceLookupFunctionTests {
|
|||
|
||||
PathResourceLookupFunction
|
||||
function = new PathResourceLookupFunction("/resources/**", location);
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.uri(new URI("http://localhost/resources/response.txt"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/response.txt").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<Resource> result = function.apply(request);
|
||||
|
||||
File expected = new ClassPathResource("response.txt", getClass()).getFile();
|
||||
|
|
@ -63,9 +64,8 @@ public class PathResourceLookupFunctionTests {
|
|||
ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
|
||||
|
||||
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.uri(new URI("http://localhost/resources/child/response.txt"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/child/response.txt").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<Resource> result = function.apply(request);
|
||||
String path = "org/springframework/web/reactive/function/server/child/response.txt";
|
||||
File expected = new ClassPathResource(path).getFile();
|
||||
|
|
@ -87,9 +87,8 @@ public class PathResourceLookupFunctionTests {
|
|||
ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
|
||||
|
||||
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.uri(new URI("http://localhost/resources/foo"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<Resource> result = function.apply(request);
|
||||
StepVerifier.create(result)
|
||||
.expectComplete()
|
||||
|
|
@ -108,9 +107,8 @@ public class PathResourceLookupFunctionTests {
|
|||
lookupFunction.andThen(resourceMono -> resourceMono
|
||||
.switchIfEmpty(Mono.just(defaultResource)));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.uri(new URI("http://localhost/resources/foo"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/foo").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Resource> result = customLookupFunction.apply(request);
|
||||
StepVerifier.create(result)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,8 +16,13 @@
|
|||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
|
|
@ -31,7 +36,8 @@ public class RequestPredicateTests {
|
|||
RequestPredicate predicate2 = request -> true;
|
||||
RequestPredicate predicate3 = request -> false;
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
assertThat(predicate1.and(predicate2).test(request)).isTrue();
|
||||
assertThat(predicate2.and(predicate1).test(request)).isTrue();
|
||||
assertThat(predicate1.and(predicate3).test(request)).isFalse();
|
||||
|
|
@ -42,13 +48,14 @@ public class RequestPredicateTests {
|
|||
RequestPredicate predicate = request -> false;
|
||||
RequestPredicate negated = predicate.negate();
|
||||
|
||||
MockServerRequest mockRequest = MockServerRequest.builder().build();
|
||||
assertThat(negated.test(mockRequest)).isTrue();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
assertThat(negated.test(request)).isTrue();
|
||||
|
||||
predicate = request -> true;
|
||||
predicate = r -> true;
|
||||
negated = predicate.negate();
|
||||
|
||||
assertThat(negated.test(mockRequest)).isFalse();
|
||||
assertThat(negated.test(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -57,7 +64,8 @@ public class RequestPredicateTests {
|
|||
RequestPredicate predicate2 = request -> false;
|
||||
RequestPredicate predicate3 = request -> false;
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
assertThat(predicate1.or(predicate2).test(request)).isTrue();
|
||||
assertThat(predicate2.or(predicate1).test(request)).isTrue();
|
||||
assertThat(predicate2.or(predicate3).test(request)).isFalse();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -25,9 +25,10 @@ import reactor.test.StepVerifier;
|
|||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.HEAD;
|
||||
|
|
@ -45,13 +46,11 @@ public class RouterFunctionBuilderTests {
|
|||
.route(HEAD("/foo"), request -> ServerResponse.accepted().build())
|
||||
.build();
|
||||
|
||||
MockServerRequest getFooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/foo"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/foo").build();
|
||||
ServerRequest getRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(getFooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
|
||||
Mono<Integer> responseMono = route.route(getRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
|
||||
|
|
@ -59,13 +58,12 @@ public class RouterFunctionBuilderTests {
|
|||
.expectNext(200)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest headFooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.HEAD).
|
||||
uri(URI.create("http://localhost/foo"))
|
||||
.build();
|
||||
mockRequest = MockServerHttpRequest.head("https://example.com/foo").build();
|
||||
ServerRequest headRequest =
|
||||
new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
responseMono = route.route(headFooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(getFooRequest))
|
||||
responseMono = route.route(headRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(headRequest))
|
||||
.map(ServerResponse::statusCode)
|
||||
.map(HttpStatus::value);
|
||||
|
||||
|
|
@ -73,11 +71,10 @@ public class RouterFunctionBuilderTests {
|
|||
.expectNext(202)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest barRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.POST).
|
||||
uri(URI.create("http://localhost/"))
|
||||
.header("Content-Type", "text/plain")
|
||||
.build();
|
||||
mockRequest = MockServerHttpRequest.post("https://example.com/").
|
||||
contentType(MediaType.TEXT_PLAIN).build();
|
||||
ServerRequest barRequest =
|
||||
new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
responseMono = route.route(barRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(barRequest))
|
||||
|
|
@ -88,10 +85,9 @@ public class RouterFunctionBuilderTests {
|
|||
.expectNext(204)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest invalidRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.POST).
|
||||
uri(URI.create("http://localhost/"))
|
||||
.build();
|
||||
mockRequest = MockServerHttpRequest.post("https://example.com/").build();
|
||||
ServerRequest invalidRequest =
|
||||
new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
responseMono = route.route(invalidRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(invalidRequest))
|
||||
|
|
@ -112,10 +108,8 @@ public class RouterFunctionBuilderTests {
|
|||
.resources("/resources/**", resource)
|
||||
.build();
|
||||
|
||||
MockServerRequest resourceRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/resources/response.txt"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/response.txt").build();
|
||||
ServerRequest resourceRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(resourceRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(resourceRequest))
|
||||
|
|
@ -126,10 +120,8 @@ public class RouterFunctionBuilderTests {
|
|||
.expectNext(200)
|
||||
.verifyComplete();
|
||||
|
||||
MockServerRequest invalidRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.POST).
|
||||
uri(URI.create("http://localhost/resources/foo.txt"))
|
||||
.build();
|
||||
mockRequest = MockServerHttpRequest.post("https://localhost/resources/foo.txt").build();
|
||||
ServerRequest invalidRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
responseMono = route.route(invalidRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(invalidRequest))
|
||||
|
|
@ -150,10 +142,8 @@ public class RouterFunctionBuilderTests {
|
|||
.build()))
|
||||
.build();
|
||||
|
||||
MockServerRequest fooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/foo/bar/baz"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/foo/bar/baz").build();
|
||||
ServerRequest fooRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> responseMono = route.route(fooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(fooRequest))
|
||||
|
|
@ -193,10 +183,8 @@ public class RouterFunctionBuilderTests {
|
|||
.onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build())
|
||||
.build();
|
||||
|
||||
MockServerRequest fooRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/foo"))
|
||||
.build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/foo").build();
|
||||
ServerRequest fooRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<ServerResponse> fooResponseMono = route.route(fooRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(fooRequest));
|
||||
|
|
@ -209,11 +197,8 @@ public class RouterFunctionBuilderTests {
|
|||
|
||||
filterCount.set(0);
|
||||
|
||||
MockServerRequest barRequest = MockServerRequest.builder().
|
||||
method(HttpMethod.GET).
|
||||
uri(URI.create("http://localhost/bar"))
|
||||
.build();
|
||||
|
||||
mockRequest = MockServerHttpRequest.get("https://localhost/bar").build();
|
||||
ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<Integer> barResponseMono = route.route(barRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(barRequest))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,10 +16,15 @@
|
|||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
|
|
@ -36,7 +41,8 @@ public class RouterFunctionTests {
|
|||
RouterFunction<ServerResponse> result = routerFunction1.and(routerFunction2);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
|
||||
|
||||
StepVerifier.create(resultHandlerFunction)
|
||||
|
|
@ -56,7 +62,8 @@ public class RouterFunctionTests {
|
|||
RouterFunction<?> result = routerFunction1.andOther(routerFunction2);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
|
||||
|
||||
StepVerifier.create(resultHandlerFunction)
|
||||
|
|
@ -73,7 +80,8 @@ public class RouterFunctionTests {
|
|||
RouterFunction<ServerResponse> result = routerFunction1.andRoute(requestPredicate, this::handlerMethod);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request);
|
||||
|
||||
StepVerifier.create(resultHandlerFunction)
|
||||
|
|
@ -101,7 +109,8 @@ public class RouterFunctionTests {
|
|||
RouterFunction<EntityResponse<Mono<Integer>>> result = routerFunction.filter(filterFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
Mono<EntityResponse<Mono<Integer>>> responseMono =
|
||||
result.route(request).flatMap(hf -> hf.handle(request));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
|
|
@ -35,6 +36,7 @@ import org.springframework.web.server.WebFilter;
|
|||
import org.springframework.web.server.WebFilterChain;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
|
@ -50,7 +52,8 @@ public class RouterFunctionsTests {
|
|||
public void routeMatch() {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
given(requestPredicate.test(request)).willReturn(true);
|
||||
|
||||
|
|
@ -70,7 +73,8 @@ public class RouterFunctionsTests {
|
|||
public void routeNoMatch() {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
given(requestPredicate.test(request)).willReturn(false);
|
||||
|
||||
|
|
@ -88,7 +92,8 @@ public class RouterFunctionsTests {
|
|||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction);
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
given(requestPredicate.nest(request)).willReturn(Optional.of(request));
|
||||
|
||||
|
|
@ -107,7 +112,8 @@ public class RouterFunctionsTests {
|
|||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction);
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
|
||||
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
RequestPredicate requestPredicate = mock(RequestPredicate.class);
|
||||
given(requestPredicate.nest(request)).willReturn(Optional.empty());
|
||||
|
||||
|
|
@ -129,7 +135,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
|
|
@ -147,7 +153,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
|
@ -163,7 +169,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
|
@ -179,7 +185,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
|
|
@ -217,7 +223,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
|
|
@ -255,7 +261,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
|
|
@ -283,7 +289,7 @@ public class RouterFunctionsTests {
|
|||
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
|
||||
assertThat(result).isNotNull();
|
||||
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
|
||||
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
|
||||
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
|
||||
result.handle(httpRequest, httpResponse).block();
|
||||
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -23,9 +23,9 @@ import org.springframework.http.HttpHeaders.*
|
|||
import org.springframework.http.HttpMethod.*
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType.*
|
||||
import org.springframework.web.reactive.function.server.MockServerRequest.builder
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.*
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange
|
||||
import reactor.test.StepVerifier
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* Tests for [CoRouterFunctionDsl].
|
||||
|
|
@ -36,7 +36,9 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun header() {
|
||||
val request = builder().header("bar", "bar").build()
|
||||
val mockRequest = get("https://example.com")
|
||||
.header("bar","bar").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -44,7 +46,9 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun accept() {
|
||||
val request = builder().uri(URI("/content")).header(ACCEPT, APPLICATION_ATOM_XML_VALUE).build()
|
||||
val mockRequest = get("https://example.com/content")
|
||||
.header(ACCEPT, APPLICATION_ATOM_XML_VALUE).build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -52,11 +56,10 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun acceptAndPOST() {
|
||||
val request = builder()
|
||||
.method(POST)
|
||||
.uri(URI("/api/foo/"))
|
||||
val mockRequest = post("https://example.com/api/foo/")
|
||||
.header(ACCEPT, APPLICATION_JSON_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -64,11 +67,10 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun acceptAndPOSTWithRequestPredicate() {
|
||||
val request = builder()
|
||||
.method(POST)
|
||||
.uri(URI("/api/bar/"))
|
||||
val mockRequest = post("https://example.com/api/bar/")
|
||||
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -76,7 +78,10 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun contentType() {
|
||||
val request = builder().uri(URI("/content")).header(CONTENT_TYPE, APPLICATION_OCTET_STREAM_VALUE).build()
|
||||
val mockRequest = get("https://example.com/content/")
|
||||
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -84,7 +89,9 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun resourceByPath() {
|
||||
val request = builder().uri(URI("/org/springframework/web/reactive/function/response.txt")).build()
|
||||
val mockRequest = get("https://example.com/org/springframework/web/reactive/function/response.txt")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -92,7 +99,9 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun method() {
|
||||
val request = builder().method(PATCH).build()
|
||||
val mockRequest = patch("https://example.com/")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -100,7 +109,8 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun path() {
|
||||
val request = builder().uri(URI("/baz")).build()
|
||||
val mockRequest = get("https://example.com/baz").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -108,7 +118,8 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun resource() {
|
||||
val request = builder().uri(URI("/response.txt")).build()
|
||||
val mockRequest = get("https://example.com/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -116,18 +127,19 @@ class CoRouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun noRoute() {
|
||||
val request = builder()
|
||||
.uri(URI("/bar"))
|
||||
val mockRequest = get("https://example.com/bar")
|
||||
.header(ACCEPT, APPLICATION_PDF_VALUE)
|
||||
.header(CONTENT_TYPE, APPLICATION_PDF_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rendering() {
|
||||
val request = builder().uri(URI("/rendering")).build()
|
||||
val mockRequest = get("https://example.com/rendering").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request).flatMap { it.handle(request) })
|
||||
.expectNextMatches { it is RenderingResponse}
|
||||
.verifyComplete()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -23,10 +23,10 @@ import org.springframework.http.HttpHeaders.*
|
|||
import org.springframework.http.HttpMethod.*
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType.*
|
||||
import org.springframework.web.reactive.function.server.MockServerRequest.builder
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.*
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
import java.net.URI
|
||||
|
||||
/**
|
||||
* Tests for [RouterFunctionDsl].
|
||||
|
|
@ -37,7 +37,9 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun header() {
|
||||
val request = builder().header("bar", "bar").build()
|
||||
val mockRequest = get("https://example.com")
|
||||
.header("bar","bar").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -45,7 +47,9 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun accept() {
|
||||
val request = builder().uri(URI("/content")).header(ACCEPT, APPLICATION_ATOM_XML_VALUE).build()
|
||||
val mockRequest = get("https://example.com/content")
|
||||
.header(ACCEPT, APPLICATION_ATOM_XML_VALUE).build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -53,11 +57,10 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun acceptAndPOST() {
|
||||
val request = builder()
|
||||
.method(POST)
|
||||
.uri(URI("/api/foo/"))
|
||||
val mockRequest = post("https://example.com/api/foo/")
|
||||
.header(ACCEPT, APPLICATION_JSON_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -65,11 +68,10 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun acceptAndPOSTWithRequestPredicate() {
|
||||
val request = builder()
|
||||
.method(POST)
|
||||
.uri(URI("/api/bar/"))
|
||||
val mockRequest = post("https://example.com/api/bar/")
|
||||
.header(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -77,7 +79,10 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun contentType() {
|
||||
val request = builder().uri(URI("/content")).header(CONTENT_TYPE, APPLICATION_OCTET_STREAM_VALUE).build()
|
||||
val mockRequest = get("https://example.com/content/")
|
||||
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -85,7 +90,9 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun resourceByPath() {
|
||||
val request = builder().uri(URI("/org/springframework/web/reactive/function/response.txt")).build()
|
||||
val mockRequest = get("https://example.com/org/springframework/web/reactive/function/response.txt")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -93,7 +100,9 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun method() {
|
||||
val request = builder().method(PATCH).build()
|
||||
val mockRequest = patch("https://example.com/")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -101,7 +110,8 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun path() {
|
||||
val request = builder().uri(URI("/baz")).build()
|
||||
val mockRequest = get("https://example.com/baz").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -109,7 +119,8 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun resource() {
|
||||
val request = builder().uri(URI("/response.txt")).build()
|
||||
val mockRequest = get("https://example.com/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
|
|
@ -117,18 +128,19 @@ class RouterFunctionDslTests {
|
|||
|
||||
@Test
|
||||
fun noRoute() {
|
||||
val request = builder()
|
||||
.uri(URI("/bar"))
|
||||
val mockRequest = get("https://example.com/bar")
|
||||
.header(ACCEPT, APPLICATION_PDF_VALUE)
|
||||
.header(CONTENT_TYPE, APPLICATION_PDF_VALUE)
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rendering() {
|
||||
val request = builder().uri(URI("/rendering")).build()
|
||||
val mockRequest = get("https://example.com/rendering").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request).flatMap { it.handle(request) })
|
||||
.expectNextMatches { it is RenderingResponse}
|
||||
.verifyComplete()
|
||||
|
|
|
|||
Loading…
Reference in New Issue