From 3cbea86335cf013fb48495fc05e56265d7ac8b1f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 10 Mar 2020 15:41:53 +0100 Subject: [PATCH] Deprecate MockServerRequest Deprecate MockServerRequest in favor of ServerRequest::create combined with a MockServerWebExchange. --- .../function/server/MockServerRequest.java | 7 +- .../function/server/MockServerRequest.java | 593 ------------------ .../PathResourceLookupFunctionTests.java | 26 +- .../server/RequestPredicateTests.java | 22 +- .../server/RouterFunctionBuilderTests.java | 75 +-- .../function/server/RouterFunctionTests.java | 19 +- .../function/server/RouterFunctionsTests.java | 30 +- .../server/CoRouterFunctionDslTests.kt | 50 +- .../function/server/RouterFunctionDslTests.kt | 50 +- 9 files changed, 157 insertions(+), 715 deletions(-) delete mode 100644 spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java diff --git a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java index d3dde94ada2..47cb0e40113 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java @@ -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; diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java deleted file mode 100644 index 4332dc4afa9..00000000000 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java +++ /dev/null @@ -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 cookies; - - @Nullable - private final Object body; - - private final Map attributes; - - private final MultiValueMap queryParams; - - private final Map pathVariables; - - @Nullable - private final WebSession session; - - @Nullable - private Principal principal; - - @Nullable - private final InetSocketAddress remoteAddress; - - @Nullable - private final InetSocketAddress localAddress; - - private final List> messageReaders; - - @Nullable - private final ServerWebExchange exchange; - - - private MockServerRequest(HttpMethod method, URI uri, String contextPath, MockHeaders headers, - MultiValueMap cookies, @Nullable Object body, - Map attributes, MultiValueMap queryParams, - Map pathVariables, @Nullable WebSession session, @Nullable Principal principal, - @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress, - List> 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 cookies() { - return this.cookies; - } - - @Override - public Optional remoteAddress() { - return Optional.ofNullable(this.remoteAddress); - } - - @Override - public Optional localAddress() { - return Optional.ofNullable(this.localAddress); - } - - @Override - public List> messageReaders() { - return this.messageReaders; - } - - @Override - @SuppressWarnings("unchecked") - public S body(BodyExtractor extractor) { - Assert.state(this.body != null, "No body"); - return (S) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public S body(BodyExtractor extractor, Map hints) { - Assert.state(this.body != null, "No body"); - return (S) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public Mono bodyToMono(Class elementClass) { - Assert.state(this.body != null, "No body"); - return (Mono) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public Mono bodyToMono(ParameterizedTypeReference typeReference) { - Assert.state(this.body != null, "No body"); - return (Mono) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public Flux bodyToFlux(Class elementClass) { - Assert.state(this.body != null, "No body"); - return (Flux) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public Flux bodyToFlux(ParameterizedTypeReference typeReference) { - Assert.state(this.body != null, "No body"); - return (Flux) this.body; - } - - @Override - public Map attributes() { - return this.attributes; - } - - @Override - public MultiValueMap queryParams() { - return CollectionUtils.unmodifiableMultiValueMap(this.queryParams); - } - - @Override - public Map pathVariables() { - return Collections.unmodifiableMap(this.pathVariables); - } - - @Override - public Mono session() { - return Mono.justOrEmpty(this.session); - } - - @Override - public Mono principal() { - return Mono.justOrEmpty(this.principal); - } - - @Override - @SuppressWarnings("unchecked") - public Mono> formData() { - Assert.state(this.body != null, "No body"); - return (Mono>) this.body; - } - - @Override - @SuppressWarnings("unchecked") - public Mono> multipartData() { - Assert.state(this.body != null, "No body"); - return (Mono>) 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 cookies); - - Builder attribute(String name, Object value); - - Builder attributes(Map attributes); - - Builder queryParam(String key, String value); - - Builder queryParams(MultiValueMap queryParams); - - Builder pathVariable(String key, String value); - - Builder pathVariables(Map 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> 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 cookies = new LinkedMultiValueMap<>(); - - @Nullable - private Object body; - - private Map attributes = new ConcurrentHashMap<>(); - - private MultiValueMap queryParams = new LinkedMultiValueMap<>(); - - private Map pathVariables = new LinkedHashMap<>(); - - @Nullable - private WebSession session; - - @Nullable - private Principal principal; - - @Nullable - private InetSocketAddress remoteAddress; - - @Nullable - private InetSocketAddress localAddress; - - private List> 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 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 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 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 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> 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 accept() { - return delegate().getAccept(); - } - - @Override - public List acceptCharset() { - return delegate().getAcceptCharset(); - } - - @Override - public List acceptLanguage() { - return delegate().getAcceptLanguage(); - } - - @Override - public OptionalLong contentLength() { - return toOptionalLong(delegate().getContentLength()); - } - - @Override - public Optional contentType() { - return Optional.ofNullable(delegate().getContentType()); - } - - @Override - public InetSocketAddress host() { - return delegate().getHost(); - } - - @Override - public List range() { - return delegate().getRange(); - } - - @Override - public List header(String headerName) { - List 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(); - } - - } - -} diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/PathResourceLookupFunctionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/PathResourceLookupFunctionTests.java index 0c62da5a3d7..5b5cf24e0f4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/PathResourceLookupFunctionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/PathResourceLookupFunctionTests.java @@ -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 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 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 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 result = customLookupFunction.apply(request); StepVerifier.create(result) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicateTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicateTests.java index 0b76e92f0b5..479d367ac29 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicateTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicateTests.java @@ -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(); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java index 479e04ea372..7a5f5292fc6 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionBuilderTests.java @@ -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 responseMono = route.route(getFooRequest) - .flatMap(handlerFunction -> handlerFunction.handle(getFooRequest)) + Mono 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 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 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 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 barResponseMono = route.route(barRequest) .flatMap(handlerFunction -> handlerFunction.handle(barRequest)) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java index 19757a9476d..286a8a8a12b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java @@ -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 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> 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> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) @@ -73,7 +80,8 @@ public class RouterFunctionTests { RouterFunction 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> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) @@ -101,7 +109,8 @@ public class RouterFunctionTests { RouterFunction>> 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>> responseMono = result.route(request).flatMap(hf -> hf.handle(request)); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java index 57919dece4a..46b29bdf6cd 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java @@ -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 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 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 handlerFunction = request -> ServerResponse.ok().build(); RouterFunction 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 handlerFunction = request -> ServerResponse.ok().build(); RouterFunction 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); diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDslTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDslTests.kt index e9ce80592ca..1a2bc064463 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDslTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDslTests.kt @@ -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() diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt index 8fe769c8237..a050776f48a 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt @@ -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()