From ba3cc535f1b9ecaadac9130959a46662b25d01da Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 13 Jan 2017 15:08:03 -0500 Subject: [PATCH] Refactor reactive mock request and response support MockServerHttpRequest and MockServerHttpResponse now extend the same abstract base classes that server-specific implementations do and therefore approximate their behavior more closely. As an immediate consequence MockServerHttpRequest is read-only after it is created. Instead it now exposes static builder methods similar to those found in RequestEntity. This enforces more strictness as well as recycling of requests in tests and provides nicer builder methods. To simplify tests DefaultServerWebExchange now offers a constructor with just a request and response, and automatically creating a DefaultWebSessionManager. The spring-test module now also contains client-side reactive mock request and response implementations. The mock client request extends the same AbstractClientHttpRequest as client-specific implementations do. There is no abstract base class for client responses. Issue: SPR-14590 --- .../reactive}/MockClientHttpRequest.java | 66 +-- .../reactive}/MockClientHttpResponse.java | 95 ++-- .../http/client/reactive/package-info.java | 4 + .../reactive/MockServerHttpRequest.java | 460 ++++++++++++++---- .../reactive/MockServerHttpResponse.java | 122 ++--- .../reactive/DispatcherHandlerErrorTests.java | 48 +- ...positeContentTypeResolverBuilderTests.java | 61 +-- .../HeaderContentTypeResolverTests.java | 17 +- ...PathExtensionContentTypeResolverTests.java | 5 +- .../config/ResourceHandlerRegistryTests.java | 8 +- .../WebReactiveConfigurationSupportTests.java | 35 +- .../function/BodyExtractorsTests.java | 33 +- .../DefaultClientRequestBuilderTests.java | 9 +- .../server/ResourceHandlerFunctionTests.java | 9 +- .../function/server/RouterFunctionsTests.java | 4 +- .../handler/CorsUrlHandlerMappingTests.java | 22 +- .../handler/SimpleUrlHandlerMappingTests.java | 16 +- .../AppCacheManifestTransformerTests.java | 35 +- .../CachingResourceResolverTests.java | 40 +- .../CssLinkResourceTransformerTests.java | 35 +- .../resource/GzipResourceResolverTests.java | 40 +- .../ResourceTransformerSupportTests.java | 26 +- .../resource/ResourceUrlProviderTests.java | 6 +- .../resource/ResourceWebHandlerTests.java | 201 ++++---- .../VersionResourceResolverTests.java | 7 +- .../WebJarsResourceResolverTests.java | 8 +- .../result/HandlerResultHandlerTests.java | 37 +- .../CompositeRequestConditionTests.java | 39 +- .../ConsumesRequestConditionTests.java | 19 +- .../HeadersRequestConditionTests.java | 19 +- .../ParamsRequestConditionTests.java | 31 +- .../PatternsRequestConditionTests.java | 10 +- .../ProducesRequestConditionTests.java | 19 +- .../RequestConditionHolderTests.java | 17 +- .../condition/RequestMappingInfoTests.java | 74 +-- .../RequestMethodsRequestConditionTests.java | 16 +- .../method/HandlerMethodMappingTests.java | 2 +- .../method/InvocableHandlerMethodTests.java | 13 +- ...RequestMappingInfoHandlerMappingTests.java | 117 +++-- .../BindingContextFactoryTests.java | 12 +- ...ookieValueMethodArgumentResolverTests.java | 32 +- .../ErrorsArgumentResolverTests.java | 11 +- ...ssionValueMethodArgumentResolverTests.java | 12 +- .../HttpEntityArgumentResolverTests.java | 28 +- .../InitBinderBindingContextTests.java | 37 +- .../MessageReaderArgumentResolverTests.java | 41 +- .../MessageWriterResultHandlerTests.java | 6 +- ...lAttributeMethodArgumentResolverTests.java | 12 +- ...ariableMapMethodArgumentResolverTests.java | 12 +- ...thVariableMethodArgumentResolverTests.java | 12 +- ...tAttributeMethodArgumentResolverTests.java | 15 +- .../RequestBodyArgumentResolverTests.java | 28 +- ...tHeaderMapMethodArgumentResolverTests.java | 33 +- ...uestHeaderMethodArgumentResolverTests.java | 55 ++- ...stParamMapMethodArgumentResolverTests.java | 25 +- ...questParamMethodArgumentResolverTests.java | 29 +- .../ResponseBodyResultHandlerTests.java | 15 +- .../ResponseEntityResultHandlerTests.java | 72 +-- ...erverWebExchangeArgumentResolverTests.java | 14 +- ...nAttributeMethodArgumentResolverTests.java | 16 +- .../view/HttpMessageWriterViewTests.java | 5 +- .../result/view/RedirectViewTests.java | 60 +-- .../result/view/RequestContextTests.java | 11 +- .../ViewResolutionResultHandlerTests.java | 113 +++-- .../view/freemarker/FreeMarkerViewTests.java | 16 +- .../view/script/JRubyScriptTemplateTests.java | 5 +- .../script/JythonScriptTemplateTests.java | 2 +- .../script/KotlinScriptTemplateTests.java | 2 +- .../script/NashornScriptTemplateTests.java | 2 +- ...tParamMethodArgumentResolverKotlinTests.kt | 37 +- .../adapter/DefaultServerWebExchange.java | 12 + .../server/adapter/WebHttpHandlerBuilder.java | 1 + .../codec/EncoderHttpMessageWriterTests.java | 2 +- .../codec/FormHttpMessageReaderTests.java | 17 +- .../codec/ResourceHttpMessageWriterTests.java | 52 +- ...ServerSentEventHttpMessageReaderTests.java | 39 +- ...ServerSentEventHttpMessageWriterTests.java | 75 ++- .../HttpHandlerAdapterSupportTests.java | 9 +- .../reactive/test/MockClientHttpRequest.java | 106 ++++ .../reactive/test/MockClientHttpResponse.java | 132 +++++ .../reactive/test/MockServerHttpRequest.java | 460 ++++++++++++++---- .../reactive/test/MockServerHttpResponse.java | 129 ++--- .../support/WebExchangeDataBinderTests.java | 70 +-- .../web/cors/reactive/CorsUtilsTests.java | 34 +- .../reactive/DefaultCorsProcessorTests.java | 174 ++++--- .../UrlBasedCorsConfigurationSourceTests.java | 7 +- ...erverWebExchangeCheckNotModifiedTests.java | 124 ++--- .../ExceptionHandlingHttpHandlerTests.java | 8 +- .../handler/FilteringWebHandlerTests.java | 8 +- .../ResponseStatusExceptionHandlerTests.java | 25 +- .../DefaultWebSessionManagerTests.java | 11 +- 91 files changed, 2314 insertions(+), 1776 deletions(-) rename {spring-web/src/test/java/org/springframework/web/client/reactive/test => spring-test/src/main/java/org/springframework/mock/http/client/reactive}/MockClientHttpRequest.java (64%) rename {spring-web/src/test/java/org/springframework/web/client/reactive/test => spring-test/src/main/java/org/springframework/mock/http/client/reactive}/MockClientHttpResponse.java (54%) create mode 100644 spring-test/src/main/java/org/springframework/mock/http/client/reactive/package-info.java create mode 100644 spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpRequest.java create mode 100644 spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java diff --git a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java similarity index 64% rename from spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java rename to spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java index 5b6427e24b..64df2ecd3b 100644 --- a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.client.reactive.test; +package org.springframework.mock.http.client.reactive; import java.net.URI; @@ -28,60 +28,43 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpMethod; import org.springframework.http.client.reactive.AbstractClientHttpRequest; import org.springframework.http.client.reactive.ClientHttpRequest; +import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ClientHttpRequest}. * @author Brian Clozel + * @author Rossen Stoyanchev + * @since 5.0 */ public class MockClientHttpRequest extends AbstractClientHttpRequest { private HttpMethod httpMethod; - private URI uri; + private URI url; private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); private Flux body; - private Flux> bodyWithFlushes; - - public MockClientHttpRequest() { + public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) { + this(httpMethod, UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri()); } - public MockClientHttpRequest(HttpMethod httpMethod, String uri) { - this(httpMethod, (uri != null ? URI.create(uri) : null)); - } - - public MockClientHttpRequest(HttpMethod httpMethod, URI uri) { - super(); + public MockClientHttpRequest(HttpMethod httpMethod, URI url) { this.httpMethod = httpMethod; - this.uri = uri; + this.url = url; } + @Override public HttpMethod getMethod() { return this.httpMethod; } - public MockClientHttpRequest setMethod(HttpMethod httpMethod) { - this.httpMethod = httpMethod; - return this; - } - @Override public URI getURI() { - return this.uri; - } - - public MockClientHttpRequest setUri(String uri) { - this.uri = URI.create(uri); - return this; - } - - public MockClientHttpRequest setUri(URI uri) { - this.uri = uri; - return this; + return this.url; } @Override @@ -89,34 +72,35 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { return this.bufferFactory; } + public Flux getBody() { + return this.body; + } + @Override public Mono writeWith(Publisher body) { this.body = Flux.from(body); - return doCommit(() -> this.body.then()); + return doCommit(() -> { + this.body = Flux.from(body); + return Mono.empty(); + }); } @Override public Mono writeAndFlushWith(Publisher> body) { - this.bodyWithFlushes = Flux.from(body).map(p -> Flux.from(p)); - return doCommit(() -> this.bodyWithFlushes.then()); + return writeWith(Flux.from(body).flatMap(p -> p)); } - public Publisher getBody() { - return body; + @Override + protected void applyHeaders() { } - public Publisher> getBodyWithFlush() { - return bodyWithFlushes; + @Override + protected void applyCookies() { } @Override public Mono setComplete() { - return doCommit().then(); + return doCommit(Mono::empty); } - @Override - protected void applyHeaders() { } - - @Override - protected void applyCookies() { } } diff --git a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java similarity index 54% rename from spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java rename to spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java index 35f6882c99..b67b3a1392 100644 --- a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.client.reactive.test; +package org.springframework.mock.http.client.reactive; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -22,23 +22,30 @@ import java.nio.charset.StandardCharsets; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferFactory; +import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseCookie; import org.springframework.http.client.reactive.ClientHttpResponse; +import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; /** * Mock implementation of {@link ClientHttpResponse}. * @author Brian Clozel + * @author Rossen Stoyanchev + * @since 5.0 */ public class MockClientHttpResponse implements ClientHttpResponse { - private HttpStatus status; + private final HttpStatus status; private final HttpHeaders headers = new HttpHeaders(); @@ -46,60 +53,80 @@ public class MockClientHttpResponse implements ClientHttpResponse { private Flux body = Flux.empty(); - @Override - public HttpHeaders getHeaders() { - return headers; + private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); + + + public MockClientHttpResponse(HttpStatus status) { + Assert.notNull(status, "HttpStatus is required"); + this.status = status; } - public MockClientHttpResponse addHeader(String name, String value) { - getHeaders().add(name, value); - return this; - } - public MockClientHttpResponse setHeader(String name, String value) { - getHeaders().set(name, value); - return this; - } - - @Override public HttpStatus getStatusCode() { return this.status; } - public void setStatus(HttpStatus status) { - this.status = status; - } - @Override - public Flux getBody() { - return this.body; + public HttpHeaders getHeaders() { + return this.headers; } - public MockClientHttpResponse setBody(Publisher body) { + public MultiValueMap getCookies() { + return this.cookies; + } + + public void setBody(Publisher body) { this.body = Flux.from(body); - return this; } - public MockClientHttpResponse setBody(String body) { - DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8); - this.body = Flux.just(buffer); - return this; + public void setBody(String body) { + setBody(body, StandardCharsets.UTF_8); } - public MockClientHttpResponse setBody(String body, Charset charset) { + public void setBody(String body, Charset charset) { DataBuffer buffer = toDataBuffer(body, charset); this.body = Flux.just(buffer); - return this; } private DataBuffer toDataBuffer(String body, Charset charset) { byte[] bytes = body.getBytes(charset); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - return new DefaultDataBufferFactory().wrap(byteBuffer); + return this.bufferFactory.wrap(byteBuffer); } - @Override - public MultiValueMap getCookies() { - return this.cookies; + public Flux getBody() { + return this.body; } -} + + /** + * Return the response body aggregated and converted to a String using the + * charset of the Content-Type response or otherwise as "UTF-8". + */ + public Mono getBodyAsString() { + Charset charset = getCharset(); + return Flux.from(getBody()) + .reduce(bufferFactory.allocateBuffer(), (previous, current) -> { + previous.write(current); + DataBufferUtils.release(current); + return previous; + }) + .map(buffer -> dumpString(buffer, charset)); + } + + private static String dumpString(DataBuffer buffer, Charset charset) { + Assert.notNull(charset, "'charset' must not be null"); + byte[] bytes = new byte[buffer.readableByteCount()]; + buffer.read(bytes); + return new String(bytes, charset); + } + + private Charset getCharset() { + Charset charset = null; + MediaType contentType = getHeaders().getContentType(); + if (contentType != null) { + charset = contentType.getCharset(); + } + return (charset != null ? charset : StandardCharsets.UTF_8); + } + +} \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/package-info.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/package-info.java new file mode 100644 index 0000000000..09ce8aec5c --- /dev/null +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/package-info.java @@ -0,0 +1,4 @@ +/** + * Mock implementations of reactive HTTP client contracts. + */ +package org.springframework.mock.http.client.reactive; diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java index ada440f66b..40cba344c7 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java @@ -19,6 +19,8 @@ import java.net.URI; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -28,140 +30,394 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; 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.server.reactive.AbstractServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ServerHttpRequest}. + * + *

Note: this class extends the same + * {@link AbstractServerHttpRequest} base class as actual server-specific + * implementation and is therefore read-only once created. Use static builder + * methods in this class to build up request instances. + * * @author Rossen Stoyanchev * @since 5.0 */ -public class MockServerHttpRequest implements ServerHttpRequest { +public class MockServerHttpRequest extends AbstractServerHttpRequest { - private HttpMethod httpMethod; + private final HttpMethod httpMethod; - private URI url; + private final String contextPath; - private String contextPath = ""; + private final MultiValueMap cookies; - private final MultiValueMap queryParams = new LinkedMultiValueMap<>(); - - private final HttpHeaders headers = new HttpHeaders(); - - private final MultiValueMap cookies = new LinkedMultiValueMap<>(); - - private Flux body = Flux.empty(); + private final Flux body; - /** - * Create a new instance where the HTTP method and/or URL can be set later - * via {@link #setHttpMethod(HttpMethod)} and {@link #setUri(URI)}. - */ - public MockServerHttpRequest() { - } + private MockServerHttpRequest(HttpMethod httpMethod, URI uri, String contextPath, + HttpHeaders headers, MultiValueMap cookies, + Publisher body) { - /** - * Convenience alternative to {@link #MockServerHttpRequest(HttpMethod, URI)} - * that accepts a String URL. - */ - public MockServerHttpRequest(HttpMethod httpMethod, String url) { - this(httpMethod, (url != null ? URI.create(url) : null)); - } - - /** - * Create a new instance with the given HTTP method and URL. - */ - public MockServerHttpRequest(HttpMethod httpMethod, URI url) { + super(uri, headers); this.httpMethod = httpMethod; - this.url = url; + this.contextPath = (contextPath != null ? contextPath : ""); + this.cookies = cookies; + this.body = Flux.from(body); } - public void setHttpMethod(HttpMethod httpMethod) { - this.httpMethod = httpMethod; - } - @Override public HttpMethod getMethod() { return this.httpMethod; } - public MockServerHttpRequest setUri(String url) { - this.url = URI.create(url); - return this; - } - - public MockServerHttpRequest setUri(URI uri) { - this.url = uri; - return this; - } - - @Override - public URI getURI() { - return this.url; - } - - public void setContextPath(String contextPath) { - this.contextPath = contextPath; - } - @Override public String getContextPath() { return this.contextPath; } - public MockServerHttpRequest addHeader(String name, String value) { - getHeaders().add(name, value); - return this; - } - - public MockServerHttpRequest setHeader(String name, String value) { - getHeaders().set(name, value); - return this; - } - - @Override - public HttpHeaders getHeaders() { - return this.headers; - } - - @Override - public MultiValueMap getQueryParams() { - return this.queryParams; - } - - @Override - public MultiValueMap getCookies() { - return this.cookies; - } - - public MockServerHttpRequest setBody(Publisher body) { - this.body = Flux.from(body); - return this; - } - - public MockServerHttpRequest setBody(String body) { - DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8); - this.body = Flux.just(buffer); - return this; - } - - public MockServerHttpRequest setBody(String body, Charset charset) { - DataBuffer buffer = toDataBuffer(body, charset); - this.body = Flux.just(buffer); - return this; - } - - private DataBuffer toDataBuffer(String body, Charset charset) { - byte[] bytes = body.getBytes(charset); - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - return new DefaultDataBufferFactory().wrap(byteBuffer); - } - @Override public Flux getBody() { return this.body; } -} + @Override + protected MultiValueMap initCookies() { + return this.cookies; + } + + // Static builder methods + + /** + * Create a builder with the given HTTP method and a {@link URI}. + * @param method the HTTP method (GET, POST, etc) + * @param url the URL + * @return the created builder + */ + public static BodyBuilder method(HttpMethod method, URI url) { + return new DefaultBodyBuilder(method, url); + } + + /** + * Alternative to {@link #method(HttpMethod, URI)} that accepts a URI template. + * @param method the HTTP method (GET, POST, etc) + * @param urlTemplate the URL template + * @param vars variables to expand into the template + * @return the created builder + */ + public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) { + URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); + return new DefaultBodyBuilder(method, url); + } + + /** + * Create an HTTP GET builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder get(String urlTemplate, Object... uriVars) { + return method(HttpMethod.GET, urlTemplate, uriVars); + } + + /** + * Create an HTTP HEAD builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder head(String urlTemplate, Object... uriVars) { + return method(HttpMethod.HEAD, urlTemplate, uriVars); + } + + /** + * Create an HTTP POST builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder post(String urlTemplate, Object... uriVars) { + return method(HttpMethod.POST, urlTemplate, uriVars); + } + + /** + * Create an HTTP PUT builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder put(String urlTemplate, Object... uriVars) { + return method(HttpMethod.PUT, urlTemplate, uriVars); + } + + /** + * Create an HTTP PATCH builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder patch(String urlTemplate, Object... uriVars) { + return method(HttpMethod.PATCH, urlTemplate, uriVars); + } + + /** + * Create an HTTP DELETE builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder delete(String urlTemplate, Object... uriVars) { + return method(HttpMethod.DELETE, urlTemplate, uriVars); + } + + /** + * Creates an HTTP OPTIONS builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder options(String urlTemplate, Object... uriVars) { + return method(HttpMethod.OPTIONS, urlTemplate, uriVars); + } + + + /** + * Defines a builder that adds headers to the request. + * @param the builder subclass + */ + public interface BaseBuilder> { + + /** + * Set the contextPath to return. + */ + B contextPath(String contextPath); + + /** + * Add one or more cookies. + */ + B cookie(String path, HttpCookie... cookie); + + /** + * Add the given, single header value under the given name. + * @param headerName the header name + * @param headerValues the header value(s) + * @see HttpHeaders#add(String, String) + */ + B header(String headerName, String... headerValues); + + /** + * Set the list of acceptable {@linkplain MediaType media types}, as + * specified by the {@code Accept} header. + * @param acceptableMediaTypes the acceptable media types + */ + B accept(MediaType... acceptableMediaTypes); + + /** + * Set the list of acceptable {@linkplain Charset charsets}, as specified + * by the {@code Accept-Charset} header. + * @param acceptableCharsets the acceptable charsets + */ + B acceptCharset(Charset... acceptableCharsets); + + /** + * Set the value of the {@code If-Modified-Since} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. + * @param ifModifiedSince the new value of the header + */ + B ifModifiedSince(long ifModifiedSince); + + /** + * Set the (new) value of the {@code If-Unmodified-Since} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. + * @param ifUnmodifiedSince the new value of the header + * @see HttpHeaders#setIfUnmodifiedSince(long) + */ + B ifUnmodifiedSince(long ifUnmodifiedSince); + + /** + * Set the values of the {@code If-None-Match} header. + * @param ifNoneMatches the new value of the header + */ + B ifNoneMatch(String... ifNoneMatches); + + /** + * Set the (new) value of the Range header. + * @param ranges the HTTP ranges + * @see HttpHeaders#setRange(List) + */ + B range(HttpRange... ranges); + + /** + * Builds the request with no body. + * @return the request + * @see BodyBuilder#body(Publisher) + * @see BodyBuilder#body(String) + */ + MockServerHttpRequest build(); + } + + /** + * A builder that adds a body to the request. + */ + public interface BodyBuilder extends BaseBuilder { + + /** + * Set the length of the body in bytes, as specified by the + * {@code Content-Length} header. + * @param contentLength the content length + * @return this builder + * @see HttpHeaders#setContentLength(long) + */ + BodyBuilder contentLength(long contentLength); + + /** + * Set the {@linkplain MediaType media type} of the body, as specified + * by the {@code Content-Type} header. + * @param contentType the content type + * @return this builder + * @see HttpHeaders#setContentType(MediaType) + */ + BodyBuilder contentType(MediaType contentType); + + /** + * Set the body of the request and build it. + * @param body the body + * @return the built request entity + */ + MockServerHttpRequest body(Publisher body); + + /** + * Set the body of the request and build it. + *

The String is assumed to be UTF-8 encoded unless the request has a + * "content-type" header with a charset attribute. + * @param body the body as text + * @return the built request entity + */ + MockServerHttpRequest body(String body); + + } + + + private static class DefaultBodyBuilder implements BodyBuilder { + + private final HttpMethod method; + + private final URI url; + + private String contextPath; + + private final HttpHeaders headers = new HttpHeaders(); + + private final MultiValueMap cookies = new LinkedMultiValueMap<>(); + + + public DefaultBodyBuilder(HttpMethod method, URI url) { + this.method = method; + this.url = url; + } + + @Override + public BodyBuilder contextPath(String contextPath) { + this.contextPath = contextPath; + return this; + } + + @Override + public BodyBuilder cookie(String path, HttpCookie... cookies) { + this.cookies.put(path, Arrays.asList(cookies)); + return this; + } + + @Override + public BodyBuilder header(String headerName, String... headerValues) { + for (String headerValue : headerValues) { + this.headers.add(headerName, headerValue); + } + return this; + } + + @Override + public BodyBuilder accept(MediaType... acceptableMediaTypes) { + this.headers.setAccept(Arrays.asList(acceptableMediaTypes)); + return this; + } + + @Override + public BodyBuilder acceptCharset(Charset... acceptableCharsets) { + this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets)); + return this; + } + + @Override + public BodyBuilder contentLength(long contentLength) { + this.headers.setContentLength(contentLength); + return this; + } + + @Override + public BodyBuilder contentType(MediaType contentType) { + this.headers.setContentType(contentType); + return this; + } + + @Override + public BodyBuilder ifModifiedSince(long ifModifiedSince) { + this.headers.setIfModifiedSince(ifModifiedSince); + return this; + } + + @Override + public BodyBuilder ifUnmodifiedSince(long ifUnmodifiedSince) { + this.headers.setIfUnmodifiedSince(ifUnmodifiedSince); + return this; + } + + @Override + public BodyBuilder ifNoneMatch(String... ifNoneMatches) { + this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches)); + return this; + } + + @Override + public BodyBuilder range(HttpRange... ranges) { + this.headers.setRange(Arrays.asList(ranges)); + return this; + } + + @Override + public MockServerHttpRequest body(Publisher body) { + return new MockServerHttpRequest(this.method, this.url, this.contextPath, + this.headers, this.cookies, body); + } + + @Override + public MockServerHttpRequest body(String body) { + Charset charset = getCharset(); + byte[] bytes = body.getBytes(charset); + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer); + return body(Flux.just(buffer)); + } + + private Charset getCharset() { + MediaType contentType = this.headers.getContentType(); + Charset charset = (contentType != null ? contentType.getCharset() : null); + charset = charset != null ? charset : StandardCharsets.UTF_8; + return charset; + } + + @Override + public MockServerHttpRequest build() { + return body(Flux.empty()); + } + } + +} \ No newline at end of file diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java index 12163771f5..2cbbfea7b7 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java @@ -18,130 +18,57 @@ package org.springframework.mock.http.server.reactive; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.function.Function; -import java.util.function.Supplier; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.ResponseCookie; +import org.springframework.http.server.reactive.AbstractServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.util.Assert; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; /** * Mock implementation of {@link ServerHttpResponse}. * @author Rossen Stoyanchev * @since 5.0 */ -public class MockServerHttpResponse implements ServerHttpResponse { - - private HttpStatus status; - - private final HttpHeaders headers = new HttpHeaders(); - - private final MultiValueMap cookies = new LinkedMultiValueMap<>(); - - private Function urlEncoder = url -> url; +public class MockServerHttpResponse extends AbstractServerHttpResponse { private Flux body; - private Flux> bodyWithFlushes; - private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); - - - @Override - public boolean setStatusCode(HttpStatus status) { - this.status = status; - return true; + public MockServerHttpResponse() { + super(new DefaultDataBufferFactory()); } - @Override - public HttpStatus getStatusCode() { - return this.status; - } - @Override - public HttpHeaders getHeaders() { - return this.headers; - } - - @Override - public MultiValueMap getCookies() { - return this.cookies; - } - - @Override - public String encodeUrl(String url) { - return (this.urlEncoder != null ? this.urlEncoder.apply(url) : url); - } - - @Override - public void registerUrlEncoder(Function encoder) { - this.urlEncoder = (this.urlEncoder != null ? this.urlEncoder.andThen(encoder) : encoder); - } - - public Publisher getBody() { + /** + * Return the output Publisher used to write to the response. + */ + public Flux getBody() { return this.body; } - public Publisher> getBodyWithFlush() { - return this.bodyWithFlushes; - } - - @Override - public Mono writeWith(Publisher body) { - this.body = Flux.from(body); - return this.body.then(); - } - - @Override - public Mono writeAndFlushWith(Publisher> body) { - this.bodyWithFlushes = Flux.from(body).map(Flux::from); - return this.bodyWithFlushes.then(); - } - - @Override - public void beforeCommit(Supplier> action) { - } - - @Override - public Mono setComplete() { - return Mono.empty(); - } - - @Override - public DataBufferFactory bufferFactory() { - return this.bufferFactory; - } - /** - * Return the body of the response aggregated and converted to a String - * using the charset of the Content-Type response or otherwise defaulting - * to "UTF-8". + * Return the response body aggregated and converted to a String using the + * charset of the Content-Type response or otherwise as "UTF-8". */ public Mono getBodyAsString() { Charset charset = getCharset(); - return Flux.from(getBody()) + return getBody() .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { previous.write(current); DataBufferUtils.release(current); return previous; }) - .map(buffer -> dumpString(buffer, charset)); + .map(buffer -> bufferToString(buffer, charset)); } - private static String dumpString(DataBuffer buffer, Charset charset) { + private static String bufferToString(DataBuffer buffer, Charset charset) { Assert.notNull(charset, "'charset' must not be null"); byte[] bytes = new byte[buffer.readableByteCount()]; buffer.read(bytes); @@ -157,4 +84,27 @@ public class MockServerHttpResponse implements ServerHttpResponse { return (charset != null ? charset : StandardCharsets.UTF_8); } + @Override + protected Mono writeWithInternal(Publisher body) { + this.body = Flux.from(body); + return Mono.empty(); + } + + @Override + protected Mono writeAndFlushWithInternal(Publisher> body) { + return writeWithInternal(Flux.from(body).flatMap(Flux::from)); + } + + @Override + protected void applyStatusCode() { + } + + @Override + protected void applyHeaders() { + } + + @Override + protected void applyCookies() { + } + } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java index 9020322c24..f610f83109 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java @@ -19,6 +19,7 @@ package org.springframework.web.reactive; import java.time.Duration; import java.util.Collections; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import org.reactivestreams.Publisher; @@ -29,7 +30,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.codec.CharSequenceEncoder; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; @@ -50,7 +50,6 @@ import org.springframework.web.server.WebExceptionHandler; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.handler.ExceptionHandlingWebHandler; -import org.springframework.web.server.session.MockWebSessionManager; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.startsWith; @@ -58,6 +57,7 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; +import static org.springframework.http.MediaType.APPLICATION_JSON; /** @@ -76,28 +76,20 @@ public class DispatcherHandlerErrorTests { private MockServerHttpRequest request; - private ServerWebExchange exchange; - @Before public void setUp() throws Exception { AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(); appContext.register(TestConfig.class); appContext.refresh(); - this.dispatcherHandler = new DispatcherHandler(appContext); - - this.request = new MockServerHttpRequest(HttpMethod.GET, "/"); - MockServerHttpResponse response = new MockServerHttpResponse(); - MockWebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, response, sessionManager); } @Test public void noHandler() throws Exception { - this.request.setUri("/does-not-exist"); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.get("/does-not-exist").build(); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> { @@ -110,8 +102,8 @@ public class DispatcherHandlerErrorTests { @Test public void controllerReturnsMonoError() throws Exception { - this.request.setUri("/error-signal"); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.get("/error-signal").build(); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> assertSame(EXCEPTION, error)) @@ -120,8 +112,8 @@ public class DispatcherHandlerErrorTests { @Test public void controllerThrowsException() throws Exception { - this.request.setUri("/raise-exception"); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.get("/raise-exception").build(); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> assertSame(EXCEPTION, error)) @@ -130,8 +122,8 @@ public class DispatcherHandlerErrorTests { @Test public void unknownReturnType() throws Exception { - this.request.setUri("/unknown-return-type"); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.get("/unknown-return-type").build(); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> { @@ -143,8 +135,8 @@ public class DispatcherHandlerErrorTests { @Test public void responseBodyMessageConversionError() throws Exception { - this.request.setUri("/request-body").setHeader("Accept", "application/json").setBody("body"); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.post("/request-body").accept(APPLICATION_JSON).body("body"); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> assertThat(error, instanceOf(NotAcceptableStatusException.class))) @@ -153,8 +145,8 @@ public class DispatcherHandlerErrorTests { @Test public void requestBodyError() throws Exception { - this.request.setUri("/request-body").setBody(Mono.error(EXCEPTION)); - Mono publisher = this.dispatcherHandler.handle(this.exchange); + this.request = MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)); + Mono publisher = this.dispatcherHandler.handle(createExchange()); StepVerifier.create(publisher) .consumeErrorWith(error -> { @@ -166,13 +158,19 @@ public class DispatcherHandlerErrorTests { @Test public void webExceptionHandler() throws Exception { - this.request.setUri("/unknown-argument-type"); + this.request = MockServerHttpRequest.get("/unknown-argument-type").build(); + ServerWebExchange exchange = createExchange(); WebExceptionHandler exceptionHandler = new ServerError500ExceptionHandler(); WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, exceptionHandler); - webHandler.handle(this.exchange).block(Duration.ofSeconds(5)); + webHandler.handle(exchange).block(Duration.ofSeconds(5)); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, this.exchange.getResponse().getStatusCode()); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode()); + } + + @NotNull + private ServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/CompositeContentTypeResolverBuilderTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/CompositeContentTypeResolverBuilderTests.java index 57bbdf44ca..808d2b5a8c 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/CompositeContentTypeResolverBuilderTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/CompositeContentTypeResolverBuilderTests.java @@ -17,19 +17,17 @@ package org.springframework.web.reactive.accept; import java.net.URISyntaxException; import java.util.Collections; +import java.util.List; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; @@ -54,14 +52,13 @@ public class CompositeContentTypeResolverBuilderTests { assertEquals("Should ignore unknown extensions by default", Collections.emptyList(), resolver.resolveMediaTypes(exchange)); - exchange = createExchange("/flower"); - exchange.getRequest().getQueryParams().add("format", "gif"); + exchange = createExchange("/flower?format=gif"); assertEquals("Should not resolve request parameters by default", Collections.emptyList(), resolver.resolveMediaTypes(exchange)); - exchange = createExchange("/flower"); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.IMAGE_GIF)); + ServerHttpRequest request = MockServerHttpRequest.get("/flower").accept(MediaType.IMAGE_GIF).build(); + exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); assertEquals("Should resolve Accept header by default", Collections.singletonList(MediaType.IMAGE_GIF), resolver.resolveMediaTypes(exchange)); @@ -108,9 +105,7 @@ public class CompositeContentTypeResolverBuilderTests { .ignoreUnknownPathExtensions(false) .build(); - ServerWebExchange exchange = createExchange("/flower.xyz"); - exchange.getRequest().getQueryParams().add("format", "json"); - + ServerWebExchange exchange = createExchange("/flower.xyz?format=json"); resolver.resolveMediaTypes(exchange); } @@ -121,11 +116,9 @@ public class CompositeContentTypeResolverBuilderTests { .mediaType("json", MediaType.APPLICATION_JSON) .build(); - ServerWebExchange exchange = createExchange("/flower"); - exchange.getRequest().getQueryParams().add("format", "json"); + ServerWebExchange exchange = createExchange("/flower?format=json"); - assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), - resolver.resolveMediaTypes(exchange)); + assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), resolver.resolveMediaTypes(exchange)); } @Test(expected = NotAcceptableStatusException.class) // SPR-10170 @@ -134,9 +127,7 @@ public class CompositeContentTypeResolverBuilderTests { .favorParameter(true) .build(); - ServerWebExchange exchange = createExchange("/flower"); - exchange.getRequest().getQueryParams().add("format", "xyz"); - + ServerWebExchange exchange = createExchange("/flower?format=xyz"); resolver.resolveMediaTypes(exchange); } @@ -146,8 +137,8 @@ public class CompositeContentTypeResolverBuilderTests { .ignoreAcceptHeader(true) .build(); - ServerWebExchange exchange = createExchange("/flower"); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.IMAGE_GIF)); + ServerHttpRequest request = MockServerHttpRequest.get("/flower").accept(MediaType.IMAGE_GIF).build(); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); assertEquals(Collections.emptyList(), resolver.resolveMediaTypes(exchange)); } @@ -158,15 +149,10 @@ public class CompositeContentTypeResolverBuilderTests { .defaultContentType(MediaType.APPLICATION_JSON) .build(); - ServerWebExchange exchange = createExchange("/"); + ServerHttpRequest request = MockServerHttpRequest.get("/").accept(MediaType.ALL).build(); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); - assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), - resolver.resolveMediaTypes(exchange)); - - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.ALL)); - - assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), - resolver.resolveMediaTypes(exchange)); + assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), resolver.resolveMediaTypes(exchange)); } @Test // SPR-12286 @@ -175,21 +161,20 @@ public class CompositeContentTypeResolverBuilderTests { .defaultContentTypeResolver(new FixedContentTypeResolver(MediaType.APPLICATION_JSON)) .build(); + List expected = Collections.singletonList(MediaType.APPLICATION_JSON); + ServerWebExchange exchange = createExchange("/"); + assertEquals(expected, resolver.resolveMediaTypes(exchange)); - assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), - resolver.resolveMediaTypes(exchange)); - - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.ALL)); - assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), - resolver.resolveMediaTypes(exchange)); + ServerHttpRequest request = MockServerHttpRequest.get("/").accept(MediaType.ALL).build(); + exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); + assertEquals(expected, resolver.resolveMediaTypes(exchange)); } - private ServerWebExchange createExchange(String path) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + private ServerWebExchange createExchange(String url) throws URISyntaxException { + ServerHttpRequest request = MockServerHttpRequest.get(url).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/HeaderContentTypeResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/HeaderContentTypeResolverTests.java index 7dbe4d85c8..c9c9e6a1a9 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/HeaderContentTypeResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/HeaderContentTypeResolverTests.java @@ -21,16 +21,13 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; @@ -70,12 +67,12 @@ public class HeaderContentTypeResolverTests { private ServerWebExchange createExchange(String accept) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - if (accept != null) { - request.getHeaders().add("Accept", accept); - } - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + + ServerHttpRequest request = (accept != null ? + MockServerHttpRequest.get("/").header("accept", accept).build() : + MockServerHttpRequest.get("/").build()); + + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolverTests.java index df3fdb86cd..8e0317fbac 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolverTests.java @@ -22,11 +22,10 @@ import java.util.Map; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; @@ -110,7 +109,7 @@ public class PathExtensionContentTypeResolverTests { private ServerWebExchange createExchange(String path) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path); + ServerHttpRequest request = MockServerHttpRequest.get(path).build(); WebSessionManager sessionManager = new MockWebSessionManager(); return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/ResourceHandlerRegistryTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/ResourceHandlerRegistryTests.java index e92644b5ba..7dfdff8d54 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/ResourceHandlerRegistryTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/ResourceHandlerRegistryTests.java @@ -30,7 +30,6 @@ import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.buffer.support.DataBufferTestUtils; import org.springframework.http.CacheControl; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.reactive.HandlerMapping; @@ -47,8 +46,6 @@ import org.springframework.web.reactive.resource.VersionResourceResolver; import org.springframework.web.reactive.resource.WebJarsResourceResolver; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -78,10 +75,9 @@ public class ResourceHandlerRegistryTests { this.registration = this.registry.addResourceHandler("/resources/**"); this.registration.addResourceLocations("classpath:org/springframework/web/reactive/config/"); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, ""); + MockServerHttpRequest request = MockServerHttpRequest.get("").build(); this.response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, this.response, manager); + this.exchange = new DefaultServerWebExchange(request, this.response); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupportTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupportTests.java index 767d112655..271526a635 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupportTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/config/WebReactiveConfigurationSupportTests.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; @@ -34,7 +35,6 @@ import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; import org.springframework.core.convert.ConversionService; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.EncoderHttpMessageWriter; @@ -63,13 +63,19 @@ import org.springframework.web.reactive.result.view.ViewResolutionResultHandler; import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import static org.junit.Assert.*; -import static org.springframework.http.MediaType.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM; +import static org.springframework.http.MediaType.APPLICATION_XML; +import static org.springframework.http.MediaType.IMAGE_PNG; +import static org.springframework.http.MediaType.TEXT_PLAIN; /** * Unit tests for {@link WebReactiveConfigurationSupport}. @@ -79,14 +85,10 @@ public class WebReactiveConfigurationSupportTests { private MockServerHttpRequest request; - private ServerWebExchange exchange; - @Before public void setUp() throws Exception { - this.request = new MockServerHttpRequest(HttpMethod.GET, "/"); - MockServerHttpResponse response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, response, new MockWebSessionManager()); + this.request = MockServerHttpRequest.get("/").build(); } @@ -108,12 +110,12 @@ public class WebReactiveConfigurationSupportTests { RequestedContentTypeResolver resolver = context.getBean(name, RequestedContentTypeResolver.class); assertSame(resolver, mapping.getContentTypeResolver()); - this.request.setUri("/path.json"); + this.request = MockServerHttpRequest.get("/path.json").build(); List list = Collections.singletonList(MediaType.APPLICATION_JSON); - assertEquals(list, resolver.resolveMediaTypes(this.exchange)); + assertEquals(list, resolver.resolveMediaTypes(createExchange())); - this.request.setUri("/path.xml"); - assertEquals(Collections.emptyList(), resolver.resolveMediaTypes(this.exchange)); + this.request = MockServerHttpRequest.get("/path.xml").build(); + assertEquals(Collections.emptyList(), resolver.resolveMediaTypes(createExchange())); } @Test @@ -267,6 +269,11 @@ public class WebReactiveConfigurationSupportTests { assertNotNull(webHandler); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + private void assertHasMessageReader(List> readers, Class clazz, MediaType mediaType) { ResolvableType type = ResolvableType.forClass(clazz); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyExtractorsTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyExtractorsTests.java index 1ade037330..5b360efc3b 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyExtractorsTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/BodyExtractorsTests.java @@ -47,9 +47,9 @@ import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.http.codec.xml.Jaxb2XmlDecoder; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; -import static org.springframework.http.codec.json.AbstractJackson2Codec.JSON_VIEW_HINT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.springframework.http.codec.json.AbstractJackson2Codec.JSON_VIEW_HINT; /** * @author Arjen Poutsma @@ -91,9 +91,7 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(body); - + MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body); Mono result = extractor.extract(request, this.context); StepVerifier.create(result) @@ -112,9 +110,9 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.getHeaders().setContentType(MediaType.APPLICATION_JSON); - request.setBody(body); + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .contentType(MediaType.APPLICATION_JSON) + .body(body); Mono result = extractor.extract(request, this.context); @@ -136,9 +134,7 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(body); - + MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body); Flux result = extractor.extract(request, this.context); StepVerifier.create(result) @@ -157,9 +153,9 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.getHeaders().setContentType(MediaType.APPLICATION_JSON); - request.setBody(body); + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .contentType(MediaType.APPLICATION_JSON) + .body(body); Flux result = extractor.extract(request, this.context); @@ -185,9 +181,9 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.getHeaders().setContentType(MediaType.APPLICATION_JSON); - request.setBody(body); + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .contentType(MediaType.APPLICATION_JSON) + .body(body); BodyExtractor.Context emptyContext = new BodyExtractor.Context() { @Override @@ -215,9 +211,7 @@ public class BodyExtractorsTests { factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux body = Flux.just(dataBuffer); - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(body); - + MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body); Flux result = extractor.extract(request, this.context); StepVerifier.create(result) @@ -229,6 +223,7 @@ public class BodyExtractorsTests { interface SafeToDeserialize {} + @SuppressWarnings("unused") private static class User { @JsonView(SafeToDeserialize.class) diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java index 79954169df..a32dbd5391 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java @@ -36,7 +36,7 @@ import org.springframework.http.MediaType; import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter; -import org.springframework.web.client.reactive.test.MockClientHttpRequest; +import org.springframework.mock.http.client.reactive.test.MockClientHttpRequest; import org.springframework.web.reactive.function.BodyInserter; import static java.nio.charset.StandardCharsets.UTF_8; @@ -171,7 +171,7 @@ public class DefaultClientRequestBuilderTests { .cookie("foo", "bar") .build(); - MockClientHttpRequest request = new MockClientHttpRequest(); + MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/"); WebClientStrategies strategies = mock(WebClientStrategies.class); result.writeTo(request, strategies).block(); @@ -196,14 +196,13 @@ public class DefaultClientRequestBuilderTests { ClientRequest result = ClientRequest.POST("http://example.com") .body(inserter); - MockClientHttpRequest request = new MockClientHttpRequest(); - List> messageWriters = new ArrayList<>(); - messageWriters.add(new EncoderHttpMessageWriter(new CharSequenceEncoder())); + messageWriters.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())); WebClientStrategies strategies = mock(WebClientStrategies.class); when(strategies.messageWriters()).thenReturn(messageWriters::stream); + MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/"); result.writeTo(request, strategies).block(); assertNotNull(request.getBody()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java index 5d7697c8e8..9e0c17d6a1 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java @@ -51,8 +51,7 @@ public class ResourceHandlerFunctionTests { @Test public void get() throws IOException { - MockServerHttpRequest mockRequest = - new MockServerHttpRequest(HttpMethod.GET, "http://localhost"); + MockServerHttpRequest mockRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse mockResponse = new MockServerHttpResponse(); ServerWebExchange exchange = new DefaultServerWebExchange(mockRequest, mockResponse, new MockWebSessionManager()); @@ -93,8 +92,7 @@ TODO: enable when ServerEntityResponse is reintroduced @Test public void head() throws IOException { - MockServerHttpRequest mockRequest = - new MockServerHttpRequest(HttpMethod.HEAD, "http://localhost"); + MockServerHttpRequest mockRequest = MockServerHttpRequest.head("http://localhost").build(); MockServerHttpResponse mockResponse = new MockServerHttpResponse(); ServerWebExchange exchange = new DefaultServerWebExchange(mockRequest, mockResponse, new MockWebSessionManager()); @@ -123,8 +121,7 @@ TODO: enable when ServerEntityResponse is reintroduced @Test public void options() { - MockServerHttpRequest mockRequest = - new MockServerHttpRequest(HttpMethod.OPTIONS, "http://localhost"); + MockServerHttpRequest mockRequest = MockServerHttpRequest.options("http://localhost").build(); MockServerHttpResponse mockResponse = new MockServerHttpResponse(); ServerWebExchange exchange = new DefaultServerWebExchange(mockRequest, mockResponse, new MockWebSessionManager()); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java index fc80c41ccd..ed8ef6e28f 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionsTests.java @@ -22,7 +22,6 @@ import org.junit.Test; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import org.springframework.http.HttpMethod; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.server.reactive.HttpHandler; @@ -145,8 +144,7 @@ public class RouterFunctionsTests { HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, strategies); assertNotNull(result); - MockServerHttpRequest httpRequest = - new MockServerHttpRequest(HttpMethod.GET, "http://localhost"); + MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse serverHttpResponse = new MockServerHttpResponse(); result.handle(httpRequest, serverHttpResponse); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/CorsUrlHandlerMappingTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/CorsUrlHandlerMappingTests.java index 05f0ad92aa..6b558d506c 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/CorsUrlHandlerMappingTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/CorsUrlHandlerMappingTests.java @@ -17,8 +17,6 @@ package org.springframework.web.reactive.handler; import java.util.Collections; -import static org.junit.Assert.*; -import static org.junit.Assert.assertSame; import org.junit.Before; import org.junit.Test; @@ -31,8 +29,12 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.reactive.CorsConfigurationSource; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; /** * Unit tests for CORS support at {@link AbstractUrlHandlerMapping} level. @@ -135,12 +137,12 @@ public class CorsUrlHandlerMappingTests { private ServerWebExchange createExchange(HttpMethod method, String path, String origin, String accessControlRequestMethod) { - ServerHttpRequest request = new MockServerHttpRequest(method, "http://localhost" + path); - request.getHeaders().add(HttpHeaders.ORIGIN, origin); - request.getHeaders().add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, accessControlRequestMethod); - MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, sessionManager); + ServerHttpRequest request = MockServerHttpRequest + .method(method, "http://localhost" + path) + .header("Origin", origin) + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, accessControlRequestMethod) + .build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/SimpleUrlHandlerMappingTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/SimpleUrlHandlerMappingTests.java index 0626775e2a..e2d08af54f 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/SimpleUrlHandlerMappingTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/handler/SimpleUrlHandlerMappingTests.java @@ -15,6 +15,7 @@ */ package org.springframework.web.reactive.handler; +import java.net.URI; import java.net.URISyntaxException; import org.junit.Test; @@ -24,14 +25,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -103,9 +102,7 @@ public class SimpleUrlHandlerMappingTests { } - private void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) - throws URISyntaxException { - + private void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) { ServerWebExchange exchange = createExchange(url); Object actual = handlerMapping.getHandler(exchange).block(); if (bean != null) { @@ -119,10 +116,9 @@ public class SimpleUrlHandlerMappingTests { } } - private ServerWebExchange createExchange(String path) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + private ServerWebExchange createExchange(String path) { + ServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, URI.create(path)).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/AppCacheManifestTransformerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/AppCacheManifestTransformerTests.java index 30104fd342..a75140a5af 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/AppCacheManifestTransformerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/AppCacheManifestTransformerTests.java @@ -29,14 +29,11 @@ import reactor.core.publisher.Mono; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpMethod; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.util.FileCopyUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -44,9 +41,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.mock; /** - * Unit tests for - * {@link AppCacheManifestTransformer}. - * + * Unit tests for {@link AppCacheManifestTransformer}. * @author Rossen Stoyanchev * @author Brian Clozel */ @@ -56,8 +51,6 @@ public class AppCacheManifestTransformerTests { private ResourceTransformerChain chain; - private ServerWebExchange exchange; - @Before public void setup() { @@ -75,7 +68,7 @@ public class AppCacheManifestTransformerTests { CssLinkResourceTransformer cssLinkResourceTransformer = new CssLinkResourceTransformer(); cssLinkResourceTransformer.setResourceUrlProvider(resourceUrlProvider); - List transformers = Arrays.asList(cssLinkResourceTransformer); + List transformers = Collections.singletonList(cssLinkResourceTransformer); this.chain = new DefaultResourceTransformerChain(resolverChain, transformers); this.transformer = new AppCacheManifestTransformer(); this.transformer.setResourceUrlProvider(resourceUrlProvider); @@ -88,30 +81,30 @@ public class AppCacheManifestTransformerTests { @Test public void noTransformIfExtensionNoMatch() throws Exception { - initExchange(HttpMethod.GET, "/static/foobar.file"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/foobar.file"); this.chain = mock(ResourceTransformerChain.class); Resource resource = mock(Resource.class); given(resource.getFilename()).willReturn("foobar.file"); - given(this.chain.transform(this.exchange, resource)).willReturn(Mono.just(resource)); + given(this.chain.transform(exchange, resource)).willReturn(Mono.just(resource)); - Resource result = this.transformer.transform(this.exchange, resource, this.chain).blockMillis(5000); + Resource result = this.transformer.transform(exchange, resource, this.chain).blockMillis(5000); assertEquals(resource, result); } @Test public void syntaxErrorInManifest() throws Exception { - initExchange(HttpMethod.GET, "/static/error.appcache"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/error.appcache"); this.chain = mock(ResourceTransformerChain.class); Resource resource = new ClassPathResource("test/error.appcache", getClass()); - given(this.chain.transform(this.exchange, resource)).willReturn(Mono.just(resource)); + given(this.chain.transform(exchange, resource)).willReturn(Mono.just(resource)); - Resource result = this.transformer.transform(this.exchange, resource, this.chain).blockMillis(5000); + Resource result = this.transformer.transform(exchange, resource, this.chain).blockMillis(5000); assertEquals(resource, result); } @Test public void transformManifest() throws Exception { - initExchange(HttpMethod.GET, "/static/test.appcache"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/test.appcache"); VersionResourceResolver versionResolver = new VersionResourceResolver(); versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy())); @@ -126,7 +119,7 @@ public class AppCacheManifestTransformerTests { this.chain = new DefaultResourceTransformerChain(resolverChain, transformers); Resource resource = new ClassPathResource("test/test.appcache", getClass()); - Resource result = this.transformer.transform(this.exchange, resource, this.chain).blockMillis(5000); + Resource result = this.transformer.transform(exchange, resource, this.chain).blockMillis(5000); byte[] bytes = FileCopyUtils.copyToByteArray(result.getInputStream()); String content = new String(bytes, "UTF-8"); @@ -146,10 +139,8 @@ public class AppCacheManifestTransformerTests { Matchers.containsString("# Hash: 4bf0338bcbeb0a5b3a4ec9ed8864107d")); } - private void initExchange(HttpMethod method, String url) { - MockServerHttpRequest request = new MockServerHttpRequest(method, url); - ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + private ServerWebExchange createExchange(HttpMethod method, String url) { + MockServerHttpRequest request = MockServerHttpRequest.method(method, url).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java index 222e719685..b4f470623d 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java @@ -19,6 +19,7 @@ package org.springframework.web.reactive.resource; import java.util.ArrayList; import java.util.List; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -27,14 +28,9 @@ import org.springframework.cache.Cache; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -52,8 +48,6 @@ public class CachingResourceResolverTests { private List locations; - private ServerWebExchange exchange; - private MockServerHttpRequest request; @@ -70,10 +64,7 @@ public class CachingResourceResolverTests { this.locations = new ArrayList<>(); this.locations.add(new ClassPathResource("test/", getClass())); - this.request = new MockServerHttpRequest(HttpMethod.GET, ""); - ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.request = MockServerHttpRequest.get("").build(); } @@ -81,7 +72,7 @@ public class CachingResourceResolverTests { public void resolveResourceInternal() { String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); - Resource actual = this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource actual = this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000); assertEquals(expected, actual); } @@ -93,14 +84,14 @@ public class CachingResourceResolverTests { this.cache.put(CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + "bar.css", expected); String file = "bar.css"; - Resource actual = this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource actual = this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000); assertSame(expected, actual); } @Test public void resolveResourceInternalNoMatch() { - assertNull(this.chain.resolveResource(this.exchange, "invalid.css", this.locations).blockMillis(5000)); + assertNull(this.chain.resolveResource(createExchange(), "invalid.css", this.locations).blockMillis(5000)); } @Test @@ -128,9 +119,9 @@ public class CachingResourceResolverTests { @Test public void resolveResourceAcceptEncodingInCacheKey() { String file = "bar.css"; - this.request.setUri(file).setHeader("Accept-Encoding", "gzip"); + this.request = MockServerHttpRequest.get(file).header("Accept-Encoding", "gzip").build(); - Resource expected = this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource expected = this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000); String cacheKey = CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + file + "+encoding=gzip"; assertEquals(expected, this.cache.get(cacheKey).get()); @@ -139,9 +130,9 @@ public class CachingResourceResolverTests { @Test public void resolveResourceNoAcceptEncodingInCacheKey() { String file = "bar.css"; - this.request.setUri(file); + this.request = MockServerHttpRequest.get(file).build(); - Resource expected = this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource expected = this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000); String cacheKey = CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + file; assertEquals(expected, this.cache.get(cacheKey).get()); @@ -155,11 +146,16 @@ public class CachingResourceResolverTests { this.cache.put(CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + "bar.css+encoding=gzip", gzResource); String file = "bar.css"; - this.request.setUri(file); - assertSame(resource, this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000)); + this.request = MockServerHttpRequest.get(file).build(); + assertSame(resource, this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000)); - request.addHeader("Accept-Encoding", "gzip"); - assertSame(gzResource, this.chain.resolveResource(this.exchange, file, this.locations).blockMillis(5000)); + this.request = MockServerHttpRequest.get(file).header("Accept-Encoding", "gzip").build(); + assertSame(gzResource, this.chain.resolveResource(createExchange(), file, this.locations).blockMillis(5000)); + } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java index 49bbc52917..e3239f1ffe 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java @@ -39,8 +39,6 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; @@ -53,8 +51,6 @@ public class CssLinkResourceTransformerTests { private ResourceTransformerChain transformerChain; - private ServerWebExchange exchange; - @Before public void setUp() { @@ -84,7 +80,7 @@ public class CssLinkResourceTransformerTests { @Test public void transform() throws Exception { - initExchange(HttpMethod.GET, "/static/main.css"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/main.css"); Resource css = new ClassPathResource("test/main.css", getClass()); String expected = "\n" + @@ -95,7 +91,7 @@ public class CssLinkResourceTransformerTests { "@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';\n\n" + "body { background: url(\"/static/images/image-f448cd1d5dba82b774f3202c878230b3.png\") }\n"; - StepVerifier.create(this.transformerChain.transform(this.exchange, css).cast(TransformedResource.class)) + StepVerifier.create(this.transformerChain.transform(exchange, css).cast(TransformedResource.class)) .consumeNextWith(resource -> { String result = new String(resource.getByteArray(), StandardCharsets.UTF_8); result = StringUtils.deleteAny(result, "\r"); @@ -106,24 +102,22 @@ public class CssLinkResourceTransformerTests { @Test public void transformNoLinks() throws Exception { - initExchange(HttpMethod.GET, "/static/foo.css"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/foo.css"); Resource expected = new ClassPathResource("test/foo.css", getClass()); - StepVerifier.create(this.transformerChain.transform(this.exchange, expected)) - .consumeNextWith(resource -> { - assertSame(expected, resource); - }) + StepVerifier.create(this.transformerChain.transform(exchange, expected)) + .consumeNextWith(resource -> assertSame(expected, resource)) .expectComplete().verify(); } @Test public void transformExtLinksNotAllowed() throws Exception { - initExchange(HttpMethod.GET, "/static/external.css"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/external.css"); ResourceResolverChain resolverChain = Mockito.mock(DefaultResourceResolverChain.class); ResourceTransformerChain transformerChain = new DefaultResourceTransformerChain(resolverChain, Collections.singletonList(new CssLinkResourceTransformer())); Resource externalCss = new ClassPathResource("test/external.css", getClass()); - StepVerifier.create(transformerChain.transform(this.exchange, externalCss).cast(TransformedResource.class)) + StepVerifier.create(transformerChain.transform(exchange, externalCss).cast(TransformedResource.class)) .consumeNextWith(resource -> { String expected = "@import url(\"http://example.org/fonts/css\");\n" + "body { background: url(\"file:///home/spring/image.png\") }\n" + @@ -143,20 +137,20 @@ public class CssLinkResourceTransformerTests { @Test public void transformWithNonCssResource() throws Exception { - initExchange(HttpMethod.GET, "/static/images/image.png"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/images/image.png"); Resource expected = new ClassPathResource("test/images/image.png", getClass()); - StepVerifier.create(this.transformerChain.transform(this.exchange, expected)) + StepVerifier.create(this.transformerChain.transform(exchange, expected)) .expectNext(expected) .expectComplete().verify(); } @Test public void transformWithGzippedResource() throws Exception { - initExchange(HttpMethod.GET, "/static/main.css"); + ServerWebExchange exchange = createExchange(HttpMethod.GET, "/static/main.css"); Resource original = new ClassPathResource("test/main.css", getClass()); createTempCopy("main.css", "main.css.gz"); GzipResourceResolver.GzippedResource expected = new GzipResourceResolver.GzippedResource(original); - StepVerifier.create(this.transformerChain.transform(this.exchange, expected)) + StepVerifier.create(this.transformerChain.transform(exchange, expected)) .expectNext(expected) .expectComplete().verify(); } @@ -170,11 +164,10 @@ public class CssLinkResourceTransformerTests { copy.toFile().deleteOnExit(); } - private void initExchange(HttpMethod method, String url) { - MockServerHttpRequest request = new MockServerHttpRequest(method, url); + private ServerWebExchange createExchange(HttpMethod method, String url) { + MockServerHttpRequest request = MockServerHttpRequest.method(method, url).build(); ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + return new DefaultServerWebExchange(request, response); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/GzipResourceResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/GzipResourceResolverTests.java index 977cf18426..e7d32074e7 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/GzipResourceResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/GzipResourceResolverTests.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.zip.GZIPOutputStream; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -37,15 +38,10 @@ import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.util.FileCopyUtils; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -64,8 +60,6 @@ public class GzipResourceResolverTests { private Cache cache; - private ServerWebExchange exchange; - private MockServerHttpRequest request; @@ -106,18 +100,16 @@ public class GzipResourceResolverTests { this.locations.add(new ClassPathResource("test/", getClass())); this.locations.add(new ClassPathResource("testalternatepath/", getClass())); - this.request = new MockServerHttpRequest(HttpMethod.GET, ""); - ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.request = MockServerHttpRequest.get("").build(); } @Test public void resolveGzippedFile() throws IOException { - this.request.addHeader("Accept-Encoding", "gzip"); + this.request = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").build(); + String file = "js/foo.js"; - Resource resolved = this.resolver.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource resolved = this.resolver.resolveResource(createExchange(), file, this.locations).blockMillis(5000); String gzFile = file+".gz"; Resource resource = new ClassPathResource("test/" + gzFile, getClass()); @@ -129,9 +121,10 @@ public class GzipResourceResolverTests { @Test public void resolveFingerprintedGzippedFile() throws IOException { - this.request.addHeader("Accept-Encoding", "gzip"); + this.request = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").build(); + String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css"; - Resource resolved = this.resolver.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource resolved = this.resolver.resolveResource(createExchange(), file, this.locations).blockMillis(5000); String gzFile = file + ".gz"; Resource resource = new ClassPathResource("test/" + gzFile, getClass()); @@ -143,9 +136,10 @@ public class GzipResourceResolverTests { @Test public void resolveFromCacheWithEncodingVariants() throws IOException { - this.request.addHeader("Accept-Encoding", "gzip"); + this.request = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").build(); + String file = "js/foo.js"; - Resource resolved = this.resolver.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + Resource resolved = this.resolver.resolveResource(createExchange(), file, this.locations).blockMillis(5000); String gzFile = file+".gz"; Resource gzResource = new ClassPathResource("test/"+gzFile, getClass()); @@ -156,11 +150,8 @@ public class GzipResourceResolverTests { // resolved resource is now cached in CachingResourceResolver - this.request = new MockServerHttpRequest(HttpMethod.GET, "/js/foo.js"); - MockServerHttpResponse response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, response, new DefaultWebSessionManager()); - - resolved = this.resolver.resolveResource(this.exchange, file, this.locations).blockMillis(5000); + this.request = MockServerHttpRequest.get("/js/foo.js").build(); + resolved = this.resolver.resolveResource(createExchange(), file, this.locations).blockMillis(5000); Resource resource = new ClassPathResource("test/"+file, getClass()); assertEquals(resource.getDescription(), resolved.getDescription()); @@ -181,4 +172,9 @@ public class GzipResourceResolverTests { assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource); } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceTransformerSupportTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceTransformerSupportTests.java index a2acb42383..89300883e7 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceTransformerSupportTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceTransformerSupportTests.java @@ -19,20 +19,17 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; @@ -48,8 +45,6 @@ public class ResourceTransformerSupportTests { private TestResourceTransformerSupport transformer; - private ServerWebExchange exchange; - private MockServerHttpRequest request; @@ -65,12 +60,10 @@ public class ResourceTransformerSupportTests { this.transformer = new TestResourceTransformerSupport(); this.transformer.setResourceUrlProvider(createResourceUrlProvider(resolvers)); - this.request = new MockServerHttpRequest(HttpMethod.GET, ""); - ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, response, manager); + this.request = MockServerHttpRequest.get("").build(); } + private ResourceUrlProvider createResourceUrlProvider(List resolvers) { ResourceWebHandler handler = new ResourceWebHandler(); handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass()))); @@ -83,11 +76,11 @@ public class ResourceTransformerSupportTests { @Test public void resolveUrlPath() throws Exception { - this.request.setUri("/resources/main.css"); + this.request = MockServerHttpRequest.get("/resources/main.css").build(); String resourcePath = "/resources/bar.css"; Resource css = new ClassPathResource("test/main.css", getClass()); String actual = this.transformer.resolveUrlPath( - resourcePath, this.exchange, css, this.transformerChain).blockMillis(5000); + resourcePath, createExchange(), css, this.transformerChain).blockMillis(5000); assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual); assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual); @@ -97,7 +90,7 @@ public class ResourceTransformerSupportTests { public void resolveUrlPathWithRelativePath() throws Exception { Resource css = new ClassPathResource("test/main.css", getClass()); String actual = this.transformer.resolveUrlPath( - "bar.css", this.exchange, css, this.transformerChain).blockMillis(5000); + "bar.css", createExchange(), css, this.transformerChain).blockMillis(5000); assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual); } @@ -106,11 +99,16 @@ public class ResourceTransformerSupportTests { public void resolveUrlPathWithRelativePathInParentDirectory() throws Exception { Resource imagePng = new ClassPathResource("test/images/image.png", getClass()); String actual = this.transformer.resolveUrlPath( - "../bar.css", this.exchange, imagePng, this.transformerChain).blockMillis(5000); + "../bar.css", createExchange(), imagePng, this.transformerChain).blockMillis(5000); assertEquals("../bar-11e16cf79faee7ac698c805cf28248d2.css", actual); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + private static class TestResourceTransformerSupport extends ResourceTransformerSupport { diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceUrlProviderTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceUrlProviderTests.java index f42bb13f27..8e841ce004 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceUrlProviderTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceUrlProviderTests.java @@ -29,7 +29,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.mock.web.test.MockServletContext; @@ -80,10 +79,9 @@ public class ResourceUrlProviderTests { @Test // SPR-13374 public void getStaticResourceUrlRequestWithQueryOrHash() { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); + ServerWebExchange exchange = new DefaultServerWebExchange(request, response); String url = "/resources/foo.css?foo=bar&url=http://example.org"; String resolvedUrl = this.urlProvider.getForRequestUrl(exchange, url).blockMillis(5000); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java index d2c29f0748..ad435021a2 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Flux; @@ -51,8 +52,6 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuild import org.springframework.web.server.MethodNotAllowedException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -70,13 +69,9 @@ public class ResourceWebHandlerTests { private ResourceWebHandler handler; - private ServerWebExchange exchange; + private MockServerHttpRequest request; - private MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, ""); - - private MockServerHttpResponse response = new MockServerHttpResponse(); - - private WebSessionManager sessionManager = new DefaultWebSessionManager(); + private MockServerHttpResponse response; private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); @@ -94,14 +89,15 @@ public class ResourceWebHandlerTests { this.handler.afterPropertiesSet(); this.handler.afterSingletonsInstantiated(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); + this.request = MockServerHttpRequest.get("").build(); + this.response = new MockServerHttpResponse(); } @Test public void getResource() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); HttpHeaders headers = this.response.getHeaders(); assertEquals(MediaType.parseMediaType("text/css"), headers.getContentType()); @@ -116,9 +112,9 @@ public class ResourceWebHandlerTests { @Test public void getResourceHttpHeader() throws Exception { - this.request.setHttpMethod(HttpMethod.HEAD); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.head("").build(); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertNull(this.response.getStatusCode()); HttpHeaders headers = this.response.getHeaders(); @@ -134,9 +130,9 @@ public class ResourceWebHandlerTests { @Test public void getResourceHttpOptions() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.options("").build(); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertNull(this.response.getStatusCode()); assertEquals("GET,HEAD,OPTIONS", this.response.getHeaders().getFirst("Allow")); @@ -144,9 +140,9 @@ public class ResourceWebHandlerTests { @Test public void getResourceNoCache() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); + ServerWebExchange exchange = createExchange("foo.css"); this.handler.setCacheControl(CacheControl.noStore()); - this.handler.handle(this.exchange).blockMillis(5000); + this.handler.handle(exchange).blockMillis(5000); assertEquals("no-store", this.response.getHeaders().getCacheControl()); assertTrue(this.response.getHeaders().containsKey("Last-Modified")); @@ -163,8 +159,8 @@ public class ResourceWebHandlerTests { this.handler.afterPropertiesSet(); this.handler.afterSingletonsInstantiated(); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("versionString/foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertEquals("\"versionString\"", this.response.getHeaders().getETag()); assertEquals("bytes", this.response.getHeaders().getFirst("Accept-Ranges")); @@ -173,8 +169,8 @@ public class ResourceWebHandlerTests { @Test public void getResourceWithHtmlMediaType() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("foo.html"); + this.handler.handle(exchange).blockMillis(5000); HttpHeaders headers = this.response.getHeaders(); assertEquals(MediaType.TEXT_HTML, headers.getContentType()); @@ -187,8 +183,8 @@ public class ResourceWebHandlerTests { @Test public void getResourceFromAlternatePath() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "baz.css"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("baz.css"); + this.handler.handle(exchange).blockMillis(5000); HttpHeaders headers = this.response.getHeaders(); assertEquals(MediaType.parseMediaType("text/css"), headers.getContentType()); @@ -203,8 +199,8 @@ public class ResourceWebHandlerTests { @Test public void getResourceFromSubDirectory() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/foo.js"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("js/foo.js"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(MediaType.parseMediaType("text/javascript"), this.response.getHeaders().getContentType()); assertResponseBody("function foo() { console.log(\"hello world\"); }"); @@ -212,8 +208,8 @@ public class ResourceWebHandlerTests { @Test public void getResourceFromSubDirectoryOfAlternatePath() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/baz.js"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("js/baz.js"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(MediaType.parseMediaType("text/javascript"), this.response.getHeaders().getContentType()); assertResponseBody("function foo() { console.log(\"hello world\"); }"); @@ -232,8 +228,8 @@ public class ResourceWebHandlerTests { handler.afterPropertiesSet(); handler.afterSingletonsInstantiated(); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("foo.css"); + handler.handle(exchange).blockMillis(5000); assertEquals(MediaType.parseMediaType("foo/bar"), this.response.getHeaders().getContentType()); assertResponseBody("h1 { color:red; }"); @@ -252,9 +248,9 @@ public class ResourceWebHandlerTests { handler.afterPropertiesSet(); handler.afterSingletonsInstantiated(); - this.request.addHeader("Accept", "application/json,text/plain,*/*"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); - handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("Accept", "application/json,text/plain,*/*").build(); + ServerWebExchange exchange = createExchange("foo.html"); + handler.handle(exchange).blockMillis(5000); assertEquals(MediaType.TEXT_HTML, this.response.getHeaders().getContentType()); } @@ -262,47 +258,41 @@ public class ResourceWebHandlerTests { @Test public void invalidPath() throws Exception { for (HttpMethod method : HttpMethod.values()) { - this.request = new MockServerHttpRequest(HttpMethod.GET, ""); - this.response = new MockServerHttpResponse(); - this.sessionManager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); testInvalidPath(method); } } private void testInvalidPath(HttpMethod httpMethod) throws Exception { - this.request.setHttpMethod(httpMethod); - Resource location = new ClassPathResource("test/", getClass()); this.handler.setLocations(Collections.singletonList(location)); - testInvalidPath(location, "../testsecret/secret.txt"); - testInvalidPath(location, "test/../../testsecret/secret.txt"); - testInvalidPath(location, ":/../../testsecret/secret.txt"); + testInvalidPath(httpMethod, "../testsecret/secret.txt", location); + testInvalidPath(httpMethod, "test/../../testsecret/secret.txt", location); + testInvalidPath(httpMethod, ":/../../testsecret/secret.txt", location); location = new UrlResource(getClass().getResource("./test/")); this.handler.setLocations(Collections.singletonList(location)); Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt")); String secretPath = secretResource.getURL().getPath(); - testInvalidPath(location, "file:" + secretPath); - testInvalidPath(location, "/file:" + secretPath); - testInvalidPath(location, "url:" + secretPath); - testInvalidPath(location, "/url:" + secretPath); - testInvalidPath(location, "////../.." + secretPath); - testInvalidPath(location, "/%2E%2E/testsecret/secret.txt"); - testInvalidPath(location, "url:" + secretPath); + testInvalidPath(httpMethod, "file:" + secretPath, location); + testInvalidPath(httpMethod, "/file:" + secretPath, location); + testInvalidPath(httpMethod, "url:" + secretPath, location); + testInvalidPath(httpMethod, "/url:" + secretPath, location); + testInvalidPath(httpMethod, "////../.." + secretPath, location); + testInvalidPath(httpMethod, "/%2E%2E/testsecret/secret.txt", location); + testInvalidPath(httpMethod, "url:" + secretPath, location); // The following tests fail with a MalformedURLException on Windows // testInvalidPath(location, "/" + secretPath); // testInvalidPath(location, "/ " + secretPath); } - private void testInvalidPath(Resource location, String requestPath) throws Exception { + private void testInvalidPath(HttpMethod httpMethod, String requestPath, Resource location) throws Exception { + this.request = MockServerHttpRequest.method(httpMethod, "").build(); this.response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange(requestPath); + this.handler.handle(exchange).blockMillis(5000); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } @@ -311,10 +301,8 @@ public class ResourceWebHandlerTests { @Test public void ignoreInvalidEscapeSequence() throws Exception { - this.response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/%foo%/bar.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("/%foo%/bar.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); } @@ -379,17 +367,18 @@ public class ResourceWebHandlerTests { @Test public void notModified() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.request.getHeaders().setIfModifiedSince(resourceLastModified("test/foo.css")); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").ifModifiedSince(resourceLastModified("test/foo.css")).build(); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.NOT_MODIFIED, this.response.getStatusCode()); } @Test public void modified() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.request.getHeaders().setIfModifiedSince(resourceLastModified("test/foo.css") / 1000 * 1000 - 1); - this.handler.handle(this.exchange).blockMillis(5000); + long timestamp = resourceLastModified("test/foo.css") / 1000 * 1000 - 1; + this.request = MockServerHttpRequest.get("").ifModifiedSince(timestamp).build(); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertNull(this.response.getStatusCode()); assertResponseBody("h1 { color:red; }"); @@ -397,15 +386,15 @@ public class ResourceWebHandlerTests { @Test public void directory() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("js/"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); } @Test public void directoryInJarFile() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "underscorejs/"); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("underscorejs/"); + this.handler.handle(exchange).blockMillis(5000); assertNull(this.response.getStatusCode()); assertEquals(0, this.response.getHeaders().getContentLength()); @@ -413,45 +402,44 @@ public class ResourceWebHandlerTests { @Test public void missingResourcePath() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, ""); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange(""); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); } @Test(expected = IllegalStateException.class) public void noPathWithinHandlerMappingAttribute() throws Exception { - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = new DefaultServerWebExchange(this.request, this.response); + this.handler.handle(exchange).blockMillis(5000); } @Test(expected = MethodNotAllowedException.class) public void unsupportedHttpMethod() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); - this.request.setHttpMethod(HttpMethod.POST); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.post("").build(); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); } @Test public void resourceNotFound() throws Exception { for (HttpMethod method : HttpMethod.values()) { - this.request = new MockServerHttpRequest(HttpMethod.GET, ""); - this.response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, this.sessionManager); resourceNotFound(method); } } private void resourceNotFound(HttpMethod httpMethod) throws Exception { - this.request.setHttpMethod(httpMethod); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "not-there.css"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.method(httpMethod, "").build(); + this.response = new MockServerHttpResponse(); + ServerWebExchange exchange = createExchange("not-there.css"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); } @Test public void partialContentByteRange() throws Exception { - this.request.addHeader("Range", "bytes=0-1"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("Range", "bytes=0-1").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, this.response.getHeaders().getContentType()); @@ -464,9 +452,9 @@ public class ResourceWebHandlerTests { @Test public void partialContentByteRangeNoEnd() throws Exception { - this.request.addHeader("Range", "bytes=9-"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("range", "bytes=9-").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, this.response.getHeaders().getContentType()); @@ -479,9 +467,9 @@ public class ResourceWebHandlerTests { @Test public void partialContentByteRangeLargeEnd() throws Exception { - this.request.addHeader("Range", "bytes=9-10000"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("range", "bytes=9-10000").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, this.response.getHeaders().getContentType()); @@ -494,9 +482,9 @@ public class ResourceWebHandlerTests { @Test public void partialContentSuffixRange() throws Exception { - this.request.addHeader("Range", "bytes=-1"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("range", "bytes=-1").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, this.response.getHeaders().getContentType()); @@ -509,9 +497,9 @@ public class ResourceWebHandlerTests { @Test public void partialContentSuffixRangeLargeSuffix() throws Exception { - this.request.addHeader("Range", "bytes=-11"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("range", "bytes=-11").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, this.response.getHeaders().getContentType()); @@ -524,10 +512,10 @@ public class ResourceWebHandlerTests { @Test public void partialContentInvalidRangeHeader() throws Exception { - this.request.addHeader("Range", "bytes= foo bar"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); + this.request = MockServerHttpRequest.get("").header("range", "bytes=foo bar").build(); + ServerWebExchange exchange = createExchange("foo.txt"); - StepVerifier.create(this.handler.handle(this.exchange)) + StepVerifier.create(this.handler.handle(exchange)) .expectNextCount(0) .expectComplete() .verify(); @@ -538,9 +526,9 @@ public class ResourceWebHandlerTests { @Test public void partialContentMultipleByteRanges() throws Exception { - this.request.addHeader("Range", "bytes=0-1, 4-5, 8-9"); - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); - this.handler.handle(this.exchange).blockMillis(5000); + this.request = MockServerHttpRequest.get("").header("Range", "bytes=0-1, 4-5, 8-9").build(); + ServerWebExchange exchange = createExchange("foo.txt"); + this.handler.handle(exchange).blockMillis(5000); assertEquals(HttpStatus.PARTIAL_CONTENT, this.response.getStatusCode()); assertTrue(this.response.getHeaders().getContentType().toString() @@ -581,13 +569,20 @@ public class ResourceWebHandlerTests { @Test // SPR-14005 public void doOverwriteExistingCacheControlHeaders() throws Exception { - this.exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.response.getHeaders().setCacheControl(CacheControl.noStore().getHeaderValue()); - this.handler.handle(this.exchange).blockMillis(5000); + ServerWebExchange exchange = createExchange("foo.css"); + this.handler.handle(exchange).blockMillis(5000); assertEquals("max-age=3600", this.response.getHeaders().getCacheControl()); } + @NotNull + private ServerWebExchange createExchange(String path) { + ServerWebExchange exchange = new DefaultServerWebExchange(this.request, this.response); + exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, path); + return exchange; + } + private long resourceLastModified(String resourceName) throws IOException { return new ClassPathResource(resourceName, getClass()).getFile().lastModified(); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/VersionResourceResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/VersionResourceResolverTests.java index 397744f57f..98dddb291e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/VersionResourceResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/VersionResourceResolverTests.java @@ -28,12 +28,10 @@ import reactor.core.publisher.Mono; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -165,10 +163,9 @@ public class VersionResourceResolverTests { String version = "version"; String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/resources/bar-version.css"); + MockServerHttpRequest request = MockServerHttpRequest.get("/resources/bar-version.css").build(); MockServerHttpResponse response = new MockServerHttpResponse(); - DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); - ServerWebExchange exchange = new DefaultServerWebExchange(request, response, sessionManager); + ServerWebExchange exchange = new DefaultServerWebExchange(request, response); given(this.chain.resolveResource(exchange, versionFile, this.locations)).willReturn(Mono.empty()); given(this.chain.resolveResource(exchange, file, this.locations)).willReturn(Mono.just(expected)); given(this.versionStrategy.extractVersion(versionFile)).willReturn(version); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/WebJarsResourceResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/WebJarsResourceResolverTests.java index 084c10f641..0ec2db9345 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/WebJarsResourceResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/resource/WebJarsResourceResolverTests.java @@ -24,14 +24,11 @@ import reactor.core.publisher.Mono; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static java.util.Collections.singletonList; import static org.junit.Assert.assertEquals; @@ -66,10 +63,9 @@ public class WebJarsResourceResolverTests { this.resolver = new WebJarsResourceResolver(); this.chain = mock(ResourceResolverChain.class); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, ""); + MockServerHttpRequest request = MockServerHttpRequest.get("").build(); ServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.exchange = new DefaultServerWebExchange(request, response); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java index d24f1a99a2..b0c5580bee 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/HandlerResultHandlerTests.java @@ -15,16 +15,14 @@ */ package org.springframework.web.reactive.result; -import java.net.URI; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Set; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -33,7 +31,6 @@ import org.springframework.web.reactive.accept.HeaderContentTypeResolver; import org.springframework.web.reactive.accept.RequestedContentTypeResolver; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.springframework.http.MediaType.ALL; @@ -55,15 +52,11 @@ public class HandlerResultHandlerTests { private MockServerHttpRequest request; - private ServerWebExchange exchange; - @Before public void setUp() throws Exception { this.resultHandler = new TestResultHandler(); - this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path")); - this.exchange = new DefaultServerWebExchange( - this.request, new MockServerHttpResponse(), new MockWebSessionManager()); + this.request = MockServerHttpRequest.get("/path").build(); } @@ -71,28 +64,30 @@ public class HandlerResultHandlerTests { public void usesContentTypeResolver() throws Exception { TestResultHandler resultHandler = new TestResultHandler(new FixedContentTypeResolver(IMAGE_GIF)); List mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG); - MediaType actual = resultHandler.selectMediaType(this.exchange, () -> mediaTypes); + MediaType actual = resultHandler.selectMediaType(exchange(), () -> mediaTypes); assertEquals(IMAGE_GIF, actual); } @Test public void producibleMediaTypesRequestAttribute() throws Exception { - Set producible = Collections.singleton(IMAGE_GIF); - this.exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, producible); + ServerWebExchange exchange = exchange(); + exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(IMAGE_GIF)); List mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG); - MediaType actual = resultHandler.selectMediaType(this.exchange, () -> mediaTypes); + MediaType actual = resultHandler.selectMediaType(exchange, () -> mediaTypes); assertEquals(IMAGE_GIF, actual); } @Test // SPR-9160 public void sortsByQuality() throws Exception { - this.request.setHeader("Accept", "text/plain; q=0.5, application/json"); + this.request = MockServerHttpRequest.get("/path") + .header("Accept", "text/plain; q=0.5, application/json") + .build(); List mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON_UTF8); - MediaType actual = this.resultHandler.selectMediaType(this.exchange, () -> mediaTypes); + MediaType actual = this.resultHandler.selectMediaType(exchange(), () -> mediaTypes); assertEquals(APPLICATION_JSON_UTF8, actual); } @@ -101,9 +96,8 @@ public class HandlerResultHandlerTests { public void charsetFromAcceptHeader() throws Exception { MediaType text8859 = MediaType.parseMediaType("text/plain;charset=ISO-8859-1"); MediaType textUtf8 = MediaType.parseMediaType("text/plain;charset=UTF-8"); - this.request.getHeaders().setAccept(Collections.singletonList(text8859)); - MediaType actual = this.resultHandler.selectMediaType(this.exchange, - () -> Collections.singletonList(textUtf8)); + this.request = MockServerHttpRequest.get("/path").accept(text8859).build(); + MediaType actual = this.resultHandler.selectMediaType(exchange(), () -> Collections.singletonList(textUtf8)); assertEquals(text8859, actual); } @@ -111,11 +105,16 @@ public class HandlerResultHandlerTests { @Test // SPR-12894 public void noConcreteMediaType() throws Exception { List producible = Collections.singletonList(ALL); - MediaType actual = this.resultHandler.selectMediaType(this.exchange, () -> producible); + MediaType actual = this.resultHandler.selectMediaType(exchange(), () -> producible); assertEquals(APPLICATION_OCTET_STREAM, actual); } + @NotNull + private DefaultServerWebExchange exchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + @SuppressWarnings("WeakerAccess") private static class TestResultHandler extends AbstractHandlerResultHandler { diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/CompositeRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/CompositeRequestConditionTests.java index cd734fb697..80027e589f 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/CompositeRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/CompositeRequestConditionTests.java @@ -16,18 +16,16 @@ package org.springframework.web.reactive.result.condition; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -40,8 +38,6 @@ import static org.junit.Assert.assertSame; */ public class CompositeRequestConditionTests { - private ServerWebExchange exchange; - private ServerHttpRequest request; private ParamsRequestCondition param1; @@ -55,9 +51,7 @@ public class CompositeRequestConditionTests { @Before public void setup() throws Exception { - WebSessionManager sessionManager = new MockWebSessionManager(); - this.request = new MockServerHttpRequest(HttpMethod.GET, "/"); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + this.request = MockServerHttpRequest.get("/").build(); this.param1 = new ParamsRequestCondition("param1"); this.param2 = new ParamsRequestCondition("param2"); @@ -97,7 +91,7 @@ public class CompositeRequestConditionTests { @Test public void match() { - this.request.getQueryParams().add("param1", "paramValue1"); + this.request = MockServerHttpRequest.get("/path?param1=paramValue1").build(); RequestCondition condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestCondition condition2 = new RequestMethodsRequestCondition(RequestMethod.GET); @@ -105,45 +99,52 @@ public class CompositeRequestConditionTests { CompositeRequestCondition composite1 = new CompositeRequestCondition(this.param1, condition1); CompositeRequestCondition composite2 = new CompositeRequestCondition(this.param1, condition2); - assertEquals(composite2, composite1.getMatchingCondition(this.exchange)); + assertEquals(composite2, composite1.getMatchingCondition(createExchange())); } @Test public void noMatch() { CompositeRequestCondition cond = new CompositeRequestCondition(this.param1); - assertNull(cond.getMatchingCondition(this.exchange)); + assertNull(cond.getMatchingCondition(createExchange())); } @Test public void matchEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); - assertSame(empty, empty.getMatchingCondition(this.exchange)); + assertSame(empty, empty.getMatchingCondition(createExchange())); } @Test public void compare() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond3 = new CompositeRequestCondition(this.param3); + ServerWebExchange exchange = createExchange(); - assertEquals(1, cond1.compareTo(cond3, this.exchange)); - assertEquals(-1, cond3.compareTo(cond1, this.exchange)); + assertEquals(1, cond1.compareTo(cond3, exchange)); + assertEquals(-1, cond3.compareTo(cond1, exchange)); } @Test public void compareEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); CompositeRequestCondition notEmpty = new CompositeRequestCondition(this.param1); + ServerWebExchange exchange = createExchange(); - assertEquals(0, empty.compareTo(empty, this.exchange)); - assertEquals(-1, notEmpty.compareTo(empty, this.exchange)); - assertEquals(1, empty.compareTo(notEmpty, this.exchange)); + assertEquals(0, empty.compareTo(empty, exchange)); + assertEquals(-1, notEmpty.compareTo(empty, exchange)); + assertEquals(1, empty.compareTo(notEmpty, exchange)); } @Test(expected = IllegalArgumentException.class) public void compareDifferentLength() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1); - cond1.compareTo(cond2, this.exchange); + cond1.compareTo(cond2, createExchange()); + } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ConsumesRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ConsumesRequestConditionTests.java index 9d431ef37f..d6e8f96e03 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ConsumesRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ConsumesRequestConditionTests.java @@ -22,15 +22,12 @@ import java.util.Collections; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.HttpHeaders; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.reactive.result.condition.ConsumesRequestCondition.ConsumeMediaTypeExpression; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -197,13 +194,13 @@ public class ConsumesRequestConditionTests { return createExchange(null); } - private ServerWebExchange createExchange(String contentType) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - if (contentType != null) { - request.getHeaders().add("Content-Type", contentType); - } - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + private ServerWebExchange createExchange(String contentType) { + + MockServerHttpRequest request = (contentType != null ? + MockServerHttpRequest.post("/").header(HttpHeaders.CONTENT_TYPE, contentType).build() : + MockServerHttpRequest.get("/").build()); + + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/HeadersRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/HeadersRequestConditionTests.java index 671aedb80b..620a433501 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/HeadersRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/HeadersRequestConditionTests.java @@ -21,14 +21,11 @@ import java.util.Collection; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -160,13 +157,13 @@ public class HeadersRequestConditionTests { return createExchange(null, null); } - private ServerWebExchange createExchange(String headerName, String headerValue) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - if (headerName != null) { - request.getHeaders().add(headerName, headerValue); - } - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + private ServerWebExchange createExchange(String headerName, String headerValue) { + + ServerHttpRequest request = headerName != null ? + MockServerHttpRequest.get("/").header(headerName, headerValue).build() : + MockServerHttpRequest.get("/").build(); + + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ParamsRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ParamsRequestConditionTests.java index 97a0f12890..05ebfd5d95 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ParamsRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ParamsRequestConditionTests.java @@ -21,23 +21,18 @@ import java.util.Collection; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.util.MultiValueMap; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.springframework.web.util.UriComponentsBuilder.fromPath; /** * Unit tests for {@link ParamsRequestCondition}. @@ -94,7 +89,8 @@ public class ParamsRequestConditionTests { @Test public void compareTo() throws Exception { - ServerWebExchange exchange = exchange(new MockServerHttpRequest(HttpMethod.GET, "/")); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); ParamsRequestCondition condition1 = new ParamsRequestCondition("foo", "bar", "baz"); ParamsRequestCondition condition2 = new ParamsRequestCondition("foo", "bar"); @@ -118,27 +114,20 @@ public class ParamsRequestConditionTests { private ServerWebExchange exchangeWithQuery(String query) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - MultiValueMap params = fromPath("/").query(query).build().getQueryParams(); - request.getQueryParams().putAll(params); - return exchange(request); + ServerHttpRequest request = MockServerHttpRequest.get("/path?" + query).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private ServerWebExchange exchangeWithFormData(String formData) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); - request.setBody(formData); - return exchange(request); + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(formData); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private ServerWebExchange exchange() { - return exchange(new MockServerHttpRequest(HttpMethod.GET, "/")); - } - - private ServerWebExchange exchange(ServerHttpRequest request) { - MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, manager); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java index 39f613aa84..9c101eb535 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/PatternsRequestConditionTests.java @@ -22,14 +22,11 @@ import java.util.Set; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -225,9 +222,8 @@ public class PatternsRequestConditionTests { private ServerWebExchange createExchange(String path) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get(path).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java index c24ec4d256..25dde69358 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/ProducesRequestConditionTests.java @@ -22,14 +22,11 @@ import java.util.Collections; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -313,14 +310,12 @@ public class ProducesRequestConditionTests { private ServerWebExchange createExchange(String... accept) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - if (accept != null) { - for (String value : accept) { - request.getHeaders().add("Accept", value); - } - } - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + + ServerHttpRequest request = (accept != null ? + MockServerHttpRequest.get("/").header("Accept", accept).build() : + MockServerHttpRequest.get("/").build()); + + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestConditionHolderTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestConditionHolderTests.java index 328350f22f..c89d1d7c44 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestConditionHolderTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestConditionHolderTests.java @@ -16,20 +16,15 @@ package org.springframework.web.reactive.result.condition; -import java.net.URISyntaxException; - import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -48,13 +43,9 @@ public class RequestConditionHolderTests { @Before public void setUp() throws Exception { - this.exchange = createExchange(); - } - - private ServerWebExchange createExchange() throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + MockServerHttpResponse response = new MockServerHttpResponse(); + this.exchange = new DefaultServerWebExchange(request, response); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java index 2aab0570e5..521c5ee096 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMappingInfoTests.java @@ -20,15 +20,16 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.springframework.http.HttpMethod; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.reactive.result.method.RequestMappingInfo; import org.springframework.web.server.ServerWebExchange; @@ -50,8 +51,6 @@ import static org.junit.Assert.assertNull; */ public class RequestMappingInfoTests { - private ServerWebExchange exchange; - private ServerHttpRequest request; @@ -59,12 +58,9 @@ public class RequestMappingInfoTests { @Before public void setUp() throws Exception { - WebSessionManager sessionManager = new MockWebSessionManager(); - this.request = new MockServerHttpRequest(HttpMethod.GET, "/foo"); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + this.request = MockServerHttpRequest.get("/foo").build(); } - @Test public void createEmpty() { RequestMappingInfo info = new RequestMappingInfo(null, null, null, null, null, null, null); @@ -85,105 +81,103 @@ public class RequestMappingInfoTests { RequestMappingInfo expected = new RequestMappingInfo( new PatternsRequestCondition("/foo*"), null, null, null, null, null, null); - assertEquals(expected, info.getMatchingCondition(this.exchange)); + assertEquals(expected, info.getMatchingCondition(createExchange())); info = new RequestMappingInfo( new PatternsRequestCondition("/**", "/foo*", "/foo"), null, null, null, null, null, null); expected = new RequestMappingInfo( new PatternsRequestCondition("/foo", "/foo*", "/**"), null, null, null, null, null, null); - assertEquals(expected, info.getMatchingCondition(this.exchange)); + assertEquals(expected, info.getMatchingCondition(createExchange())); } @Test public void matchParamsCondition() { - this.request.getQueryParams().add("foo", "bar"); + this.request = MockServerHttpRequest.get("/foo?foo=bar").build(); - RequestMappingInfo info = - new RequestMappingInfo( - new PatternsRequestCondition("/foo"), null, - new ParamsRequestCondition("foo=bar"), null, null, null, null); - RequestMappingInfo match = info.getMatchingCondition(this.exchange); + RequestMappingInfo info = new RequestMappingInfo( + new PatternsRequestCondition("/foo"), null, + new ParamsRequestCondition("foo=bar"), null, null, null, null); + RequestMappingInfo match = info.getMatchingCondition(createExchange()); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, new ParamsRequestCondition("foo!=bar"), null, null, null, null); - match = info.getMatchingCondition(this.exchange); + match = info.getMatchingCondition(createExchange()); assertNull(match); } @Test public void matchHeadersCondition() { - this.request.getHeaders().add("foo", "bar"); + this.request = MockServerHttpRequest.get("/foo").header("foo", "bar").build(); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, new HeadersRequestCondition("foo=bar"), null, null, null); - RequestMappingInfo match = info.getMatchingCondition(this.exchange); + RequestMappingInfo match = info.getMatchingCondition(createExchange()); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, new HeadersRequestCondition("foo!=bar"), null, null, null); - match = info.getMatchingCondition(this.exchange); + match = info.getMatchingCondition(createExchange()); assertNull(match); } @Test public void matchConsumesCondition() { - this.request.getHeaders().setContentType(MediaType.TEXT_PLAIN); + this.request = MockServerHttpRequest.post("/foo").contentType(MediaType.TEXT_PLAIN).build(); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, new ConsumesRequestCondition("text/plain"), null, null); - RequestMappingInfo match = info.getMatchingCondition(this.exchange); + RequestMappingInfo match = info.getMatchingCondition(createExchange()); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, new ConsumesRequestCondition("application/xml"), null, null); - match = info.getMatchingCondition(this.exchange); + match = info.getMatchingCondition(createExchange()); assertNull(match); } @Test public void matchProducesCondition() { - this.request.getHeaders().setAccept(Collections.singletonList(MediaType.TEXT_PLAIN)); + this.request = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).build(); - RequestMappingInfo info = - new RequestMappingInfo( - new PatternsRequestCondition("/foo"), null, null, null, null, - new ProducesRequestCondition("text/plain"), null); - RequestMappingInfo match = info.getMatchingCondition(this.exchange); + RequestMappingInfo info = new RequestMappingInfo( + new PatternsRequestCondition("/foo"), null, null, null, null, + new ProducesRequestCondition("text/plain"), null); + RequestMappingInfo match = info.getMatchingCondition(createExchange()); assertNotNull(match); info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, null, new ProducesRequestCondition("application/xml"), null); - match = info.getMatchingCondition(this.exchange); + match = info.getMatchingCondition(createExchange()); assertNull(match); } @Test public void matchCustomCondition() { - this.request.getQueryParams().add("foo", "bar"); + this.request = MockServerHttpRequest.get("/foo?foo=bar").build(); RequestMappingInfo info = new RequestMappingInfo( new PatternsRequestCondition("/foo"), null, null, null, null, null, new ParamsRequestCondition("foo=bar")); - RequestMappingInfo match = info.getMatchingCondition(this.exchange); + RequestMappingInfo match = info.getMatchingCondition(createExchange()); assertNotNull(match); @@ -191,7 +185,7 @@ public class RequestMappingInfoTests { new PatternsRequestCondition("/foo"), null, new ParamsRequestCondition("foo!=bar"), null, null, null, new ParamsRequestCondition("foo!=bar")); - match = info.getMatchingCondition(this.exchange); + match = info.getMatchingCondition(createExchange()); assertNull(match); } @@ -207,7 +201,7 @@ public class RequestMappingInfoTests { new RequestMethodsRequestCondition(RequestMethod.GET), new ParamsRequestCondition("foo"), null, null, null, null); - Comparator comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange); + Comparator comparator = (info, otherInfo) -> info.compareTo(otherInfo, createExchange()); List list = asList(none, oneMethod, oneMethodOneParam); Collections.shuffle(list); @@ -329,9 +323,10 @@ public class RequestMappingInfoTests { @Test @Ignore public void preFlightRequest() throws Exception { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.OPTIONS, "/foo"); - request.getHeaders().setOrigin("http://domain.com"); - request.getHeaders().setAccessControlRequestMethod(HttpMethod.POST); + ServerHttpRequest request = MockServerHttpRequest.options("/foo") + .header("Origin", "http://domain.com") + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "POST") + .build(); WebSessionManager manager = new MockWebSessionManager(); MockServerHttpResponse response = new MockServerHttpResponse(); @@ -350,4 +345,9 @@ public class RequestMappingInfoTests { assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java index 91bde9c6c2..ddb0ced32a 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java @@ -24,14 +24,12 @@ import org.junit.Test; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -105,7 +103,7 @@ public class RequestMethodsRequestConditionTests { RequestMethodsRequestCondition c2 = new RequestMethodsRequestCondition(POST); RequestMethodsRequestCondition c3 = new RequestMethodsRequestCondition(); - ServerWebExchange exchange = createExchange(); + ServerWebExchange exchange = createExchange("GET"); int result = c1.compareTo(c2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); @@ -142,15 +140,9 @@ public class RequestMethodsRequestConditionTests { assertNull(condition.getMatchingCondition(exchange)); } - - private ServerWebExchange createExchange() throws URISyntaxException { - return createExchange(null); - } - private ServerWebExchange createExchange(String method) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.resolve(method), "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.valueOf(method), "/").build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java index 2961d5e879..7dcdd0c4e8 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/HandlerMethodMappingTests.java @@ -150,7 +150,7 @@ public class HandlerMethodMappingTests { private ServerWebExchange createExchange(HttpMethod httpMethod, String path) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(httpMethod, path); + ServerHttpRequest request = MockServerHttpRequest.method(httpMethod, path).build(); WebSessionManager sessionManager = new MockWebSessionManager(); return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java index da13ddb503..ec4c297415 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java @@ -24,7 +24,6 @@ import org.junit.Test; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.reactive.BindingContext; @@ -35,10 +34,14 @@ import org.springframework.web.server.UnsupportedMediaTypeStatusException; import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.session.MockWebSessionManager; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Unit tests for {@link InvocableHandlerMethod}. @@ -53,7 +56,7 @@ public class InvocableHandlerMethodTests { @Before public void setUp() throws Exception { this.exchange = new DefaultServerWebExchange( - new MockServerHttpRequest(HttpMethod.GET, "http://localhost:8080/path"), + MockServerHttpRequest.get("http://localhost:8080/path").build(), new MockServerHttpResponse(), new MockWebSessionManager()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java index ccd034c924..3147d015f9 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java @@ -17,7 +17,7 @@ package org.springframework.web.reactive.result.method; import java.lang.reflect.Method; -import java.net.URISyntaxException; +import java.net.URI; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -51,21 +51,25 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerResult; import org.springframework.web.reactive.result.ResolvableMethod; -import org.springframework.web.reactive.result.method.RequestMappingInfo.*; +import org.springframework.web.reactive.result.method.RequestMappingInfo.BuilderConfiguration; import org.springframework.web.server.MethodNotAllowedException; import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.UnsupportedMediaTypeStatusException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import org.springframework.web.server.support.HttpRequestPathHelper; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import static org.springframework.web.bind.annotation.RequestMethod.*; -import static org.springframework.web.reactive.result.method.RequestMappingInfo.*; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.springframework.web.bind.annotation.RequestMethod.GET; +import static org.springframework.web.bind.annotation.RequestMethod.HEAD; +import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS; +import static org.springframework.web.reactive.result.method.RequestMappingInfo.paths; /** * Unit tests for {@link RequestMappingInfoHandlerMapping}. @@ -75,6 +79,8 @@ public class RequestMappingInfoHandlerMappingTests { private TestRequestMappingInfoHandlerMapping handlerMapping; + private ServerHttpRequest request; + @Before public void setUp() throws Exception { @@ -98,8 +104,8 @@ public class RequestMappingInfoHandlerMappingTests { String[] params = new String[] {}; Method expected = resolveMethod(new TestController(), patterns, null, params); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/foo"); - HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.get("/foo").build(); + HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); assertEquals(expected, hm.getMethod()); } @@ -110,8 +116,8 @@ public class RequestMappingInfoHandlerMappingTests { RequestMethod[] methods = new RequestMethod[] {GET, HEAD}; Method expected = resolveMethod(new TestController(), patterns, methods, null); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/bar"); - HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.get("/bar").build(); + HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); assertEquals(expected, hm.getMethod()); } @@ -121,12 +127,12 @@ public class RequestMappingInfoHandlerMappingTests { String[] patterns = new String[] {""}; Method expected = resolveMethod(new TestController(), patterns, null, null); - ServerWebExchange exchange = createExchange(HttpMethod.GET, ""); - HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.get("").build(); + HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); assertEquals(expected, hm.getMethod()); - exchange = createExchange(HttpMethod.GET, "/"); - hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.get("/").build(); + hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); assertEquals(expected, hm.getMethod()); } @@ -136,17 +142,16 @@ public class RequestMappingInfoHandlerMappingTests { String[] params = new String[] {"p"}; Method expected = resolveMethod(new TestController(), patterns, null, params); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/foo"); - exchange.getRequest().getQueryParams().add("p", "anything"); - HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.get("/foo?p=anything").build(); + HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); assertEquals(expected, hm.getMethod()); } @Test public void getHandlerRequestMethodNotAllowed() throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.POST, "/bar"); - Mono mono = this.handlerMapping.getHandler(exchange); + this.request = MockServerHttpRequest.post("/bar").build(); + Mono mono = this.handlerMapping.getHandler(createExchange()); assertError(mono, MethodNotAllowedException.class, ex -> assertEquals(new HashSet<>(Arrays.asList("GET", "HEAD")), ex.getSupportedMethods())); @@ -154,10 +159,9 @@ public class RequestMappingInfoHandlerMappingTests { @Test // SPR-9603 public void getHandlerRequestMethodMatchFalsePositive() throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/users"); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); + this.request = MockServerHttpRequest.get("/users").accept(MediaType.APPLICATION_XML).build(); this.handlerMapping.registerHandler(new UserController()); - Mono mono = this.handlerMapping.getHandler(exchange); + Mono mono = this.handlerMapping.getHandler(createExchange()); StepVerifier.create(mono) .expectError(NotAcceptableStatusException.class) @@ -173,9 +177,8 @@ public class RequestMappingInfoHandlerMappingTests { @Test public void getHandlerTestInvalidContentType() throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.PUT, "/person/1"); - exchange.getRequest().getHeaders().add("Content-Type", "bogus"); - Mono mono = this.handlerMapping.getHandler(exchange); + this.request = MockServerHttpRequest.put("/person/1").header("content-type", "bogus").build(); + Mono mono = this.handlerMapping.getHandler(createExchange()); assertError(mono, UnsupportedMediaTypeStatusException.class, ex -> assertEquals("Request failure [status: 415, " + @@ -192,8 +195,8 @@ public class RequestMappingInfoHandlerMappingTests { @Test // SPR-12854 public void getHandlerTestRequestParamMismatch() throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/params"); - Mono mono = this.handlerMapping.getHandler(exchange); + this.request = MockServerHttpRequest.get("/params").build(); + Mono mono = this.handlerMapping.getHandler(createExchange()); assertError(mono, ServerWebInputException.class, ex -> { assertThat(ex.getReason(), containsString("[foo=bar]")); assertThat(ex.getReason(), containsString("[bar=baz]")); @@ -210,15 +213,15 @@ public class RequestMappingInfoHandlerMappingTests { @Test public void getHandlerProducibleMediaTypesAttribute() throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/content"); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); + this.request = MockServerHttpRequest.get("/content").accept(MediaType.APPLICATION_XML).build(); + ServerWebExchange exchange = createExchange(); this.handlerMapping.getHandler(exchange).block(); String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE; assertEquals(Collections.singleton(MediaType.APPLICATION_XML), exchange.getAttributes().get(name)); - exchange = createExchange(HttpMethod.GET, "/content"); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + this.request = MockServerHttpRequest.get("/content").accept(MediaType.APPLICATION_JSON).build(); + exchange = createExchange(); this.handlerMapping.getHandler(exchange).block(); assertNull("Negated expression shouldn't be listed as producible type", @@ -227,9 +230,11 @@ public class RequestMappingInfoHandlerMappingTests { @Test @SuppressWarnings("unchecked") public void handleMatchUriTemplateVariables() throws Exception { + String lookupPath = "/1/2"; + this.request = MockServerHttpRequest.get(lookupPath).build(); + ServerWebExchange exchange = createExchange(); + RequestMappingInfo key = paths("/{path1}/{path2}").build(); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/1/2"); - String lookupPath = exchange.getRequest().getURI().getPath(); this.handlerMapping.handleMatch(key, lookupPath, exchange); String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; @@ -243,7 +248,8 @@ public class RequestMappingInfoHandlerMappingTests { @Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() throws Exception { RequestMappingInfo key = paths("/{group}/{identifier}").build(); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/group/a%2Fb"); + this.request = MockServerHttpRequest.method(HttpMethod.GET, URI.create("/group/a%2Fb")).build(); + ServerWebExchange exchange = createExchange(); HttpRequestPathHelper pathHelper = new HttpRequestPathHelper(); pathHelper.setUrlDecode(false); @@ -264,7 +270,8 @@ public class RequestMappingInfoHandlerMappingTests { @Test public void handleMatchBestMatchingPatternAttribute() throws Exception { RequestMappingInfo key = paths("/{path1}/2", "/**").build(); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/1/2"); + this.request = MockServerHttpRequest.get("/1/2").build(); + ServerWebExchange exchange = createExchange(); this.handlerMapping.handleMatch(key, "/1/2", exchange); assertEquals("/{path1}/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)); @@ -273,7 +280,8 @@ public class RequestMappingInfoHandlerMappingTests { @Test public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Exception { RequestMappingInfo key = paths().build(); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/1/2"); + this.request = MockServerHttpRequest.get("/1/2").build(); + ServerWebExchange exchange = createExchange(); this.handlerMapping.handleMatch(key, "/1/2", exchange); @@ -286,7 +294,8 @@ public class RequestMappingInfoHandlerMappingTests { MultiValueMap matrixVariables; Map uriVariables; - exchange = createExchange(HttpMethod.GET, "/"); + this.request = MockServerHttpRequest.get("/").build(); + exchange = createExchange(); handleMatch(exchange, "/{cars}", "/cars;colors=red,blue,green;year=2012"); matrixVariables = getMatrixVariables(exchange, "cars"); @@ -297,7 +306,8 @@ public class RequestMappingInfoHandlerMappingTests { assertEquals("2012", matrixVariables.getFirst("year")); assertEquals("cars", uriVariables.get("cars")); - exchange = createExchange(HttpMethod.GET, "/"); + this.request = MockServerHttpRequest.get("/").build(); + exchange = createExchange(); handleMatch(exchange, "/{cars:[^;]+}{params}", "/cars;colors=red,blue,green;year=2012"); matrixVariables = getMatrixVariables(exchange, "params"); @@ -309,7 +319,8 @@ public class RequestMappingInfoHandlerMappingTests { assertEquals("cars", uriVariables.get("cars")); assertEquals(";colors=red,blue,green;year=2012", uriVariables.get("params")); - exchange = createExchange(HttpMethod.GET, "/"); + this.request = MockServerHttpRequest.get("/").build(); + exchange = createExchange(); handleMatch(exchange, "/{cars:[^;]+}{params}", "/cars"); matrixVariables = getMatrixVariables(exchange, "params"); @@ -326,7 +337,8 @@ public class RequestMappingInfoHandlerMappingTests { urlPathHelper.setUrlDecode(false); this.handlerMapping.setPathHelper(urlPathHelper); - ServerWebExchange exchange = createExchange(HttpMethod.GET, "/"); + this.request = MockServerHttpRequest.get("/").build(); + ServerWebExchange exchange = createExchange(); handleMatch(exchange, "/path{filter}", "/path;mvar=a%2fb"); MultiValueMap matrixVariables = getMatrixVariables(exchange, "filter"); @@ -338,10 +350,8 @@ public class RequestMappingInfoHandlerMappingTests { } - private ServerWebExchange createExchange(HttpMethod method, String url) throws URISyntaxException { - ServerHttpRequest request = new MockServerHttpRequest(method, url); - WebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + private ServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); } @@ -359,9 +369,8 @@ public class RequestMappingInfoHandlerMappingTests { private void testHttpMediaTypeNotSupportedException(String url) throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.PUT, url); - exchange.getRequest().getHeaders().setContentType(MediaType.APPLICATION_JSON); - Mono mono = this.handlerMapping.getHandler(exchange); + this.request = MockServerHttpRequest.put(url).contentType(MediaType.APPLICATION_JSON).build(); + Mono mono = this.handlerMapping.getHandler(createExchange()); assertError(mono, UnsupportedMediaTypeStatusException.class, ex -> assertEquals("Invalid supported consumable media types", @@ -370,8 +379,9 @@ public class RequestMappingInfoHandlerMappingTests { } private void testHttpOptions(String requestURI, String allowHeader) throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, requestURI); - HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); + this.request = MockServerHttpRequest.options(requestURI).build(); + ServerWebExchange exchange = createExchange(); + HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block(); BindingContext bindingContext = new BindingContext(); InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod); @@ -387,9 +397,8 @@ public class RequestMappingInfoHandlerMappingTests { } private void testMediaTypeNotAcceptable(String url) throws Exception { - ServerWebExchange exchange = createExchange(HttpMethod.GET, url); - exchange.getRequest().getHeaders().setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - Mono mono = this.handlerMapping.getHandler(exchange); + this.request = MockServerHttpRequest.get(url).accept(MediaType.APPLICATION_JSON).build(); + Mono mono = this.handlerMapping.getHandler(createExchange()); assertError(mono, NotAcceptableStatusException.class, ex -> assertEquals("Invalid supported producible media types", diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/BindingContextFactoryTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/BindingContextFactoryTests.java index 2ac3ea5b07..fdce3b106a 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/BindingContextFactoryTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/BindingContextFactoryTests.java @@ -25,7 +25,6 @@ import reactor.core.publisher.Mono; import rx.Single; import org.springframework.context.support.StaticApplicationContext; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.ui.Model; @@ -42,11 +41,9 @@ import org.springframework.web.reactive.config.WebReactiveConfigurationSupport; import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; /** * Unit tests for {@link BindingContextFactory}. @@ -67,10 +64,9 @@ public class BindingContextFactoryTests { adapter.afterPropertiesSet(); this.contextFactory = new BindingContextFactory(adapter); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); + MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.exchange = new DefaultServerWebExchange(request, response); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/CookieValueMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/CookieValueMethodArgumentResolverTests.java index b3d38d63a4..97f235efb8 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/CookieValueMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/CookieValueMethodArgumentResolverTests.java @@ -18,6 +18,7 @@ package org.springframework.web.reactive.result.method.annotation; import java.lang.reflect.Method; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; @@ -27,19 +28,17 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.http.HttpCookie; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.reactive.BindingContext; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Test fixture with {@link CookieValueMethodArgumentResolver}. @@ -50,7 +49,7 @@ public class CookieValueMethodArgumentResolverTests { private CookieValueMethodArgumentResolver resolver; - private ServerWebExchange exchange; + private ServerHttpRequest request; private MethodParameter cookieParameter; private MethodParameter cookieStringParameter; @@ -65,9 +64,7 @@ public class CookieValueMethodArgumentResolverTests { context.refresh(); this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory()); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + this.request = MockServerHttpRequest.get("/").build(); Method method = getClass().getMethod("params", HttpCookie.class, String.class, String.class); this.cookieParameter = new SynthesizingMethodParameter(method, 0); @@ -86,10 +83,10 @@ public class CookieValueMethodArgumentResolverTests { @Test public void resolveCookieArgument() { HttpCookie expected = new HttpCookie("name", "foo"); - this.exchange.getRequest().getCookies().add(expected.getName(), expected); + this.request = MockServerHttpRequest.get("/").cookie(expected.getName(), expected).build(); Mono mono = this.resolver.resolveArgument( - this.cookieParameter, this.bindingContext, this.exchange); + this.cookieParameter, this.bindingContext, createExchange()); assertEquals(expected, mono.block()); } @@ -97,10 +94,10 @@ public class CookieValueMethodArgumentResolverTests { @Test public void resolveCookieStringArgument() { HttpCookie cookie = new HttpCookie("name", "foo"); - this.exchange.getRequest().getCookies().add(cookie.getName(), cookie); + this.request = MockServerHttpRequest.get("/").cookie(cookie.getName(), cookie).build(); Mono mono = this.resolver.resolveArgument( - this.cookieStringParameter, this.bindingContext, this.exchange); + this.cookieStringParameter, this.bindingContext, createExchange()); assertEquals("Invalid result", cookie.getValue(), mono.block()); } @@ -108,7 +105,7 @@ public class CookieValueMethodArgumentResolverTests { @Test public void resolveCookieDefaultValue() { Object result = this.resolver.resolveArgument( - this.cookieStringParameter, this.bindingContext, this.exchange).block(); + this.cookieStringParameter, this.bindingContext, createExchange()).block(); assertTrue(result instanceof String); assertEquals("bar", result); @@ -116,13 +113,18 @@ public class CookieValueMethodArgumentResolverTests { @Test public void notFound() { - Mono mono = resolver.resolveArgument(this.cookieParameter, this.bindingContext, this.exchange); + Mono mono = resolver.resolveArgument(this.cookieParameter, this.bindingContext, createExchange()); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + @SuppressWarnings("unused") public void params( diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsArgumentResolverTests.java index d1e8e52629..bb075ba8e0 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ErrorsArgumentResolverTests.java @@ -24,7 +24,6 @@ import reactor.core.publisher.MonoProcessor; import org.springframework.core.MethodParameter; import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ResolvableType; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.validation.BindingResult; @@ -35,13 +34,12 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import static org.springframework.core.ResolvableType.*; +import static org.springframework.core.ResolvableType.forClass; +import static org.springframework.core.ResolvableType.forClassWithGenerics; /** * Unit tests for {@link ErrorsMethodArgumentResolver}. @@ -65,10 +63,9 @@ public class ErrorsArgumentResolverTests { public void setUp() throws Exception { this.resolver = new ErrorsMethodArgumentResolver(new ReactiveAdapterRegistry()); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.POST, "/path"); + MockServerHttpRequest request = MockServerHttpRequest.post("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.exchange = new DefaultServerWebExchange(request, response); Foo foo = new Foo(); WebExchangeDataBinder binder = this.bindingContext.createDataBinder(this.exchange, foo, "foo"); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ExpressionValueMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ExpressionValueMethodArgumentResolverTests.java index 8823ea7770..b750b27a5e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ExpressionValueMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ExpressionValueMethodArgumentResolverTests.java @@ -25,17 +25,16 @@ import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.MethodParameter; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.reactive.BindingContext; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Unit tests for {@link ExpressionValueMethodArgumentResolver}. @@ -58,9 +57,8 @@ public class ExpressionValueMethodArgumentResolverTests { context.refresh(); this.resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory()); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); Method method = getClass().getMethod("params", int.class, String.class); this.paramSystemProperty = new MethodParameter(method, 0); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/HttpEntityArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/HttpEntityArgumentResolverTests.java index 630d6908d4..dabb5c0eea 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/HttpEntityArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/HttpEntityArgumentResolverTests.java @@ -41,7 +41,7 @@ import org.springframework.core.codec.StringDecoder; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.HttpMessageReader; @@ -52,10 +52,13 @@ import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import static org.junit.Assert.*; -import static org.springframework.core.ResolvableType.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.core.ResolvableType.forClassWithGenerics; /** * Unit tests for {@link HttpEntityArgumentResolver}.When adding a test also @@ -69,8 +72,6 @@ public class HttpEntityArgumentResolverTests { private HttpEntityArgumentResolver resolver = createResolver(); - private ServerWebExchange exchange; - private MockServerHttpRequest request; private ResolvableMethod testMethod = ResolvableMethod.onClass(getClass()).name("handle"); @@ -78,9 +79,7 @@ public class HttpEntityArgumentResolverTests { @Before public void setUp() throws Exception { - this.request = new MockServerHttpRequest(HttpMethod.POST, "/path"); - MockServerHttpResponse response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, response, new MockWebSessionManager()); + this.request = MockServerHttpRequest.post("/path").build(); } private HttpEntityArgumentResolver createResolver() { @@ -318,11 +317,13 @@ public class HttpEntityArgumentResolverTests { @SuppressWarnings("unchecked") private T resolveValue(ResolvableType type, String body) { - this.request.setHeader("foo", "bar").setHeader("Content-Type", "text/plain"); - this.request.setBody(body); + this.request = MockServerHttpRequest.post("/path").header("foo", "bar") + .contentType(MediaType.TEXT_PLAIN) + .body(body); + ServerWebExchange exchange = new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); MethodParameter param = this.testMethod.resolveParam(type); - Mono result = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); + Mono result = this.resolver.resolveArgument(param, new BindingContext(), exchange); Object value = result.block(Duration.ofSeconds(5)); assertNotNull(value); @@ -334,8 +335,9 @@ public class HttpEntityArgumentResolverTests { @SuppressWarnings("unchecked") private HttpEntity resolveValueWithEmptyBody(ResolvableType type) { + ServerWebExchange exchange = new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); MethodParameter param = this.testMethod.resolveParam(type); - Mono result = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); + Mono result = this.resolver.resolveArgument(param, new BindingContext(), exchange); HttpEntity httpEntity = (HttpEntity) result.block(Duration.ofSeconds(5)); assertEquals(this.request.getHeaders(), httpEntity.getHeaders()); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/InitBinderBindingContextTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/InitBinderBindingContextTests.java index 5e8540a363..b3020b89e1 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/InitBinderBindingContextTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/InitBinderBindingContextTests.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.junit.Before; import org.junit.Test; import org.springframework.core.LocalVariableTableParameterNameDiscoverer; @@ -35,11 +37,12 @@ import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver; import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; /** * Unit tests for {@link InitBinderBindingContext}. @@ -51,14 +54,18 @@ public class InitBinderBindingContextTests { private final List argumentResolvers = new ArrayList<>(); - private final ServerWebExchange exchange = new DefaultServerWebExchange( - new MockServerHttpRequest(), new MockServerHttpResponse(), new DefaultWebSessionManager()); + private MockServerHttpRequest request; + @Before + public void setUp() throws Exception { + this.request = MockServerHttpRequest.get("/").build(); + } + @Test public void createBinder() throws Exception { BindingContext context = createBindingContext("initBinder", WebDataBinder.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, null); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, null); assertNotNull(dataBinder.getDisallowedFields()); assertEquals("id", dataBinder.getDisallowedFields()[0]); @@ -70,7 +77,7 @@ public class InitBinderBindingContextTests { bindingInitializer.setConversionService(conversionService); BindingContext context = createBindingContext("initBinder", WebDataBinder.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, null); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, null); assertSame(conversionService, dataBinder.getConversionService()); } @@ -78,7 +85,7 @@ public class InitBinderBindingContextTests { @Test public void createBinderWithAttrName() throws Exception { BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, "foo"); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, "foo"); assertNotNull(dataBinder.getDisallowedFields()); assertEquals("id", dataBinder.getDisallowedFields()[0]); @@ -87,7 +94,7 @@ public class InitBinderBindingContextTests { @Test public void createBinderWithAttrNameNoMatch() throws Exception { BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, "invalidName"); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, "invalidName"); assertNull(dataBinder.getDisallowedFields()); } @@ -95,7 +102,7 @@ public class InitBinderBindingContextTests { @Test public void createBinderNullAttrName() throws Exception { BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, null); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, null); assertNull(dataBinder.getDisallowedFields()); } @@ -103,21 +110,25 @@ public class InitBinderBindingContextTests { @Test(expected = IllegalStateException.class) public void returnValueNotExpected() throws Exception { BindingContext context = createBindingContext("initBinderReturnValue", WebDataBinder.class); - context.createDataBinder(this.exchange, null, "invalidName"); + context.createDataBinder(createExchange(), null, "invalidName"); } @Test public void createBinderTypeConversion() throws Exception { - this.exchange.getRequest().getQueryParams().add("requestParam", "22"); + this.request = MockServerHttpRequest.get("/path?requestParam=22").build(); this.argumentResolvers.add(new RequestParamMethodArgumentResolver(null, false)); BindingContext context = createBindingContext("initBinderTypeConversion", WebDataBinder.class, int.class); - WebDataBinder dataBinder = context.createDataBinder(this.exchange, null, "foo"); + WebDataBinder dataBinder = context.createDataBinder(createExchange(), null, "foo"); assertNotNull(dataBinder.getDisallowedFields()); assertEquals("requestParam-22", dataBinder.getDisallowedFields()[0]); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } private BindingContext createBindingContext(String methodName, Class... parameterTypes) throws Exception { diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java index 273f6daf99..f96ba7fbe7 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageReaderArgumentResolverTests.java @@ -30,6 +30,7 @@ import javax.xml.bind.annotation.XmlRootElement; import io.reactivex.Flowable; import io.reactivex.Maybe; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Flux; @@ -41,7 +42,7 @@ import rx.Single; import org.springframework.core.MethodParameter; import org.springframework.core.ResolvableType; import org.springframework.core.codec.Decoder; -import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.json.Jackson2JsonDecoder; @@ -54,14 +55,16 @@ import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.method.HandlerMethod; import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.result.ResolvableMethod; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.UnsupportedMediaTypeStatusException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import static org.junit.Assert.*; -import static org.springframework.core.ResolvableType.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.core.ResolvableType.forClass; +import static org.springframework.core.ResolvableType.forClassWithGenerics; /** * Unit tests for {@link AbstractMessageReaderArgumentResolver}. @@ -72,8 +75,6 @@ public class MessageReaderArgumentResolverTests { private AbstractMessageReaderArgumentResolver resolver = resolver(new Jackson2JsonDecoder()); - private ServerWebExchange exchange; - private MockServerHttpRequest request; private BindingContext bindingContext; @@ -83,10 +84,7 @@ public class MessageReaderArgumentResolverTests { @Before public void setUp() throws Exception { - this.request = new MockServerHttpRequest(HttpMethod.POST, "/path"); - MockServerHttpResponse response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, response, new MockWebSessionManager()); - + this.request = request().build(); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setValidator(new TestBeanValidator()); this.bindingContext = new BindingContext(initializer); @@ -95,10 +93,10 @@ public class MessageReaderArgumentResolverTests { @Test public void missingContentType() throws Exception { - this.request.setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"); + this.request = request().body("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"); ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class); MethodParameter param = this.testMethod.resolveParam(type); - Mono result = this.resolver.readBody(param, true, this.bindingContext, this.exchange); + Mono result = this.resolver.readBody(param, true, this.bindingContext, exchange()); StepVerifier.create(result).expectError(UnsupportedMediaTypeStatusException.class).verify(); } @@ -107,11 +105,11 @@ public class MessageReaderArgumentResolverTests { @Test @SuppressWarnings("unchecked") // SPR-9942 public void emptyBody() throws Exception { - this.request.setHeader("Content-Type", "application/json"); + this.request = request().header("Content-Type", "application/json").build(); ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class); MethodParameter param = this.testMethod.resolveParam(type); Mono result = (Mono) this.resolver.readBody( - param, true, this.bindingContext, this.exchange).block(); + param, true, this.bindingContext, exchange()).block(); StepVerifier.create(result).expectError(ServerWebInputException.class).verify(); } @@ -301,8 +299,8 @@ public class MessageReaderArgumentResolverTests { @SuppressWarnings("unchecked") private T resolveValue(MethodParameter param, String body) { - this.request.setHeader("Content-Type", "application/json").setBody(body); - Mono result = this.resolver.readBody(param, true, this.bindingContext, this.exchange); + this.request = request().contentType(MediaType.APPLICATION_JSON).body(body); + Mono result = this.resolver.readBody(param, true, this.bindingContext, exchange()); Object value = result.block(Duration.ofSeconds(5)); assertNotNull(value); @@ -312,6 +310,15 @@ public class MessageReaderArgumentResolverTests { return (T) value; } + private MockServerHttpRequest.BodyBuilder request() { + return MockServerHttpRequest.post("/path"); + } + + @NotNull + private DefaultServerWebExchange exchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + @SuppressWarnings("Convert2MethodRef") private AbstractMessageReaderArgumentResolver resolver(Decoder... decoders) { List> readers = new ArrayList<>(); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java index d201f71fcc..2a9a921ac6 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/MessageWriterResultHandlerTests.java @@ -44,7 +44,6 @@ import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.buffer.support.DataBufferTestUtils; -import org.springframework.http.HttpMethod; import org.springframework.http.codec.EncoderHttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter; @@ -59,7 +58,6 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuild import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -83,8 +81,8 @@ public class MessageWriterResultHandlerTests { @Before public void setUp() throws Exception { this.resultHandler = createResultHandler(); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); - this.exchange = new DefaultServerWebExchange(request, this.response, new MockWebSessionManager()); + ServerHttpRequest request = MockServerHttpRequest.get("/path").build(); + this.exchange = new DefaultServerWebExchange(request, this.response); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolverTests.java index 8071712096..50989f74f1 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolverTests.java @@ -29,7 +29,6 @@ import rx.Single; import org.springframework.core.MethodParameter; import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ResolvableType; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -43,8 +42,6 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static junit.framework.TestCase.assertNotNull; import static org.junit.Assert.assertEquals; @@ -282,12 +279,9 @@ public class ModelAttributeMethodArgumentResolverTests { } private ServerWebExchange exchange(String formData) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); - request.setBody(formData); - MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, manager); + MediaType mediaType = MediaType.APPLICATION_FORM_URLENCODED; + MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(mediaType).body(formData); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMapMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMapMethodArgumentResolverTests.java index b2abd1d6e1..d64a6e9fe5 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMapMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMapMethodArgumentResolverTests.java @@ -26,7 +26,6 @@ import org.junit.Test; import reactor.core.publisher.Mono; import org.springframework.core.MethodParameter; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -35,10 +34,10 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Unit tests for {@link PathVariableMapMethodArgumentResolver}. @@ -60,9 +59,8 @@ public class PathVariableMapMethodArgumentResolverTests { public void setUp() throws Exception { this.resolver = new PathVariableMapMethodArgumentResolver(); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); Method method = getClass().getMethod("handle", Map.class, Map.class, Map.class); this.paramMap = new MethodParameter(method, 0); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMethodArgumentResolverTests.java index 20c90560da..462964aa4d 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/PathVariableMethodArgumentResolverTests.java @@ -29,7 +29,6 @@ import reactor.test.StepVerifier; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -41,10 +40,10 @@ import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.server.ServerErrorException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Unit tests for {@link PathVariableMethodArgumentResolver}. @@ -71,9 +70,8 @@ public class PathVariableMethodArgumentResolverTests { public void setUp() throws Exception { this.resolver = new PathVariableMethodArgumentResolver(null); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class[]) null); paramNamedString = new SynthesizingMethodParameter(method, 0); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestAttributeMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestAttributeMethodArgumentResolverTests.java index 0d674d7d41..341df6b12c 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestAttributeMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestAttributeMethodArgumentResolverTests.java @@ -30,7 +30,6 @@ import org.springframework.core.GenericTypeResolver; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -41,10 +40,13 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; /** * Unit tests for {@link RequestAttributeMethodArgumentResolver}. @@ -67,9 +69,8 @@ public class RequestAttributeMethodArgumentResolverTests { context.refresh(); this.resolver = new RequestAttributeMethodArgumentResolver(context.getBeanFactory()); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithRequestAttribute", (Class[]) null); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestBodyArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestBodyArgumentResolverTests.java index 277a4cd088..4de65e418c 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestBodyArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestBodyArgumentResolverTests.java @@ -35,7 +35,6 @@ import rx.Single; import org.springframework.core.MethodParameter; import org.springframework.core.ResolvableType; import org.springframework.core.codec.StringDecoder; -import org.springframework.http.HttpMethod; import org.springframework.http.codec.DecoderHttpMessageReader; import org.springframework.http.codec.HttpMessageReader; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; @@ -46,10 +45,14 @@ import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import static org.junit.Assert.*; -import static org.springframework.core.ResolvableType.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.core.ResolvableType.forClass; +import static org.springframework.core.ResolvableType.forClassWithGenerics; /** * Unit tests for {@link RequestBodyArgumentResolver}. When adding a test also @@ -62,18 +65,11 @@ public class RequestBodyArgumentResolverTests { private RequestBodyArgumentResolver resolver = resolver(); - private ServerWebExchange exchange; - - private MockServerHttpRequest request; - private ResolvableMethod testMethod = ResolvableMethod.onClass(this.getClass()).name("handle"); @Before public void setUp() throws Exception { - this.request = new MockServerHttpRequest(HttpMethod.POST, "/path"); - MockServerHttpResponse response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(this.request, response, new MockWebSessionManager()); } private RequestBodyArgumentResolver resolver() { @@ -219,8 +215,9 @@ public class RequestBodyArgumentResolverTests { @SuppressWarnings("unchecked") private T resolveValue(MethodParameter param, String body) { - this.request.setBody(body); - Mono result = this.resolver.readBody(param, true, new BindingContext(), this.exchange); + MockServerHttpRequest request = MockServerHttpRequest.post("/path").body(body); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); + Mono result = this.resolver.readBody(param, true, new BindingContext(), exchange); Object value = result.block(Duration.ofSeconds(5)); assertNotNull(value); @@ -233,8 +230,10 @@ public class RequestBodyArgumentResolverTests { @SuppressWarnings("unchecked") private T resolveValueWithEmptyBody(ResolvableType bodyType, boolean isRequired) { + MockServerHttpRequest request = MockServerHttpRequest.post("/path").build(); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); MethodParameter param = this.testMethod.resolveParam(bodyType, requestBody(isRequired)); - Mono result = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); + Mono result = this.resolver.resolveArgument(param, new BindingContext(), exchange); Object value = result.block(Duration.ofSeconds(5)); if (value != null) { @@ -254,6 +253,7 @@ public class RequestBodyArgumentResolverTests { } + @SuppressWarnings("unused") void handle( @RequestBody String string, @RequestBody Mono mono, diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java index 88b06012d2..518f9d631a 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java @@ -20,6 +20,7 @@ import java.lang.reflect.Method; import java.util.Collections; import java.util.Map; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; @@ -27,17 +28,14 @@ import reactor.core.publisher.Mono; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -57,7 +55,7 @@ public class RequestHeaderMapMethodArgumentResolverTests { private MethodParameter paramHttpHeaders; private MethodParameter paramUnsupported; - private ServerWebExchange exchange; + private ServerHttpRequest request; @Before @@ -70,9 +68,7 @@ public class RequestHeaderMapMethodArgumentResolverTests { paramHttpHeaders = new SynthesizingMethodParameter(method, 2); paramUnsupported = new SynthesizingMethodParameter(method, 3); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + this.request = MockServerHttpRequest.get("/").build(); } @@ -89,9 +85,9 @@ public class RequestHeaderMapMethodArgumentResolverTests { String name = "foo"; String value = "bar"; Map expected = Collections.singletonMap(name, value); - this.exchange.getRequest().getHeaders().add(name, value); + this.request = MockServerHttpRequest.get("/").header(name, value).build(); - Mono mono = this.resolver.resolveArgument(paramMap, null, this.exchange); + Mono mono = this.resolver.resolveArgument(paramMap, null, createExchange()); Object result = mono.block(); assertTrue(result instanceof Map); @@ -103,15 +99,13 @@ public class RequestHeaderMapMethodArgumentResolverTests { String name = "foo"; String value1 = "bar"; String value2 = "baz"; - - this.exchange.getRequest().getHeaders().add(name, value1); - this.exchange.getRequest().getHeaders().add(name, value2); + this.request = MockServerHttpRequest.get("/").header(name, value1, value2).build(); MultiValueMap expected = new LinkedMultiValueMap<>(1); expected.add(name, value1); expected.add(name, value2); - Mono mono = this.resolver.resolveArgument(paramMultiValueMap, null, this.exchange); + Mono mono = this.resolver.resolveArgument(paramMultiValueMap, null, createExchange()); Object result = mono.block(); assertTrue(result instanceof MultiValueMap); @@ -123,21 +117,24 @@ public class RequestHeaderMapMethodArgumentResolverTests { String name = "foo"; String value1 = "bar"; String value2 = "baz"; - - this.exchange.getRequest().getHeaders().add(name, value1); - this.exchange.getRequest().getHeaders().add(name, value2); + this.request = MockServerHttpRequest.get("/").header(name, value1, value2).build(); HttpHeaders expected = new HttpHeaders(); expected.add(name, value1); expected.add(name, value2); - Mono mono = this.resolver.resolveArgument(paramHttpHeaders, null, this.exchange); + Mono mono = this.resolver.resolveArgument(paramHttpHeaders, null, createExchange()); Object result = mono.block(); assertTrue(result instanceof HttpHeaders); assertEquals("Invalid result", expected, result); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } + @SuppressWarnings("unused") public void params(@RequestHeader Map param1, diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java index 20f2f8255f..93bdcb7a5d 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestHeaderMethodArgumentResolverTests.java @@ -19,10 +19,10 @@ package org.springframework.web.reactive.result.method.annotation; import java.lang.reflect.Method; import java.time.Instant; import java.time.format.DateTimeFormatter; -import java.util.Arrays; import java.util.Date; import java.util.Map; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; @@ -32,7 +32,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -40,13 +39,13 @@ import org.springframework.util.ReflectionUtils; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.reactive.BindingContext; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Unit tests for {@link RequestHeaderMethodArgumentResolver}. @@ -66,7 +65,7 @@ public class RequestHeaderMethodArgumentResolverTests { private MethodParameter paramDate; private MethodParameter paramInstant; - private ServerWebExchange exchange; + private ServerHttpRequest request; private BindingContext bindingContext; @@ -88,9 +87,7 @@ public class RequestHeaderMethodArgumentResolverTests { this.paramDate = new SynthesizingMethodParameter(method, 6); this.paramInstant = new SynthesizingMethodParameter(method, 7); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - WebSessionManager sessionManager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); + this.request = MockServerHttpRequest.get("/").build(); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultFormattingConversionService()); @@ -108,10 +105,10 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void resolveStringArgument() throws Exception { String expected = "foo"; - this.exchange.getRequest().getHeaders().add("name", expected); + this.request = MockServerHttpRequest.get("/").header("name", expected).build(); Mono mono = this.resolver.resolveArgument( - this.paramNamedDefaultValueStringHeader, this.bindingContext, this.exchange); + this.paramNamedDefaultValueStringHeader, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String); @@ -120,21 +117,20 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void resolveStringArrayArgument() throws Exception { - String[] expected = new String[] {"foo", "bar"}; - this.exchange.getRequest().getHeaders().put("name", Arrays.asList(expected)); + this.request = MockServerHttpRequest.get("/").header("name", "foo", "bar").build(); Mono mono = this.resolver.resolveArgument( - this.paramNamedValueStringArray, this.bindingContext, this.exchange); + this.paramNamedValueStringArray, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String[]); - assertArrayEquals(expected, (String[]) result); + assertArrayEquals(new String[] {"foo", "bar"}, (String[]) result); } @Test public void resolveDefaultValue() throws Exception { Mono mono = this.resolver.resolveArgument( - this.paramNamedDefaultValueStringHeader, this.bindingContext, this.exchange); + this.paramNamedDefaultValueStringHeader, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String); @@ -146,7 +142,7 @@ public class RequestHeaderMethodArgumentResolverTests { System.setProperty("systemProperty", "bar"); try { Mono mono = this.resolver.resolveArgument( - this.paramSystemProperty, this.bindingContext, this.exchange); + this.paramSystemProperty, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String); @@ -160,12 +156,12 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void resolveNameFromSystemPropertyThroughExpression() throws Exception { String expected = "foo"; - this.exchange.getRequest().getHeaders().add("bar", expected); + this.request = MockServerHttpRequest.get("/").header("bar", expected).build(); System.setProperty("systemProperty", "bar"); try { Mono mono = this.resolver.resolveArgument( - this.paramResolvedNameWithExpression, this.bindingContext, this.exchange); + this.paramResolvedNameWithExpression, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String); @@ -179,12 +175,12 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void resolveNameFromSystemPropertyThroughPlaceholder() throws Exception { String expected = "foo"; - this.exchange.getRequest().getHeaders().add("bar", expected); + this.request = MockServerHttpRequest.get("/").header("bar", expected).build(); System.setProperty("systemProperty", "bar"); try { Mono mono = this.resolver.resolveArgument( - this.paramResolvedNameWithPlaceholder, this.bindingContext, this.exchange); + this.paramResolvedNameWithPlaceholder, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof String); @@ -198,7 +194,7 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void notFound() throws Exception { Mono mono = resolver.resolveArgument( - this.paramNamedValueStringArray, this.bindingContext, this.exchange); + this.paramNamedValueStringArray, this.bindingContext, createExchange()); StepVerifier.create(mono) .expectNextCount(0) @@ -210,9 +206,9 @@ public class RequestHeaderMethodArgumentResolverTests { @SuppressWarnings("deprecation") public void dateConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; - this.exchange.getRequest().getHeaders().add("name", rfc1123val); + this.request = MockServerHttpRequest.get("/").header("name", rfc1123val).build(); - Mono mono = this.resolver.resolveArgument(this.paramDate, this.bindingContext, this.exchange); + Mono mono = this.resolver.resolveArgument(this.paramDate, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof Date); @@ -222,15 +218,20 @@ public class RequestHeaderMethodArgumentResolverTests { @Test public void instantConversion() throws Exception { String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100"; - this.exchange.getRequest().getHeaders().add("name", rfc1123val); + this.request = MockServerHttpRequest.get("/").header("name", rfc1123val).build(); - Mono mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, this.exchange); + Mono mono = this.resolver.resolveArgument(this.paramInstant, this.bindingContext, createExchange()); Object result = mono.block(); assertTrue(result instanceof Instant); assertEquals(Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rfc1123val)), result); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); + } + @SuppressWarnings("unused") public void params( diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMapMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMapMethodArgumentResolverTests.java index a29dc7cf91..574008c739 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMapMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMapMethodArgumentResolverTests.java @@ -27,22 +27,17 @@ import org.junit.Test; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.springframework.web.util.UriComponentsBuilder.fromPath; /** * Unit tests for {@link RequestParamMapMethodArgumentResolver}. @@ -104,23 +99,15 @@ public class RequestParamMapMethodArgumentResolverTests { private ServerWebExchange exchangeWithQuery(String query) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - MultiValueMap params = fromPath("/").query(query).build().getQueryParams(); - request.getQueryParams().putAll(params); - return exchange(request); + MockServerHttpRequest request = MockServerHttpRequest.get("/path?" + query).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private ServerWebExchange exchangeWithFormData(String formData) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); - request.setBody(formData); - return exchange(request); - } - - private ServerWebExchange exchange(ServerHttpRequest request) { - MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, manager); + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(formData); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private Object resolve(MethodParameter parameter, ServerWebExchange exchange) { diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java index 3e1f431901..1066f31f49 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -31,12 +31,9 @@ import org.springframework.core.MethodParameter; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.util.MultiValueMap; import org.springframework.util.ReflectionUtils; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; @@ -44,15 +41,12 @@ import org.springframework.web.reactive.BindingContext; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.springframework.web.util.UriComponentsBuilder.fromPath; /** * Unit tests for {@link RequestParamMethodArgumentResolver}. @@ -196,27 +190,20 @@ public class RequestParamMethodArgumentResolverTests { private ServerWebExchange exchangeWithQuery(String query) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - MultiValueMap params = fromPath("/").query(query).build().getQueryParams(); - request.getQueryParams().putAll(params); - return exchange(request); + MockServerHttpRequest request = MockServerHttpRequest.get("/path?" + query).build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private ServerWebExchange exchangeWithFormData(String formData) throws URISyntaxException { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); - request.setBody(formData); - return exchange(request); + MockServerHttpRequest request = MockServerHttpRequest.post("/path") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(formData); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private ServerWebExchange exchange() { - return exchange(new MockServerHttpRequest(HttpMethod.GET, "/")); - } - - private ServerWebExchange exchange(ServerHttpRequest request) { - MockServerHttpResponse response = new MockServerHttpResponse(); - WebSessionManager manager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, manager); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } private Object resolve(MethodParameter parameter, ServerWebExchange exchange) { diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java index 6924780ab5..3581f8a5bc 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java @@ -29,7 +29,6 @@ import rx.Single; import org.springframework.core.codec.ByteBufferEncoder; import org.springframework.core.codec.CharSequenceEncoder; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.codec.EncoderHttpMessageWriter; @@ -51,7 +50,6 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolver; import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; @@ -71,7 +69,7 @@ public class ResponseBodyResultHandlerTests { private ResponseBodyResultHandler resultHandler; - private MockServerHttpResponse response = new MockServerHttpResponse(); + private MockServerHttpResponse response; private ServerWebExchange exchange; @@ -79,8 +77,13 @@ public class ResponseBodyResultHandlerTests { @Before public void setUp() throws Exception { this.resultHandler = createHandler(); - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); - this.exchange = new DefaultServerWebExchange(request, this.response, new MockWebSessionManager()); + initExchange(); + } + + private void initExchange() { + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); + this.response = new MockServerHttpResponse(); + this.exchange = new DefaultServerWebExchange(request, this.response); } @@ -122,12 +125,14 @@ public class ResponseBodyResultHandlerTests { HandlerMethod hm = handlerMethod(controller, "handleToString"); HandlerResult handlerResult = new HandlerResult(hm, null, hm.getReturnType()); + initExchange(); StepVerifier.create(this.resultHandler.handleResult(this.exchange, handlerResult)).expectComplete().verify(); assertEquals(HttpStatus.NO_CONTENT, this.response.getStatusCode()); hm = handlerMethod(controller, "handleToMonoVoid"); handlerResult = new HandlerResult(hm, null, hm.getReturnType()); + initExchange(); StepVerifier.create(this.resultHandler.handleResult(this.exchange, handlerResult)).expectComplete().verify(); assertEquals(HttpStatus.CREATED, this.response.getStatusCode()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java index 72381d3d1c..e3038b6a17 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java @@ -27,6 +27,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; @@ -40,7 +41,6 @@ import org.springframework.core.codec.ByteBufferEncoder; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.io.buffer.support.DataBufferTestUtils; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -59,8 +59,6 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuild import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -85,19 +83,21 @@ public class ResponseEntityResultHandlerTests { private MockServerHttpRequest request; - private MockServerHttpResponse response = new MockServerHttpResponse(); - - private ServerWebExchange exchange; + private MockServerHttpResponse response; @Before public void setUp() throws Exception { this.resultHandler = createHandler(); - this.request = new MockServerHttpRequest(HttpMethod.GET, "/path"); - WebSessionManager manager = new MockWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, manager); + initExchange(); } + private void initExchange() { + this.request = MockServerHttpRequest.get("/path").build(); + this.response = new MockServerHttpResponse(); + } + + private ResponseEntityResultHandler createHandler(HttpMessageWriter... writers) { List> writerList; if (ObjectUtils.isEmpty(writers)) { @@ -152,7 +152,7 @@ public class ResponseEntityResultHandlerTests { ResponseEntity value = ResponseEntity.noContent().build(); ResolvableType type = responseEntity(Void.class); HandlerResult result = handlerResult(value, type); - this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.NO_CONTENT, this.response.getStatusCode()); assertEquals(0, this.response.getHeaders().size()); @@ -165,7 +165,7 @@ public class ResponseEntityResultHandlerTests { ResolvableType type = responseEntity(Void.class); ResponseEntity value = ResponseEntity.created(location).build(); HandlerResult result = handlerResult(value, type); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.CREATED, this.response.getStatusCode()); assertEquals(1, this.response.getHeaders().size()); @@ -178,7 +178,7 @@ public class ResponseEntityResultHandlerTests { Object returnValue = Mono.just(notFound().build()); ResolvableType returnType = forClassWithGenerics(Mono.class, responseEntity(String.class)); HandlerResult result = handlerResult(returnValue, returnType); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); assertNull(this.response.getBody()); } @@ -206,11 +206,11 @@ public class ResponseEntityResultHandlerTests { public void handleReturnValueLastModified() throws Exception { Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); - this.request.getHeaders().setIfModifiedSince(currentTime.toEpochMilli()); + this.request = MockServerHttpRequest.get("/path").ifModifiedSince(currentTime.toEpochMilli()).build(); ResponseEntity entity = ok().lastModified(oneMinAgo.toEpochMilli()).body("body"); HandlerResult result = handlerResult(entity, responseEntity(String.class)); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, null, oneMinAgo); } @@ -218,22 +218,22 @@ public class ResponseEntityResultHandlerTests { @Test public void handleReturnValueEtag() throws Exception { String etagValue = "\"deadb33f8badf00d\""; - this.request.getHeaders().setIfNoneMatch(etagValue); + this.request = MockServerHttpRequest.get("/path").ifNoneMatch(etagValue).build(); ResponseEntity entity = ok().eTag(etagValue).body("body"); HandlerResult result = handlerResult(entity, responseEntity(String.class)); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, Instant.MIN); } @Test // SPR-14559 public void handleReturnValueEtagInvalidIfNoneMatch() throws Exception { - this.request.getHeaders().setIfNoneMatch("unquoted"); + this.request = MockServerHttpRequest.get("/path").ifNoneMatch("unquoted").build(); ResponseEntity entity = ok().eTag("\"deadb33f8badf00d\"").body("body"); HandlerResult result = handlerResult(entity, responseEntity(String.class)); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); assertResponseBody("body"); @@ -242,15 +242,18 @@ public class ResponseEntityResultHandlerTests { @Test public void handleReturnValueETagAndLastModified() throws Exception { String eTag = "\"deadb33f8badf00d\""; - this.request.getHeaders().setIfNoneMatch(eTag); Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); - this.request.getHeaders().setIfModifiedSince(currentTime.toEpochMilli()); + + this.request = MockServerHttpRequest.get("/path") + .ifNoneMatch(eTag) + .ifModifiedSince(currentTime.toEpochMilli()) + .build(); ResponseEntity entity = ok().eTag(eTag).lastModified(oneMinAgo.toEpochMilli()).body("body"); HandlerResult result = handlerResult(entity, responseEntity(String.class)); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, eTag, oneMinAgo); } @@ -259,15 +262,18 @@ public class ResponseEntityResultHandlerTests { public void handleReturnValueChangedETagAndLastModified() throws Exception { String etag = "\"deadb33f8badf00d\""; String newEtag = "\"changed-etag-value\""; - this.request.getHeaders().setIfNoneMatch(etag); Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS); Instant oneMinAgo = currentTime.minusSeconds(60); - this.request.getHeaders().setIfModifiedSince(currentTime.toEpochMilli()); + + this.request = MockServerHttpRequest.get("/path") + .ifNoneMatch(etag) + .ifModifiedSince(currentTime.toEpochMilli()) + .build(); ResponseEntity entity = ok().eTag(newEtag).lastModified(oneMinAgo.toEpochMilli()).body("body"); HandlerResult result = handlerResult(entity, responseEntity(String.class)); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertConditionalResponse(HttpStatus.OK, "body", newEtag, oneMinAgo); } @@ -275,7 +281,8 @@ public class ResponseEntityResultHandlerTests { @Test // SPR-14877 public void handleMonoWithWildcardBodyType() throws Exception { - this.exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, + ServerWebExchange exchange = createExchange(); + exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.APPLICATION_JSON)); HandlerResult result = new HandlerResult(new TestController(), Mono.just(ok().body("body")), @@ -283,7 +290,7 @@ public class ResponseEntityResultHandlerTests { .name("monoResponseEntityWildcard") .resolveReturnType()); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); assertResponseBody("\"body\""); @@ -292,7 +299,8 @@ public class ResponseEntityResultHandlerTests { @Test // SPR-14877 public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception { - this.exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, + ServerWebExchange exchange = createExchange(); + exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.APPLICATION_JSON)); HandlerResult result = new HandlerResult(new TestController(), Mono.just(notFound().build()), @@ -300,7 +308,7 @@ public class ResponseEntityResultHandlerTests { .name("monoResponseEntityWildcard") .resolveReturnType()); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.NOT_FOUND, this.response.getStatusCode()); assertNull(this.response.getBody()); @@ -308,14 +316,20 @@ public class ResponseEntityResultHandlerTests { private void testHandle(Object returnValue, ResolvableType type) { + initExchange(); + HandlerResult result = handlerResult(returnValue, type); - this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5)); + this.resultHandler.handleResult(createExchange(), result).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getFirst("Content-Type")); assertResponseBody("abc"); } + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, this.response); + } private ResolvableType responseEntity(Class bodyType) { return forClassWithGenerics(ResponseEntity.class, ResolvableType.forClass(bodyType)); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java index 7fc4a81d08..99510ee152 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolverTests.java @@ -31,10 +31,13 @@ import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebSession; import org.springframework.web.server.adapter.DefaultServerWebExchange; +import org.springframework.web.server.session.MockWebSessionManager; import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; /** * Unit tests for {@link ServerWebExchangeArgumentResolver}. @@ -51,13 +54,10 @@ public class ServerWebExchangeArgumentResolverTests { @Before public void setUp() throws Exception { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); + ServerHttpRequest request = MockServerHttpRequest.get("/path").build(); ServerHttpResponse response = new MockServerHttpResponse(); - WebSession session = mock(WebSession.class); - WebSessionManager sessionManager = mock(WebSessionManager.class); - when(sessionManager.getSession(any())).thenReturn(Mono.just(session)); - + WebSessionManager sessionManager = new MockWebSessionManager(mock(WebSession.class)); this.exchange = new DefaultServerWebExchange(request, response, sessionManager); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java index ae9914dabe..dce012ff71 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java @@ -30,7 +30,6 @@ import org.springframework.core.GenericTypeResolver; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.format.support.DefaultFormattingConversionService; -import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -45,8 +44,14 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.session.MockWebSessionManager; import org.springframework.web.server.session.WebSessionManager; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Unit tests for {@link SessionAttributeMethodArgumentResolver}. @@ -71,10 +76,9 @@ public class SessionAttributeMethodArgumentResolverTests { this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory()); this.session = mock(WebSession.class); - when(this.session.getAttribute(any())).thenReturn(Optional.empty()); - - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/"); WebSessionManager sessionManager = new MockWebSessionManager(this.session); + + ServerHttpRequest request = MockServerHttpRequest.get("/").build(); this.exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse(), sessionManager); this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class[]) null); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java index 30baf2f77b..0269f54eb5 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/HttpMessageWriterViewTests.java @@ -30,7 +30,6 @@ import reactor.test.StepVerifier; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.io.buffer.support.DataBufferTestUtils; -import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.http.codec.xml.Jaxb2XmlEncoder; @@ -147,12 +146,12 @@ public class HttpMessageWriterViewTests { this.model.addAttribute("pojoData", pojoData); this.view.setModelKeys(Collections.singleton("pojoData")); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); + MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); WebSessionManager manager = new DefaultWebSessionManager(); ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); - this.view.render(this.model, MediaType.APPLICATION_JSON, exchange); + this.view.render(this.model, MediaType.APPLICATION_JSON, exchange).blockMillis(5000); StepVerifier.create(response.getBody()) .consumeNextWith( buf -> assertEquals("{\"foo\":\"f\",\"bar\":\"b\"}", diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java index 0173bb3839..010dad20e5 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java @@ -21,7 +21,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; @@ -30,10 +30,11 @@ import org.springframework.http.MediaType; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.reactive.HandlerMapping; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Tests for redirect view, and query string construction. @@ -47,18 +48,14 @@ public class RedirectViewTests { private MockServerHttpResponse response; - private ServerWebExchange exchange; - @Before public void setUp() { - request = new MockServerHttpRequest(); - request.setContextPath("/context"); - response = new MockServerHttpResponse(); - WebSessionManager sessionManager = new DefaultWebSessionManager(); - exchange = new DefaultServerWebExchange(request, response, sessionManager); + this.request = MockServerHttpRequest.get("/").contextPath("/context").build(); + this.response = new MockServerHttpResponse(); } + @Test(expected = IllegalArgumentException.class) public void noUrlSet() throws Exception { RedirectView rv = new RedirectView(null); @@ -69,34 +66,34 @@ public class RedirectViewTests { public void defaultStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url); - view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode()); - assertEquals(URI.create(url), response.getHeaders().getLocation()); + view.render(new HashMap<>(), MediaType.TEXT_HTML, createExchange()); + assertEquals(HttpStatus.SEE_OTHER, this.response.getStatusCode()); + assertEquals(URI.create(url), this.response.getHeaders().getLocation()); } @Test public void customStatusCode() { String url = "http://url.somewhere.com"; RedirectView view = new RedirectView(url, HttpStatus.FOUND); - view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(HttpStatus.FOUND, response.getStatusCode()); - assertEquals(URI.create(url), response.getHeaders().getLocation()); + view.render(new HashMap<>(), MediaType.TEXT_HTML, createExchange()); + assertEquals(HttpStatus.FOUND, this.response.getStatusCode()); + assertEquals(URI.create(url), this.response.getHeaders().getLocation()); } @Test public void contextRelative() { String url = "/test.html"; RedirectView view = new RedirectView(url); - view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(URI.create("/context/test.html"), response.getHeaders().getLocation()); + view.render(new HashMap<>(), MediaType.TEXT_HTML, createExchange()); + assertEquals(URI.create("/context/test.html"), this.response.getHeaders().getLocation()); } @Test public void contextRelativeQueryParam() { String url = "/test.html?id=1"; RedirectView view = new RedirectView(url); - view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(URI.create("/context/test.html?id=1"), response.getHeaders().getLocation()); + view.render(new HashMap<>(), MediaType.TEXT_HTML, createExchange()); + assertEquals(URI.create("/context/test.html?id=1"), this.response.getHeaders().getLocation()); } @Test @@ -119,28 +116,35 @@ public class RedirectViewTests { String url = "http://url.somewhere.com?foo={foo}"; Map model = Collections.singletonMap("foo", "bar"); RedirectView view = new RedirectView(url); - view.render(model, MediaType.TEXT_HTML, exchange); - assertEquals(URI.create("http://url.somewhere.com?foo=bar"), response.getHeaders().getLocation()); + view.render(model, MediaType.TEXT_HTML, createExchange()); + assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.response.getHeaders().getLocation()); } @Test public void expandUriTemplateVariablesFromExchangeAttribute() { String url = "http://url.somewhere.com?foo={foo}"; Map attributes = Collections.singletonMap("foo", "bar"); + DefaultServerWebExchange exchange = createExchange(); exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, attributes); RedirectView view = new RedirectView(url); view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(URI.create("http://url.somewhere.com?foo=bar"), response.getHeaders().getLocation()); + assertEquals(URI.create("http://url.somewhere.com?foo=bar"), this.response.getHeaders().getLocation()); } @Test public void propagateQueryParams() throws Exception { RedirectView view = new RedirectView("http://url.somewhere.com?foo=bar#bazz"); view.setPropagateQuery(true); - request.setUri(URI.create("http://url.somewhere.com?a=b&c=d")); - view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange); - assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode()); - assertEquals(URI.create("http://url.somewhere.com?foo=bar&a=b&c=d#bazz"), response.getHeaders().getLocation()); + this.request = MockServerHttpRequest.get("http://url.somewhere.com?a=b&c=d").build(); + view.render(new HashMap<>(), MediaType.TEXT_HTML, createExchange()); + assertEquals(HttpStatus.SEE_OTHER, this.response.getStatusCode()); + assertEquals(URI.create("http://url.somewhere.com?foo=bar&a=b&c=d#bazz"), + this.response.getHeaders().getLocation()); + } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, this.response); } } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RequestContextTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RequestContextTests.java index 171d0db7a5..327e017c72 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RequestContextTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RequestContextTests.java @@ -27,7 +27,6 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; import static org.junit.Assert.assertEquals; @@ -39,8 +38,6 @@ public class RequestContextTests { private ServerWebExchange exchange; - private MockServerHttpRequest request; - private GenericApplicationContext applicationContext; private Map model = new HashMap<>(); @@ -48,24 +45,21 @@ public class RequestContextTests { @Before public void init() { - this.request = new MockServerHttpRequest(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").contextPath("foo/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); - DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, response, sessionManager); + this.exchange = new DefaultServerWebExchange(request, response); this.applicationContext = new GenericApplicationContext(); this.applicationContext.refresh(); } @Test public void testGetContextUrl() throws Exception { - this.request.setContextPath("foo/"); RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext); assertEquals("foo/bar", context.getContextUrl("bar")); } @Test public void testGetContextUrlWithMap() throws Exception { - this.request.setContextPath("foo/"); RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext); Map map = new HashMap<>(); map.put("foo", "bar"); @@ -75,7 +69,6 @@ public class RequestContextTests { @Test public void testGetContextUrlWithMapEscaping() throws Exception { - this.request.setContextPath("foo/"); RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext); Map map = new HashMap<>(); map.put("foo", "bar baz"); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java index 6c50b03ba3..60354364e0 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java @@ -41,7 +41,6 @@ import org.springframework.core.Ordered; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DefaultDataBufferFactory; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpResponse; @@ -59,15 +58,14 @@ import org.springframework.web.reactive.result.ResolvableMethod; import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; -import static java.nio.charset.StandardCharsets.*; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; -import static org.springframework.core.ResolvableType.*; -import static org.springframework.core.io.buffer.support.DataBufferTestUtils.*; -import static org.springframework.http.MediaType.*; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.springframework.core.ResolvableType.forClass; +import static org.springframework.core.ResolvableType.forClassWithGenerics; +import static org.springframework.core.io.buffer.support.DataBufferTestUtils.dumpString; +import static org.springframework.http.MediaType.APPLICATION_JSON; /** * Unit tests for {@link ViewResolutionResultHandler}. @@ -75,19 +73,14 @@ import static org.springframework.http.MediaType.*; */ public class ViewResolutionResultHandlerTests { - private final MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); - - private final MockServerHttpResponse response = new MockServerHttpResponse(); - - private ServerWebExchange exchange; + private MockServerHttpRequest request; private final BindingContext bindingContext = new BindingContext(); @Before public void setUp() throws Exception { - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, manager); + this.request = MockServerHttpRequest.get("/path").build(); } @@ -138,23 +131,23 @@ public class ViewResolutionResultHandlerTests { returnType = forClass(View.class); returnValue = new TestView("account"); - testHandle("/path", returnType, returnValue, "account: {id=123}"); - assertEquals(HttpStatus.NO_CONTENT, this.exchange.getResponse().getStatusCode()); + ServerWebExchange exchange = testHandle("/path", returnType, returnValue, "account: {id=123}"); + assertEquals(HttpStatus.NO_CONTENT, exchange.getResponse().getStatusCode()); returnType = forClassWithGenerics(Mono.class, View.class); returnValue = Mono.just(new TestView("account")); - testHandle("/path", returnType, returnValue, "account: {id=123}"); - assertEquals(HttpStatus.SEE_OTHER, this.exchange.getResponse().getStatusCode()); + exchange = testHandle("/path", returnType, returnValue, "account: {id=123}"); + assertEquals(HttpStatus.SEE_OTHER, exchange.getResponse().getStatusCode()); returnType = forClass(String.class); returnValue = "account"; - testHandle("/path", returnType, returnValue, "account: {id=123}", resolver); - assertEquals(HttpStatus.CREATED, this.exchange.getResponse().getStatusCode()); + exchange = testHandle("/path", returnType, returnValue, "account: {id=123}", resolver); + assertEquals(HttpStatus.CREATED, exchange.getResponse().getStatusCode()); returnType = forClassWithGenerics(Mono.class, String.class); returnValue = Mono.just("account"); - testHandle("/path", returnType, returnValue, "account: {id=123}", resolver); - assertEquals(HttpStatus.PARTIAL_CONTENT, this.exchange.getResponse().getStatusCode()); + exchange = testHandle("/path", returnType, returnValue, "account: {id=123}", resolver); + assertEquals(HttpStatus.PARTIAL_CONTENT, exchange.getResponse().getStatusCode()); returnType = forClass(Model.class); returnValue = new ConcurrentModel().addAttribute("name", "Joe"); @@ -200,17 +193,20 @@ public class ViewResolutionResultHandlerTests { HandlerResult result = new HandlerResult(new Object(), returnValue, returnType(type), this.bindingContext); ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account")); - this.request.setUri("/account"); - handler.handleResult(this.exchange, result).blockMillis(5000); - assertResponseBody("account: {id=123}"); + this.request = MockServerHttpRequest.get("/account").build(); + ServerWebExchange exchange = createExchange(); + handler.handleResult(exchange, result).blockMillis(5000); + assertResponseBody(exchange, "account: {id=123}"); - this.request.setUri("/account/"); - handler.handleResult(this.exchange, result).blockMillis(5000); - assertResponseBody("account: {id=123}"); + this.request = MockServerHttpRequest.get("/account/").build(); + exchange = createExchange(); + handler.handleResult(exchange, result).blockMillis(5000); + assertResponseBody(exchange, "account: {id=123}"); - this.request.setUri("/account.123"); - handler.handleResult(this.exchange, result).blockMillis(5000); - assertResponseBody("account: {id=123}"); + this.request = MockServerHttpRequest.get("/account.123").build(); + exchange = createExchange(); + handler.handleResult(exchange, result).blockMillis(5000); + assertResponseBody(exchange, "account: {id=123}"); } @Test @@ -219,8 +215,9 @@ public class ViewResolutionResultHandlerTests { MethodParameter returnType = returnType(forClass(String.class)); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); - this.request.setUri("/path"); - Mono mono = resultHandler().handleResult(this.exchange, result); + this.request = MockServerHttpRequest.get("/path").build(); + ServerWebExchange exchange = createExchange(); + Mono mono = resultHandler().handleResult(exchange, result); StepVerifier.create(mono) .expectNextCount(0) @@ -234,17 +231,17 @@ public class ViewResolutionResultHandlerTests { MethodParameter returnType = returnType(forClass(TestBean.class)); HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); - this.request.setHeader("Accept", "application/json"); - this.request.setUri("/account"); + this.request = MockServerHttpRequest.get("/account").accept(APPLICATION_JSON).build(); + ServerWebExchange exchange = createExchange(); TestView defaultView = new TestView("jsonView", APPLICATION_JSON); resultHandler(Collections.singletonList(defaultView), new TestViewResolver("account")) - .handleResult(this.exchange, handlerResult) + .handleResult(exchange, handlerResult) .block(Duration.ofSeconds(5)); - assertEquals(APPLICATION_JSON, this.response.getHeaders().getContentType()); - assertResponseBody("jsonView: {" + + assertEquals(APPLICATION_JSON, exchange.getResponse().getHeaders().getContentType()); + assertResponseBody(exchange, "jsonView: {" + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]" + @@ -257,11 +254,11 @@ public class ViewResolutionResultHandlerTests { MethodParameter returnType = returnType(forClass(TestBean.class)); HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); - this.request.setHeader("Accept", "application/json"); - this.request.setUri("/account"); + this.request = MockServerHttpRequest.get("/account").accept(APPLICATION_JSON).build(); + ServerWebExchange exchange = createExchange(); ViewResolutionResultHandler resultHandler = resultHandler(new TestViewResolver("account")); - Mono mono = resultHandler.handleResult(this.exchange, handlerResult); + Mono mono = resultHandler.handleResult(exchange, handlerResult); StepVerifier.create(mono) .expectNextCount(0) .expectError(NotAcceptableStatusException.class) @@ -281,9 +278,11 @@ public class ViewResolutionResultHandlerTests { HandlerResult result = new HandlerResult(new Object(), null, returnType(type), this.bindingContext); ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account")); - this.request.setUri("/account"); - handler.handleResult(this.exchange, result).blockMillis(5000); - assertResponseBody("account: {" + + this.request = MockServerHttpRequest.get("/account").build(); + ServerWebExchange exchange = createExchange(); + + handler.handleResult(exchange, result).blockMillis(5000); + assertResponseBody(exchange, "account: {" + "attr1=TestBean[name=Bean1], " + "attr2=[TestBean[name=Bean1], TestBean[name=Bean2]], " + "attr3=TestBean[name=Bean2], " + @@ -295,6 +294,9 @@ public class ViewResolutionResultHandlerTests { "}"); } + private ServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, new MockServerHttpResponse()); + } private MethodParameter returnType(ResolvableType type) { return resolvableMethod().returning(type).resolveReturnType(); @@ -316,13 +318,13 @@ public class ViewResolutionResultHandlerTests { return ResolvableMethod.onClass(TestController.class); } - private void testHandle(String path, ResolvableType returnType, Object returnValue, + private ServerWebExchange testHandle(String path, ResolvableType returnType, Object returnValue, String responseBody, ViewResolver... resolvers) throws URISyntaxException { - testHandle(path, resolvableMethod().returning(returnType), returnValue, responseBody, resolvers); + return testHandle(path, resolvableMethod().returning(returnType), returnValue, responseBody, resolvers); } - private void testHandle(String path, ResolvableMethod resolvableMethod, Object returnValue, + private ServerWebExchange testHandle(String path, ResolvableMethod resolvableMethod, Object returnValue, String responseBody, ViewResolver... resolvers) throws URISyntaxException { Model model = this.bindingContext.getModel(); @@ -330,13 +332,16 @@ public class ViewResolutionResultHandlerTests { model.addAttribute("id", "123"); MethodParameter returnType = resolvableMethod.resolveReturnType(); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); - this.request.setUri(path); - resultHandler(resolvers).handleResult(this.exchange, result).block(Duration.ofSeconds(5)); - assertResponseBody(responseBody); + this.request = MockServerHttpRequest.get(path).build(); + ServerWebExchange exchange = createExchange(); + resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5)); + assertResponseBody(exchange, responseBody); + return exchange; } - private void assertResponseBody(String responseBody) { - StepVerifier.create(this.response.getBody()) + private void assertResponseBody(ServerWebExchange exchange, String responseBody) { + MockServerHttpResponse response = (MockServerHttpResponse) exchange.getResponse(); + StepVerifier.create(response.getBody()) .consumeNextWith(buf -> assertEquals(responseBody, dumpString(buf, UTF_8))) .expectComplete() .verify(); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java index f55d9207bd..5e8d56d97e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java @@ -30,15 +30,12 @@ import reactor.test.StepVerifier; import org.springframework.context.ApplicationContextException; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ModelMap; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -48,7 +45,7 @@ import static org.junit.Assert.assertTrue; */ public class FreeMarkerViewTests { - public static final String TEMPLATE_PATH = "classpath*:org/springframework/web/reactive/view/freemarker/"; + private static final String TEMPLATE_PATH = "classpath*:org/springframework/web/reactive/view/freemarker/"; private ServerWebExchange exchange; @@ -77,10 +74,9 @@ public class FreeMarkerViewTests { FreeMarkerView fv = new FreeMarkerView(); fv.setApplicationContext(this.context); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); + MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); this.response = new MockServerHttpResponse(); - WebSessionManager manager = new DefaultWebSessionManager(); - this.exchange = new DefaultServerWebExchange(request, response, manager); + this.exchange = new DefaultServerWebExchange(request, this.response); } @@ -121,12 +117,10 @@ public class FreeMarkerViewTests { ModelMap model = new ExtendedModelMap(); model.addAttribute("hello", "hi FreeMarker"); - view.render(model, null, this.exchange); + view.render(model, null, this.exchange).blockMillis(5000); StepVerifier.create(this.response.getBody()) - .consumeNextWith(buf -> { - assertEquals("hi FreeMarker", asString(buf)); - }) + .consumeNextWith(buf -> assertEquals("hi FreeMarker", asString(buf))) .expectComplete() .verify(); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JRubyScriptTemplateTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JRubyScriptTemplateTests.java index b1f8c692e4..ee3cbb1c3e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JRubyScriptTemplateTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JRubyScriptTemplateTests.java @@ -19,7 +19,6 @@ package org.springframework.web.reactive.result.view.script; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -36,6 +35,8 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.WebSessionManager; +import static org.junit.Assert.assertEquals; + /** * Unit tests for ERB templates running on JRuby. * @@ -63,7 +64,7 @@ public class JRubyScriptTemplateTests { private MockServerHttpResponse renderViewWithModel(String viewUrl, Map model) throws Exception { ScriptTemplateView view = createViewWithUrl(viewUrl); - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); WebSessionManager manager = new DefaultWebSessionManager(); ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JythonScriptTemplateTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JythonScriptTemplateTests.java index b08e0aa701..861df51b8e 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JythonScriptTemplateTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/JythonScriptTemplateTests.java @@ -62,7 +62,7 @@ public class JythonScriptTemplateTests { private MockServerHttpResponse renderViewWithModel(String viewUrl, Map model) throws Exception { ScriptTemplateView view = createViewWithUrl(viewUrl); - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); WebSessionManager manager = new DefaultWebSessionManager(); ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/KotlinScriptTemplateTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/KotlinScriptTemplateTests.java index acede99e9c..999de640dc 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/KotlinScriptTemplateTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/KotlinScriptTemplateTests.java @@ -64,7 +64,7 @@ public class KotlinScriptTemplateTests { private MockServerHttpResponse renderViewWithModel(String viewUrl, Map model, Class configuration) throws Exception { ScriptTemplateView view = createViewWithUrl(viewUrl, configuration); - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); WebSessionManager manager = new DefaultWebSessionManager(); ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/NashornScriptTemplateTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/NashornScriptTemplateTests.java index 2cc17ef821..14e2451c01 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/NashornScriptTemplateTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/script/NashornScriptTemplateTests.java @@ -70,7 +70,7 @@ public class NashornScriptTemplateTests { private MockServerHttpResponse renderViewWithModel(String viewUrl, Map model, Class configuration) throws Exception { ScriptTemplateView view = createViewWithUrl(viewUrl, configuration); - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); WebSessionManager manager = new DefaultWebSessionManager(); ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager); diff --git a/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt b/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt index 2d83546f12..4330414f99 100644 --- a/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt +++ b/spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/result/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt @@ -5,17 +5,14 @@ import org.junit.Test import org.springframework.core.MethodParameter import org.springframework.core.annotation.SynthesizingMethodParameter import org.springframework.format.support.DefaultFormattingConversionService -import org.springframework.http.HttpMethod import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse import org.springframework.util.ReflectionUtils import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.support.ConfigurableWebBindingInitializer import org.springframework.web.reactive.BindingContext -import org.springframework.web.server.ServerWebExchange import org.springframework.web.server.ServerWebInputException import org.springframework.web.server.adapter.DefaultServerWebExchange -import org.springframework.web.server.session.MockWebSessionManager import reactor.test.StepVerifier /** @@ -26,7 +23,7 @@ import reactor.test.StepVerifier class RequestParamMethodArgumentResolverKotlinTests { lateinit var resolver: RequestParamMethodArgumentResolver - lateinit var exchange: ServerWebExchange + lateinit var request: MockServerHttpRequest lateinit var bindingContext: BindingContext lateinit var nullableParamRequired: MethodParameter @@ -37,10 +34,8 @@ class RequestParamMethodArgumentResolverKotlinTests { @Before fun setup() { - resolver = RequestParamMethodArgumentResolver(null, true) - val request = MockServerHttpRequest(HttpMethod.GET, "/") - val sessionManager = MockWebSessionManager() - exchange = DefaultServerWebExchange(request, MockServerHttpResponse(), sessionManager) + this.resolver = RequestParamMethodArgumentResolver(null, true) + this.request = MockServerHttpRequest.get("/").build() val initializer = ConfigurableWebBindingInitializer() initializer.conversionService = DefaultFormattingConversionService() bindingContext = BindingContext(initializer) @@ -56,56 +51,58 @@ class RequestParamMethodArgumentResolverKotlinTests { @Test fun resolveNullableRequiredWithParameter() { - exchange.request.queryParams.set("name", "123") - var result = resolver.resolveArgument(nullableParamRequired, bindingContext, exchange) + this.request = MockServerHttpRequest.get("/path?name=123").build() + var result = resolver.resolveArgument(nullableParamRequired, bindingContext, createExchange()) StepVerifier.create(result).expectNext("123").expectComplete().verify() } @Test fun resolveNullableRequiredWithoutParameter() { - var result = resolver.resolveArgument(nullableParamRequired, bindingContext, exchange) + var result = resolver.resolveArgument(nullableParamRequired, bindingContext, createExchange()) StepVerifier.create(result).expectComplete().verify() } @Test fun resolveNullableNotRequiredWithParameter() { - exchange.request.queryParams.set("name", "123") - var result = resolver.resolveArgument(nullableParamNotRequired, bindingContext, exchange) + this.request = MockServerHttpRequest.get("/path?name=123").build() + var result = resolver.resolveArgument(nullableParamNotRequired, bindingContext, createExchange()) StepVerifier.create(result).expectNext("123").expectComplete().verify() } @Test fun resolveNullableNotRequiredWithoutParameter() { - var result = resolver.resolveArgument(nullableParamNotRequired, bindingContext, exchange) + var result = resolver.resolveArgument(nullableParamNotRequired, bindingContext, createExchange()) StepVerifier.create(result).expectComplete().verify() } @Test fun resolveNonNullableRequiredWithParameter() { - exchange.request.queryParams.set("name", "123") - var result = resolver.resolveArgument(nonNullableParamRequired, bindingContext, exchange) + this.request = MockServerHttpRequest.get("/path?name=123").build() + var result = resolver.resolveArgument(nonNullableParamRequired, bindingContext, createExchange()) StepVerifier.create(result).expectNext("123").expectComplete().verify() } @Test fun resolveNonNullableRequiredWithoutParameter() { - var result = resolver.resolveArgument(nonNullableParamRequired, bindingContext, exchange) + var result = resolver.resolveArgument(nonNullableParamRequired, bindingContext, createExchange()) StepVerifier.create(result).expectError(ServerWebInputException::class.java).verify() } @Test fun resolveNonNullableNotRequiredWithParameter() { - exchange.request.queryParams.set("name", "123") - var result = resolver.resolveArgument(nonNullableParamNotRequired, bindingContext, exchange) + this.request = MockServerHttpRequest.get("/path?name=123").build() + var result = resolver.resolveArgument(nonNullableParamNotRequired, bindingContext, createExchange()) StepVerifier.create(result).expectNext("123").expectComplete().verify() } @Test fun resolveNonNullableNotRequiredWithoutParameter() { - var result = resolver.resolveArgument(nonNullableParamNotRequired, bindingContext, exchange) + var result = resolver.resolveArgument(nonNullableParamNotRequired, bindingContext, createExchange()) StepVerifier.create(result).expectComplete().verify() } + private fun createExchange() = DefaultServerWebExchange(this.request, MockServerHttpResponse()) + @Suppress("unused_parameter") fun handle( diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index 3e222219f8..051bbfe138 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -44,6 +44,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebSession; +import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.WebSessionManager; /** @@ -81,6 +82,17 @@ public class DefaultServerWebExchange implements ServerWebExchange { private volatile boolean notModified; + /** + * Constructor with a request and response only. + * By default creates a session manager of type {@link DefaultWebSessionManager}. + */ + public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response) { + this(request, response, new DefaultWebSessionManager()); + } + + /** + * Alternate constructor with a WebSessionManager parameter. + */ public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response, WebSessionManager sessionManager) { diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java b/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java index 6be91d79f1..0c3958d4c8 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java @@ -193,6 +193,7 @@ public class WebHttpHandlerBuilder { } WebExceptionHandler[] array = new WebExceptionHandler[this.exceptionHandlers.size()]; webHandler = new ExceptionHandlingWebHandler(webHandler, this.exceptionHandlers.toArray(array)); + // TODO: protected method for further decoration HttpWebHandlerAdapter httpHandler = new HttpWebHandlerAdapter(webHandler); if (this.sessionManager != null) { httpHandler.setSessionManager(this.sessionManager); diff --git a/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java b/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java index e9bd2eaea8..30be22dfd8 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/EncoderHttpMessageWriterTests.java @@ -65,7 +65,7 @@ public class EncoderHttpMessageWriterTests { EncoderHttpMessageWriter writer = createWriter(new ByteBufferEncoder()); writer.write(source, ResolvableType.forClass(ByteBuffer.class), - MediaType.APPLICATION_OCTET_STREAM, this.response, Collections.emptyMap()); + MediaType.APPLICATION_OCTET_STREAM, this.response, Collections.emptyMap()).blockMillis(5000); assertThat(this.response.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM)); StepVerifier.create(this.response.getBodyAsString()) diff --git a/spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java b/spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java index c037eda5bc..2da57b10ac 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/FormHttpMessageReaderTests.java @@ -23,6 +23,8 @@ import static org.junit.Assert.*; import org.junit.Test; import org.springframework.core.ResolvableType; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.util.MultiValueMap; @@ -60,9 +62,7 @@ public class FormHttpMessageReaderTests { @Test public void readFormAsMono() { String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"; - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(body); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MockServerHttpRequest request = request(body); MultiValueMap result = this.reader.readMono(null, request, null).block(); assertEquals("Invalid result", 3, result.size()); @@ -77,9 +77,7 @@ public class FormHttpMessageReaderTests { @Test public void readFormAsFlux() { String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3"; - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(body); - request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); + MockServerHttpRequest request = request(body); MultiValueMap result = this.reader.read(null, request, null).single().block(); assertEquals("Invalid result", 3, result.size()); @@ -91,4 +89,11 @@ public class FormHttpMessageReaderTests { assertNull("Invalid result", result.getFirst("name 3")); } + private MockServerHttpRequest request(String body) { + return MockServerHttpRequest + .method(HttpMethod.GET, "/") + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) + .body(body); + } + } diff --git a/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageWriterTests.java b/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageWriterTests.java index f13bfd58e2..a30bee7a71 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageWriterTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/ResourceHttpMessageWriterTests.java @@ -18,6 +18,7 @@ package org.springframework.http.codec; import java.nio.charset.StandardCharsets; import java.util.Collections; +import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -35,8 +36,10 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.util.MimeTypeUtils; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get; /** * Unit tests for {@link ResourceHttpMessageWriter}. @@ -47,8 +50,6 @@ public class ResourceHttpMessageWriterTests { private ResourceHttpMessageWriter writer = new ResourceHttpMessageWriter(); - private MockServerHttpRequest request = new MockServerHttpRequest(); - private MockServerHttpResponse response = new MockServerHttpResponse(); private Resource resource; @@ -69,14 +70,8 @@ public class ResourceHttpMessageWriterTests { @Test public void shouldWriteResource() throws Exception { - - Mono mono = this.writer.write(Mono.just(resource), null, - ResolvableType.forClass(Resource.class), - MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap()); - StepVerifier.create(mono) - .expectNextCount(0) - .expectComplete() - .verify(); + MockServerHttpRequest request = get("/").build(); + testWrite(request); assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN)); assertThat(this.response.getHeaders().getContentLength(), is(39L)); @@ -91,13 +86,8 @@ public class ResourceHttpMessageWriterTests { @Test public void shouldWriteResourceRange() throws Exception { - this.request.getHeaders().setRange(Collections.singletonList(HttpRange.createByteRange(0, 5))); - Mono mono = this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class), - MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap()); - StepVerifier.create(mono) - .expectNextCount(0) - .expectComplete() - .verify(); + MockServerHttpRequest request = get("/").range(HttpRange.createByteRange(0, 5)).build(); + testWrite(request); assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN)); assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/39")); @@ -105,25 +95,25 @@ public class ResourceHttpMessageWriterTests { assertThat(this.response.getHeaders().getContentLength(), is(6L)); Mono result = this.response.getBodyAsString(); - StepVerifier.create(result) - .expectNext("Spring") - .expectComplete() - .verify(); + StepVerifier.create(result).expectNext("Spring").expectComplete().verify(); } @Test public void shouldSetRangeNotSatisfiableStatus() throws Exception { - this.request.getHeaders().set(HttpHeaders.RANGE, "invalid"); - - Mono mono = this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class), - MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap()); - StepVerifier.create(mono) - .expectNextCount(0) - .expectComplete() - .verify(); + MockServerHttpRequest request = get("/").header(HttpHeaders.RANGE, "invalid").build(); + testWrite(request); assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes")); assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)); } + private void testWrite(MockServerHttpRequest request) { + Mono input = Mono.just(this.resource); + ResolvableType type = ResolvableType.forClass(Resource.class); + MediaType contentType = MediaType.TEXT_PLAIN; + Map hints = Collections.emptyMap(); + Mono mono = this.writer.write(input, null, type, contentType, request, this.response, hints); + StepVerifier.create(mono).expectNextCount(0).expectComplete().verify(); + } + } diff --git a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java index 84af432c41..dd725b4252 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java @@ -19,9 +19,6 @@ package org.springframework.http.codec; import java.time.Duration; import java.util.Collections; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Test; import reactor.core.publisher.Flux; import reactor.test.StepVerifier; @@ -32,6 +29,10 @@ import org.springframework.http.MediaType; import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + /** * @author Sebastien Deleuze */ @@ -57,10 +58,12 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll @Test public void readServerSentEvents() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody("id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" + + + MockServerHttpRequest request = MockServerHttpRequest.post("/").body( + "id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n\n" + "id:c43\nevent:bar\nretry:456\ndata:baz\n\n"); - Flux events = messageReader + + Flux events = this.messageReader .read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), request, Collections.emptyMap()).cast(ServerSentEvent.class); @@ -85,9 +88,13 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll @Test public void readServerSentEventsWithMultipleChunks() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody(Flux.just(stringBuffer("id:c42\nev"), stringBuffer("ent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:"), - stringBuffer("bar\n\nid:c43\nevent:bar\nretry:456\ndata:baz\n\n"))); + + MockServerHttpRequest request = MockServerHttpRequest.post("/") + .body(Flux.just( + stringBuffer("id:c42\nev"), + stringBuffer("ent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:"), + stringBuffer("bar\n\nid:c43\nevent:bar\nretry:456\ndata:baz\n\n"))); + Flux events = messageReader .read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), request, Collections.emptyMap()).cast(ServerSentEvent.class); @@ -113,8 +120,10 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll @Test public void readString() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody("data:foo\ndata:bar\n\ndata:baz\n\n"); + + String body = "data:foo\ndata:bar\n\ndata:baz\n\n"; + MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body); + Flux data = messageReader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class); @@ -127,9 +136,11 @@ public class ServerSentEventHttpMessageReaderTests extends AbstractDataBufferAll @Test public void readPojo() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setBody("data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" + - "data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n"); + + MockServerHttpRequest request = MockServerHttpRequest.post("/").body( + "data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" + + "data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n"); + Flux data = messageReader.read(ResolvableType.forClass(Pojo.class), request, Collections.emptyMap()).cast(Pojo.class); diff --git a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java index 70e3fef2bb..691b481ccf 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageWriterTests.java @@ -18,24 +18,22 @@ package org.springframework.http.codec; import java.time.Duration; import java.util.Collections; -import java.util.function.Consumer; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; -import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import org.springframework.core.ResolvableType; import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase; -import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.MediaType; import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Sebastien Deleuze @@ -69,12 +67,16 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll Mono> source = Mono.just(event); MockServerHttpResponse outputMessage = new MockServerHttpResponse(); messageWriter.write(source, ResolvableType.forClass(ServerSentEvent.class), - new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()); + new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()).blockMillis(5000); - Publisher> result = Flux.from(outputMessage.getBodyWithFlush()); - StepVerifier.create(result) - .consumeNextWith(sseConsumer( - "id:c42\nevent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:bar\n")) + StepVerifier.create(outputMessage.getBodyAsString()) + .expectNext("id:c42\n" + + "event:foo\n" + + "retry:123\n" + + ":bla\n" + + ":bla bla\n" + + ":bla bla bla\n" + + "data:bar\n\n") .expectComplete() .verify(); } @@ -84,12 +86,10 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll Flux source = Flux.just("foo", "bar"); MockServerHttpResponse outputMessage = new MockServerHttpResponse(); messageWriter.write(source, ResolvableType.forClass(String.class), - new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()); + new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()).blockMillis(5000); - Publisher> result = outputMessage.getBodyWithFlush(); - StepVerifier.create(result) - .consumeNextWith(sseConsumer("data:foo\n")) - .consumeNextWith(sseConsumer("data:bar\n")) + StepVerifier.create(outputMessage.getBodyAsString()) + .expectNext("data:foo\n\ndata:bar\n\n") .expectComplete() .verify(); } @@ -99,12 +99,13 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll Flux source = Flux.just("foo\nbar", "foo\nbaz"); MockServerHttpResponse outputMessage = new MockServerHttpResponse(); messageWriter.write(source, ResolvableType.forClass(String.class), - new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()); + new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()).blockMillis(5000); - Publisher> result = outputMessage.getBodyWithFlush(); - StepVerifier.create(result) - .consumeNextWith(sseConsumer("data:foo\ndata:bar\n")) - .consumeNextWith(sseConsumer("data:foo\ndata:baz\n")) + StepVerifier.create(outputMessage.getBodyAsString()) + .expectNext("data:foo\n" + + "data:bar\n\n" + + "data:foo\n" + + "data:baz\n\n") .expectComplete() .verify(); } @@ -115,12 +116,11 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll new Pojo("foofoofoo", "barbarbar")); MockServerHttpResponse outputMessage = new MockServerHttpResponse(); messageWriter.write(source, ResolvableType.forClass(Pojo.class), - MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()); + MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).blockMillis(5000); - Publisher> result = outputMessage.getBodyWithFlush(); - StepVerifier.create(result) - .consumeNextWith(sseConsumer("data:", "{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", "\n")) - .consumeNextWith(sseConsumer("data:", "{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}", "\n")) + StepVerifier.create(outputMessage.getBodyAsString()) + .expectNext("data:{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n\n" + + "data:{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n\n") .expectComplete() .verify(); } @@ -128,35 +128,24 @@ public class ServerSentEventHttpMessageWriterTests extends AbstractDataBufferAll @Test // SPR-14899 public void writePojoWithPrettyPrint() { ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build(); - this.messageWriter = new ServerSentEventHttpMessageWriter(Collections.singletonList(new Jackson2JsonEncoder(mapper))); + this.messageWriter = new ServerSentEventHttpMessageWriter( + Collections.singletonList(new Jackson2JsonEncoder(mapper))); Flux source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); MockServerHttpResponse outputMessage = new MockServerHttpResponse(); messageWriter.write(source, ResolvableType.forClass(Pojo.class), - MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()); + MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).blockMillis(5000); - Publisher> result = outputMessage.getBodyWithFlush(); - StepVerifier.create(result) - .consumeNextWith(sseConsumer("data:", "{\n" + + StepVerifier.create(outputMessage.getBodyAsString()) + .expectNext("data:{\n" + "data: \"foo\" : \"foofoo\",\n" + - "data: \"bar\" : \"barbar\"\n" + "data:}", "\n")) - .consumeNextWith(sseConsumer("data:", "{\n" + + "data: \"bar\" : \"barbar\"\n" + "data:}\n\n" + + "data:{\n" + "data: \"foo\" : \"foofoofoo\",\n" + - "data: \"bar\" : \"barbarbar\"\n" + "data:}", "\n")) + "data: \"bar\" : \"barbarbar\"\n" + "data:}\n\n") .expectComplete() .verify(); } - - private Consumer> sseConsumer(String... expected) { - return publisher -> { - StepVerifier.Step builder = StepVerifier.create(publisher); - for (String value : expected) { - builder = builder.consumeNextWith(stringConsumer(value)); - } - builder.consumeNextWith(stringConsumer("\n")).expectComplete().verify(); - }; - } - } diff --git a/spring-web/src/test/java/org/springframework/http/server/reactive/HttpHandlerAdapterSupportTests.java b/spring-web/src/test/java/org/springframework/http/server/reactive/HttpHandlerAdapterSupportTests.java index 6e32bb2339..8f358fae7e 100644 --- a/spring-web/src/test/java/org/springframework/http/server/reactive/HttpHandlerAdapterSupportTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/reactive/HttpHandlerAdapterSupportTests.java @@ -24,7 +24,6 @@ import java.util.Map; import org.junit.Test; import reactor.core.publisher.Mono; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -94,8 +93,10 @@ public class HttpHandlerAdapterSupportTests { @Test public void matchWithNativeContextPath() throws Exception { - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/yet/another/path"); - request.setContextPath("/yet"); // contextPath in underlying request + MockServerHttpRequest request = MockServerHttpRequest + .get("/yet/another/path") + .contextPath("/yet") // contextPath in underlying request + .build(); TestHttpHandler handler = new TestHttpHandler(); Map map = Collections.singletonMap("/another/path", handler); @@ -145,7 +146,7 @@ public class HttpHandlerAdapterSupportTests { } public ServerHttpResponse handle(String path) { - ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, path); + ServerHttpRequest request = MockServerHttpRequest.get(path).build(); return handle(request); } diff --git a/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpRequest.java b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpRequest.java new file mode 100644 index 0000000000..c8dfc62587 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpRequest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2002-2016 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 + * + * http://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.mock.http.client.reactive.test; + +import java.net.URI; + +import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferFactory; +import org.springframework.core.io.buffer.DefaultDataBufferFactory; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.reactive.AbstractClientHttpRequest; +import org.springframework.http.client.reactive.ClientHttpRequest; +import org.springframework.web.util.UriComponentsBuilder; + +/** + * Mock implementation of {@link ClientHttpRequest}. + * @author Brian Clozel + * @author Rossen Stoyanchev + * @since 5.0 + */ +public class MockClientHttpRequest extends AbstractClientHttpRequest { + + private HttpMethod httpMethod; + + private URI url; + + private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); + + private Flux body; + + + public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) { + this(httpMethod, UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri()); + } + + public MockClientHttpRequest(HttpMethod httpMethod, URI url) { + this.httpMethod = httpMethod; + this.url = url; + } + + + @Override + public HttpMethod getMethod() { + return this.httpMethod; + } + + @Override + public URI getURI() { + return this.url; + } + + @Override + public DataBufferFactory bufferFactory() { + return this.bufferFactory; + } + + public Flux getBody() { + return this.body; + } + + @Override + public Mono writeWith(Publisher body) { + this.body = Flux.from(body); + return doCommit(() -> { + this.body = Flux.from(body); + return Mono.empty(); + }); + } + + @Override + public Mono writeAndFlushWith(Publisher> body) { + return writeWith(Flux.from(body).flatMap(p -> p)); + } + + @Override + protected void applyHeaders() { + } + + @Override + protected void applyCookies() { + } + + @Override + public Mono setComplete() { + return doCommit(Mono::empty); + } + +} diff --git a/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java new file mode 100644 index 0000000000..c872d13c2e --- /dev/null +++ b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java @@ -0,0 +1,132 @@ +/* + * Copyright 2002-2016 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 + * + * http://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.mock.http.client.reactive.test; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DataBufferFactory; +import org.springframework.core.io.buffer.DataBufferUtils; +import org.springframework.core.io.buffer.DefaultDataBufferFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseCookie; +import org.springframework.http.client.reactive.ClientHttpResponse; +import org.springframework.util.Assert; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +/** + * Mock implementation of {@link ClientHttpResponse}. + * @author Brian Clozel + * @author Rossen Stoyanchev + * @since 5.0 + */ +public class MockClientHttpResponse implements ClientHttpResponse { + + private final HttpStatus status; + + private final HttpHeaders headers = new HttpHeaders(); + + private final MultiValueMap cookies = new LinkedMultiValueMap<>(); + + private Flux body = Flux.empty(); + + private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); + + + public MockClientHttpResponse(HttpStatus status) { + Assert.notNull(status, "HttpStatus is required"); + this.status = status; + } + + + public HttpStatus getStatusCode() { + return this.status; + } + + @Override + public HttpHeaders getHeaders() { + return this.headers; + } + + public MultiValueMap getCookies() { + return this.cookies; + } + + public void setBody(Publisher body) { + this.body = Flux.from(body); + } + + public void setBody(String body) { + setBody(body, StandardCharsets.UTF_8); + } + + public void setBody(String body, Charset charset) { + DataBuffer buffer = toDataBuffer(body, charset); + this.body = Flux.just(buffer); + } + + private DataBuffer toDataBuffer(String body, Charset charset) { + byte[] bytes = body.getBytes(charset); + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + return this.bufferFactory.wrap(byteBuffer); + } + + public Flux getBody() { + return this.body; + } + + /** + * Return the response body aggregated and converted to a String using the + * charset of the Content-Type response or otherwise as "UTF-8". + */ + public Mono getBodyAsString() { + Charset charset = getCharset(); + return Flux.from(getBody()) + .reduce(bufferFactory.allocateBuffer(), (previous, current) -> { + previous.write(current); + DataBufferUtils.release(current); + return previous; + }) + .map(buffer -> dumpString(buffer, charset)); + } + + private static String dumpString(DataBuffer buffer, Charset charset) { + Assert.notNull(charset, "'charset' must not be null"); + byte[] bytes = new byte[buffer.readableByteCount()]; + buffer.read(bytes); + return new String(bytes, charset); + } + + private Charset getCharset() { + Charset charset = null; + MediaType contentType = getHeaders().getContentType(); + if (contentType != null) { + charset = contentType.getCharset(); + } + return (charset != null ? charset : StandardCharsets.UTF_8); + } + +} diff --git a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java index 16a0849dd4..24db87396e 100644 --- a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpRequest.java @@ -19,6 +19,8 @@ import java.net.URI; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -28,140 +30,394 @@ import org.springframework.core.io.buffer.DefaultDataBufferFactory; 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.server.reactive.AbstractServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ServerHttpRequest}. + * + *

Note: this class extends the same + * {@link AbstractServerHttpRequest} base class as actual server-specific + * implementation and is therefore read-only once created. Use static builder + * methods in this class to build up request instances. + * * @author Rossen Stoyanchev * @since 5.0 */ -public class MockServerHttpRequest implements ServerHttpRequest { +public class MockServerHttpRequest extends AbstractServerHttpRequest { - private HttpMethod httpMethod; + private final HttpMethod httpMethod; - private URI url; + private final String contextPath; - private String contextPath = ""; + private final MultiValueMap cookies; - private final MultiValueMap queryParams = new LinkedMultiValueMap<>(); - - private final HttpHeaders headers = new HttpHeaders(); - - private final MultiValueMap cookies = new LinkedMultiValueMap<>(); - - private Flux body = Flux.empty(); + private final Flux body; - /** - * Create a new instance where the HTTP method and/or URL can be set later - * via {@link #setHttpMethod(HttpMethod)} and {@link #setUri(URI)}. - */ - public MockServerHttpRequest() { - } + private MockServerHttpRequest(HttpMethod httpMethod, URI uri, String contextPath, + HttpHeaders headers, MultiValueMap cookies, + Publisher body) { - /** - * Convenience alternative to {@link #MockServerHttpRequest(HttpMethod, URI)} - * that accepts a String URL. - */ - public MockServerHttpRequest(HttpMethod httpMethod, String url) { - this(httpMethod, (url != null ? URI.create(url) : null)); - } - - /** - * Create a new instance with the given HTTP method and URL. - */ - public MockServerHttpRequest(HttpMethod httpMethod, URI url) { + super(uri, headers); this.httpMethod = httpMethod; - this.url = url; + this.contextPath = (contextPath != null ? contextPath : ""); + this.cookies = cookies; + this.body = Flux.from(body); } - public void setHttpMethod(HttpMethod httpMethod) { - this.httpMethod = httpMethod; - } - @Override public HttpMethod getMethod() { return this.httpMethod; } - public MockServerHttpRequest setUri(String url) { - this.url = URI.create(url); - return this; - } - - public MockServerHttpRequest setUri(URI uri) { - this.url = uri; - return this; - } - - @Override - public URI getURI() { - return this.url; - } - - public void setContextPath(String contextPath) { - this.contextPath = contextPath; - } - @Override public String getContextPath() { return this.contextPath; } - public MockServerHttpRequest addHeader(String name, String value) { - getHeaders().add(name, value); - return this; - } - - public MockServerHttpRequest setHeader(String name, String value) { - getHeaders().set(name, value); - return this; - } - - @Override - public HttpHeaders getHeaders() { - return this.headers; - } - - @Override - public MultiValueMap getQueryParams() { - return this.queryParams; - } - - @Override - public MultiValueMap getCookies() { - return this.cookies; - } - - public MockServerHttpRequest setBody(Publisher body) { - this.body = Flux.from(body); - return this; - } - - public MockServerHttpRequest setBody(String body) { - DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8); - this.body = Flux.just(buffer); - return this; - } - - public MockServerHttpRequest setBody(String body, Charset charset) { - DataBuffer buffer = toDataBuffer(body, charset); - this.body = Flux.just(buffer); - return this; - } - - private DataBuffer toDataBuffer(String body, Charset charset) { - byte[] bytes = body.getBytes(charset); - ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - return new DefaultDataBufferFactory().wrap(byteBuffer); - } - @Override public Flux getBody() { return this.body; } -} + @Override + protected MultiValueMap initCookies() { + return this.cookies; + } + + // Static builder methods + + /** + * Create a builder with the given HTTP method and a {@link URI}. + * @param method the HTTP method (GET, POST, etc) + * @param url the URL + * @return the created builder + */ + public static BodyBuilder method(HttpMethod method, URI url) { + return new DefaultBodyBuilder(method, url); + } + + /** + * Alternative to {@link #method(HttpMethod, URI)} that accepts a URI template. + * @param method the HTTP method (GET, POST, etc) + * @param urlTemplate the URL template + * @param vars variables to expand into the template + * @return the created builder + */ + public static BodyBuilder method(HttpMethod method, String urlTemplate, Object... vars) { + URI url = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri(); + return new DefaultBodyBuilder(method, url); + } + + /** + * Create an HTTP GET builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder get(String urlTemplate, Object... uriVars) { + return method(HttpMethod.GET, urlTemplate, uriVars); + } + + /** + * Create an HTTP HEAD builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder head(String urlTemplate, Object... uriVars) { + return method(HttpMethod.HEAD, urlTemplate, uriVars); + } + + /** + * Create an HTTP POST builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder post(String urlTemplate, Object... uriVars) { + return method(HttpMethod.POST, urlTemplate, uriVars); + } + + /** + * Create an HTTP PUT builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder put(String urlTemplate, Object... uriVars) { + return method(HttpMethod.PUT, urlTemplate, uriVars); + } + + /** + * Create an HTTP PATCH builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BodyBuilder patch(String urlTemplate, Object... uriVars) { + return method(HttpMethod.PATCH, urlTemplate, uriVars); + } + + /** + * Create an HTTP DELETE builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder delete(String urlTemplate, Object... uriVars) { + return method(HttpMethod.DELETE, urlTemplate, uriVars); + } + + /** + * Creates an HTTP OPTIONS builder with the given url. + * @param urlTemplate a URL template; the resulting URL will be encoded + * @param uriVars zero or more URI variables + * @return the created builder + */ + public static BaseBuilder options(String urlTemplate, Object... uriVars) { + return method(HttpMethod.OPTIONS, urlTemplate, uriVars); + } + + + /** + * Defines a builder that adds headers to the request. + * @param the builder subclass + */ + public interface BaseBuilder> { + + /** + * Set the contextPath to return. + */ + B contextPath(String contextPath); + + /** + * Add one or more cookies. + */ + B cookie(String path, HttpCookie... cookie); + + /** + * Add the given, single header value under the given name. + * @param headerName the header name + * @param headerValues the header value(s) + * @see HttpHeaders#add(String, String) + */ + B header(String headerName, String... headerValues); + + /** + * Set the list of acceptable {@linkplain MediaType media types}, as + * specified by the {@code Accept} header. + * @param acceptableMediaTypes the acceptable media types + */ + B accept(MediaType... acceptableMediaTypes); + + /** + * Set the list of acceptable {@linkplain Charset charsets}, as specified + * by the {@code Accept-Charset} header. + * @param acceptableCharsets the acceptable charsets + */ + B acceptCharset(Charset... acceptableCharsets); + + /** + * Set the value of the {@code If-Modified-Since} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. + * @param ifModifiedSince the new value of the header + */ + B ifModifiedSince(long ifModifiedSince); + + /** + * Set the (new) value of the {@code If-Unmodified-Since} header. + *

The date should be specified as the number of milliseconds since + * January 1, 1970 GMT. + * @param ifUnmodifiedSince the new value of the header + * @see HttpHeaders#setIfUnmodifiedSince(long) + */ + B ifUnmodifiedSince(long ifUnmodifiedSince); + + /** + * Set the values of the {@code If-None-Match} header. + * @param ifNoneMatches the new value of the header + */ + B ifNoneMatch(String... ifNoneMatches); + + /** + * Set the (new) value of the Range header. + * @param ranges the HTTP ranges + * @see HttpHeaders#setRange(List) + */ + B range(HttpRange... ranges); + + /** + * Builds the request with no body. + * @return the request + * @see BodyBuilder#body(Publisher) + * @see BodyBuilder#body(String) + */ + MockServerHttpRequest build(); + } + + /** + * A builder that adds a body to the request. + */ + public interface BodyBuilder extends BaseBuilder { + + /** + * Set the length of the body in bytes, as specified by the + * {@code Content-Length} header. + * @param contentLength the content length + * @return this builder + * @see HttpHeaders#setContentLength(long) + */ + BodyBuilder contentLength(long contentLength); + + /** + * Set the {@linkplain MediaType media type} of the body, as specified + * by the {@code Content-Type} header. + * @param contentType the content type + * @return this builder + * @see HttpHeaders#setContentType(MediaType) + */ + BodyBuilder contentType(MediaType contentType); + + /** + * Set the body of the request and build it. + * @param body the body + * @return the built request entity + */ + MockServerHttpRequest body(Publisher body); + + /** + * Set the body of the request and build it. + *

The String is assumed to be UTF-8 encoded unless the request has a + * "content-type" header with a charset attribute. + * @param body the body as text + * @return the built request entity + */ + MockServerHttpRequest body(String body); + + } + + + private static class DefaultBodyBuilder implements BodyBuilder { + + private final HttpMethod method; + + private final URI url; + + private String contextPath; + + private final HttpHeaders headers = new HttpHeaders(); + + private final MultiValueMap cookies = new LinkedMultiValueMap<>(); + + + public DefaultBodyBuilder(HttpMethod method, URI url) { + this.method = method; + this.url = url; + } + + @Override + public BodyBuilder contextPath(String contextPath) { + this.contextPath = contextPath; + return this; + } + + @Override + public BodyBuilder cookie(String path, HttpCookie... cookies) { + this.cookies.put(path, Arrays.asList(cookies)); + return this; + } + + @Override + public BodyBuilder header(String headerName, String... headerValues) { + for (String headerValue : headerValues) { + this.headers.add(headerName, headerValue); + } + return this; + } + + @Override + public BodyBuilder accept(MediaType... acceptableMediaTypes) { + this.headers.setAccept(Arrays.asList(acceptableMediaTypes)); + return this; + } + + @Override + public BodyBuilder acceptCharset(Charset... acceptableCharsets) { + this.headers.setAcceptCharset(Arrays.asList(acceptableCharsets)); + return this; + } + + @Override + public BodyBuilder contentLength(long contentLength) { + this.headers.setContentLength(contentLength); + return this; + } + + @Override + public BodyBuilder contentType(MediaType contentType) { + this.headers.setContentType(contentType); + return this; + } + + @Override + public BodyBuilder ifModifiedSince(long ifModifiedSince) { + this.headers.setIfModifiedSince(ifModifiedSince); + return this; + } + + @Override + public BodyBuilder ifUnmodifiedSince(long ifUnmodifiedSince) { + this.headers.setIfUnmodifiedSince(ifUnmodifiedSince); + return this; + } + + @Override + public BodyBuilder ifNoneMatch(String... ifNoneMatches) { + this.headers.setIfNoneMatch(Arrays.asList(ifNoneMatches)); + return this; + } + + @Override + public BodyBuilder range(HttpRange... ranges) { + this.headers.setRange(Arrays.asList(ranges)); + return this; + } + + @Override + public MockServerHttpRequest body(Publisher body) { + return new MockServerHttpRequest(this.method, this.url, this.contextPath, + this.headers, this.cookies, body); + } + + @Override + public MockServerHttpRequest body(String body) { + Charset charset = getCharset(); + byte[] bytes = body.getBytes(charset); + ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); + DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer); + return body(Flux.just(buffer)); + } + + private Charset getCharset() { + MediaType contentType = this.headers.getContentType(); + Charset charset = (contentType != null ? contentType.getCharset() : null); + charset = charset != null ? charset : StandardCharsets.UTF_8; + return charset; + } + + @Override + public MockServerHttpRequest build() { + return body(Flux.empty()); + } + } + +} \ No newline at end of file diff --git a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpResponse.java b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpResponse.java index 1ebbbec957..120f155b2b 100644 --- a/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/http/server/reactive/test/MockServerHttpResponse.java @@ -18,127 +18,61 @@ package org.springframework.mock.http.server.reactive.test; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.function.Function; -import java.util.function.Supplier; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.springframework.core.io.buffer.DataBuffer; -import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; -import org.springframework.core.io.buffer.support.DataBufferTestUtils; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.http.ResponseCookie; +import org.springframework.http.server.reactive.AbstractServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; +import org.springframework.util.Assert; /** * Mock implementation of {@link ServerHttpResponse}. * @author Rossen Stoyanchev * @since 5.0 */ -public class MockServerHttpResponse implements ServerHttpResponse { - - private HttpStatus status; - - private final HttpHeaders headers = new HttpHeaders(); - - private final MultiValueMap cookies = new LinkedMultiValueMap<>(); - - private Function urlEncoder = url -> url; +public class MockServerHttpResponse extends AbstractServerHttpResponse { private Flux body; - private Flux> bodyWithFlushes; - private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); - - - @Override - public boolean setStatusCode(HttpStatus status) { - this.status = status; - return true; + public MockServerHttpResponse() { + super(new DefaultDataBufferFactory()); } - @Override - public HttpStatus getStatusCode() { - return this.status; - } - @Override - public HttpHeaders getHeaders() { - return this.headers; - } - - @Override - public MultiValueMap getCookies() { - return this.cookies; - } - - @Override - public String encodeUrl(String url) { - return (this.urlEncoder != null ? this.urlEncoder.apply(url) : url); - } - - @Override - public void registerUrlEncoder(Function encoder) { - this.urlEncoder = (this.urlEncoder != null ? this.urlEncoder.andThen(encoder) : encoder); - } - - public Publisher getBody() { + /** + * Return the output Publisher used to write to the response. + */ + public Flux getBody() { return this.body; } - public Publisher> getBodyWithFlush() { - return this.bodyWithFlushes; - } - - @Override - public Mono writeWith(Publisher body) { - this.body = Flux.from(body); - return this.body.then(); - } - - @Override - public Mono writeAndFlushWith(Publisher> body) { - this.bodyWithFlushes = Flux.from(body).map(Flux::from); - return this.bodyWithFlushes.then(); - } - - @Override - public void beforeCommit(Supplier> action) { - } - - @Override - public Mono setComplete() { - return Mono.empty(); - } - - @Override - public DataBufferFactory bufferFactory() { - return this.bufferFactory; - } - /** - * Return the body of the response aggregated and converted to a String - * using the charset of the Content-Type response or otherwise defaulting - * to "UTF-8". + * Return the response body aggregated and converted to a String using the + * charset of the Content-Type response or otherwise as "UTF-8". */ public Mono getBodyAsString() { Charset charset = getCharset(); - return Flux.from(getBody()) + return getBody() .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { previous.write(current); DataBufferUtils.release(current); return previous; }) - .map(buffer -> DataBufferTestUtils.dumpString(buffer, charset)); + .map(buffer -> bufferToString(buffer, charset)); + } + + private static String bufferToString(DataBuffer buffer, Charset charset) { + Assert.notNull(charset, "'charset' must not be null"); + byte[] bytes = new byte[buffer.readableByteCount()]; + buffer.read(bytes); + return new String(bytes, charset); } private Charset getCharset() { @@ -150,4 +84,27 @@ public class MockServerHttpResponse implements ServerHttpResponse { return (charset != null ? charset : StandardCharsets.UTF_8); } + @Override + protected Mono writeWithInternal(Publisher body) { + this.body = Flux.from(body); + return Mono.empty(); + } + + @Override + protected Mono writeAndFlushWithInternal(Publisher> body) { + return writeWithInternal(Flux.from(body).flatMap(Flux::from)); + } + + @Override + protected void applyStatusCode() { + } + + @Override + protected void applyHeaders() { + } + + @Override + protected void applyCookies() { + } + } diff --git a/spring-web/src/test/java/org/springframework/web/bind/support/WebExchangeDataBinderTests.java b/spring-web/src/test/java/org/springframework/web/bind/support/WebExchangeDataBinderTests.java index 014df31160..7aeb8330f7 100644 --- a/spring-web/src/test/java/org/springframework/web/bind/support/WebExchangeDataBinderTests.java +++ b/spring-web/src/test/java/org/springframework/web/bind/support/WebExchangeDataBinderTests.java @@ -33,7 +33,6 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.DefaultWebSessionManager; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; @@ -51,17 +50,12 @@ public class WebExchangeDataBinderTests { private TestBean testBean; - private MockServerHttpRequest request; - @Before public void setUp() throws Exception { this.testBean = new TestBean(); this.binder = new WebExchangeDataBinder(this.testBean, "person"); this.binder.registerCustomEditor(ITestBean.class, new TestBeanPropertyEditor()); - - this.request = new MockServerHttpRequest(); - this.request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); } @@ -70,8 +64,7 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("spouse", "someValue"); formData.add("spouse.name", "test"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertNotNull(this.testBean.getSpouse()); assertEquals("test", testBean.getSpouse().getName()); @@ -82,13 +75,11 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("_postProcessed", "visible"); formData.add("postProcessed", "on"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertTrue(this.testBean.isPostProcessed()); formData.remove("postProcessed"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertFalse(this.testBean.isPostProcessed()); } @@ -99,13 +90,11 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("_postProcessed", "visible"); formData.add("postProcessed", "on"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertTrue(this.testBean.isPostProcessed()); formData.remove("postProcessed"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertFalse(this.testBean.isPostProcessed()); } @@ -114,13 +103,11 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("!postProcessed", "off"); formData.add("postProcessed", "on"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertTrue(this.testBean.isPostProcessed()); formData.remove("postProcessed"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertFalse(this.testBean.isPostProcessed()); } @@ -130,18 +117,15 @@ public class WebExchangeDataBinderTests { formData.add("!postProcessed", "on"); formData.add("_postProcessed", "visible"); formData.add("postProcessed", "on"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertTrue(this.testBean.isPostProcessed()); formData.remove("postProcessed"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertTrue(this.testBean.isPostProcessed()); formData.remove("!postProcessed"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertFalse(this.testBean.isPostProcessed()); } @@ -150,13 +134,11 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("!name", "anonymous"); formData.add("name", "Scott"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertEquals("Scott", this.testBean.getName()); formData.remove("name"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertEquals("anonymous", this.testBean.getName()); } @@ -166,14 +148,12 @@ public class WebExchangeDataBinderTests { formData.add("stringArray", "bar"); formData.add("stringArray", "abc"); formData.add("stringArray", "123,def"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertEquals("Expected all three items to be bound", 3, this.testBean.getStringArray().length); formData.remove("stringArray"); formData.add("stringArray", "123,def"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertEquals("Expected only 1 item to be bound", 1, this.testBean.getStringArray().length); } @@ -182,8 +162,7 @@ public class WebExchangeDataBinderTests { MultiValueMap formData = new LinkedMultiValueMap<>(); formData.add("spouse.name", "test"); formData.add("spouse", "someValue"); - this.request.setBody(generateForm(formData)); - this.binder.bind(createExchange()).blockMillis(5000); + this.binder.bind(exchange(formData)).blockMillis(5000); assertNotNull(this.testBean.getSpouse()); assertEquals("test", this.testBean.getSpouse().getName()); @@ -191,10 +170,10 @@ public class WebExchangeDataBinderTests { @Test public void testBindingWithQueryParams() throws Exception { - MultiValueMap queryParams = createExchange().getRequest().getQueryParams(); - queryParams.add("spouse", "someValue"); - queryParams.add("spouse.name", "test"); - this.binder.bind(createExchange()).blockMillis(5000); + String url = "/path?spouse=someValue&spouse.name=test"; + MockServerHttpRequest request = MockServerHttpRequest.post(url).build(); + ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse()); + this.binder.bind(exchange).blockMillis(5000); assertNotNull(this.testBean.getSpouse()); assertEquals("test", this.testBean.getSpouse().getName()); @@ -227,9 +206,14 @@ public class WebExchangeDataBinderTests { return builder.toString(); } - private ServerWebExchange createExchange() { - return new DefaultServerWebExchange( - this.request, new MockServerHttpResponse(), new DefaultWebSessionManager()); + private ServerWebExchange exchange(MultiValueMap formData) { + + MockServerHttpRequest request = MockServerHttpRequest + .post("/") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(generateForm(formData)); + + return new DefaultServerWebExchange(request, new MockServerHttpResponse()); } diff --git a/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java b/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java index a983350ed0..12df47c147 100644 --- a/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java +++ b/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java @@ -16,57 +16,53 @@ package org.springframework.web.cors.reactive; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Test; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; -import org.springframework.web.cors.reactive.CorsUtils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get; +import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.options; /** * Test case for reactive {@link CorsUtils}. - * * @author Sebastien Deleuze + * @author Rossen Stoyanchev */ public class CorsUtilsTests { @Test public void isCorsRequest() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.addHeader(HttpHeaders.ORIGIN, "http://domain.com"); + MockServerHttpRequest request = get("/").header(HttpHeaders.ORIGIN, "http://domain.com").build(); assertTrue(CorsUtils.isCorsRequest(request)); } @Test public void isNotCorsRequest() { - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = get("/").build(); assertFalse(CorsUtils.isCorsRequest(request)); } @Test public void isPreFlightRequest() { - MockServerHttpRequest request = new MockServerHttpRequest(); - request.setHttpMethod(HttpMethod.OPTIONS); - request.addHeader(HttpHeaders.ORIGIN, "http://domain.com"); - request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"); + MockServerHttpRequest request = options("/") + .header(HttpHeaders.ORIGIN, "http://domain.com") + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET") + .build(); assertTrue(CorsUtils.isPreFlightRequest(request)); } @Test public void isNotPreFlightRequest() { - MockServerHttpRequest request = new MockServerHttpRequest(); + MockServerHttpRequest request = get("/").build(); assertFalse(CorsUtils.isPreFlightRequest(request)); - request = new MockServerHttpRequest(); - request.setHttpMethod(HttpMethod.OPTIONS); - request.addHeader(HttpHeaders.ORIGIN, "http://domain.com"); + request = options("/").header(HttpHeaders.ORIGIN, "http://domain.com").build(); assertFalse(CorsUtils.isPreFlightRequest(request)); - request = new MockServerHttpRequest(); - request.setHttpMethod(HttpMethod.OPTIONS); - request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"); + request = options("/").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build(); assertFalse(CorsUtils.isPreFlightRequest(request)); } diff --git a/spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java b/spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java index 46cbaad09c..f7d5e6e6d7 100644 --- a/spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java +++ b/spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java @@ -16,6 +16,7 @@ package org.springframework.web.cors.reactive; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; @@ -25,9 +26,7 @@ import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -45,11 +44,9 @@ import static org.springframework.http.HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD public class DefaultCorsProcessorTests { private MockServerHttpRequest request; - + private MockServerHttpResponse response; - private ServerWebExchange exchange; - private DefaultCorsProcessor processor; private CorsConfiguration conf; @@ -57,43 +54,35 @@ public class DefaultCorsProcessorTests { @Before public void setup() { - this.request = new MockServerHttpRequest(); - this.request.setUri("http://localhost/test.html"); this.conf = new CorsConfiguration(); this.response = new MockServerHttpResponse(); this.response.setStatusCode(HttpStatus.OK); this.processor = new DefaultCorsProcessor(); - this.exchange = new DefaultServerWebExchange(this.request, this.response, new MockWebSessionManager()); } @Test public void actualRequestWithOriginHeader() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - - this.processor.processRequest(this.conf, this.exchange); + this.request = actualRequest().build(); + this.processor.processRequest(this.conf, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } @Test public void actualRequestWithOriginHeaderAndNullConfig() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - - this.processor.processRequest(null, this.exchange); + this.request = actualRequest().build(); + this.processor.processRequest(null, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); } @Test public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = actualRequest().build(); this.conf.addAllowedOrigin("*"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("*", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertFalse(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE)); @@ -103,14 +92,13 @@ public class DefaultCorsProcessorTests { @Test public void actualRequestCredentials() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = actualRequest().build(); this.conf.addAllowedOrigin("http://domain1.com"); this.conf.addAllowedOrigin("http://domain2.com"); this.conf.addAllowedOrigin("http://domain3.com"); this.conf.setAllowCredentials(true); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)); @@ -120,12 +108,11 @@ public class DefaultCorsProcessorTests { @Test public void actualRequestCredentialsWithOriginWildcard() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = actualRequest().build(); this.conf.addAllowedOrigin("*"); this.conf.setAllowCredentials(true); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)); @@ -135,24 +122,22 @@ public class DefaultCorsProcessorTests { @Test public void actualRequestCaseInsensitiveOriginMatch() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = actualRequest().build(); this.conf.addAllowedOrigin("http://DOMAIN2.com"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); } @Test public void actualRequestExposedHeaders() throws Exception { - this.request.setHttpMethod(HttpMethod.GET); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = actualRequest().build(); this.conf.addExposedHeader("header1"); this.conf.addExposedHeader("header2"); this.conf.addAllowedOrigin("http://domain2.com"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)); @@ -163,84 +148,76 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestAllOriginsAllowed() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); + this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").build(); this.conf.addAllowedOrigin("*"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertEquals(HttpStatus.OK, this.response.getStatusCode()); } + @Test public void preflightRequestWrongAllowedMethod() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "DELETE"); + this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE").build(); this.conf.addAllowedOrigin("*"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } @Test public void preflightRequestMatchedAllowedMethod() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); + this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").build(); this.conf.addAllowedOrigin("*"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertEquals(HttpStatus.OK, this.response.getStatusCode()); assertEquals("GET,HEAD", this.response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)); } @Test public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); + this.request = preFlightRequest().build(); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } @Test public void preflightRequestWithoutRequestMethod() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"); + this.request = preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1").build(); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } @Test public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1") + .build(); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } @Test public void preflightRequestValidRequestAndConfig() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1") + .build(); this.conf.addAllowedOrigin("*"); this.conf.addAllowedMethod("GET"); this.conf.addAllowedMethod("PUT"); this.conf.addAllowedHeader("header1"); this.conf.addAllowedHeader("header2"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("*", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)); @@ -251,17 +228,17 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestCredentials() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1") + .build(); this.conf.addAllowedOrigin("http://domain1.com"); this.conf.addAllowedOrigin("http://domain2.com"); this.conf.addAllowedOrigin("http://domain3.com"); this.conf.addAllowedHeader("Header1"); this.conf.setAllowCredentials(true); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)); @@ -271,17 +248,17 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestCredentialsWithOriginWildcard() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1") + .build(); this.conf.addAllowedOrigin("http://domain1.com"); this.conf.addAllowedOrigin("*"); this.conf.addAllowedOrigin("http://domain3.com"); this.conf.addAllowedHeader("Header1"); this.conf.setAllowCredentials(true); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("http://domain2.com", this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); @@ -289,16 +266,16 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestAllowedHeaders() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2") + .build(); this.conf.addAllowedHeader("Header1"); this.conf.addAllowedHeader("Header2"); this.conf.addAllowedHeader("Header3"); this.conf.addAllowedOrigin("http://domain2.com"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS)); assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1")); @@ -309,14 +286,14 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestAllowsAllHeaders() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1, Header2") + .build(); this.conf.addAllowedHeader("*"); this.conf.addAllowedOrigin("http://domain2.com"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS)); assertTrue(this.response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1")); @@ -327,14 +304,14 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestWithEmptyHeaders() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, ""); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .header(ACCESS_CONTROL_REQUEST_HEADERS, "") + .build(); this.conf.addAllowedHeader("*"); this.conf.addAllowedOrigin("http://domain2.com"); - this.processor.processRequest(this.conf, this.exchange); + this.processor.processRequest(this.conf, createExchange()); assertTrue(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS)); assertEquals(HttpStatus.OK, this.response.getStatusCode()); @@ -342,14 +319,33 @@ public class DefaultCorsProcessorTests { @Test public void preflightRequestWithNullConfig() throws Exception { - this.request.setHttpMethod(HttpMethod.OPTIONS); - this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.request.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET"); + this.request = preFlightRequest() + .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") + .build(); this.conf.addAllowedOrigin("*"); - this.processor.processRequest(null, this.exchange); + this.processor.processRequest(null, createExchange()); assertFalse(this.response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, this.response.getStatusCode()); } + private MockServerHttpRequest.BaseBuilder actualRequest() { + return corsRequest(HttpMethod.GET); + } + + private MockServerHttpRequest.BaseBuilder preFlightRequest() { + return corsRequest(HttpMethod.OPTIONS); + } + + private MockServerHttpRequest.BaseBuilder corsRequest(HttpMethod httpMethod) { + return MockServerHttpRequest + .method(httpMethod, "http://localhost/test.html") + .header(HttpHeaders.ORIGIN, "http://domain2.com"); + } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, this.response); + } + } diff --git a/spring-web/src/test/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSourceTests.java b/spring-web/src/test/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSourceTests.java index 9ad06c2b8c..f29d3da41f 100644 --- a/spring-web/src/test/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSourceTests.java +++ b/spring-web/src/test/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSourceTests.java @@ -25,7 +25,6 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -64,12 +63,10 @@ public class UrlBasedCorsConfigurationSourceTests { this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration()); } - private ServerWebExchange createExchange(HttpMethod httpMethod, String url) { - ServerHttpRequest request = new MockServerHttpRequest(httpMethod, url); + ServerHttpRequest request = MockServerHttpRequest.method(httpMethod, url).build(); MockServerHttpResponse response = new MockServerHttpResponse(); - MockWebSessionManager sessionManager = new MockWebSessionManager(); - return new DefaultServerWebExchange(request, response, sessionManager); + return new DefaultServerWebExchange(request, response); } } diff --git a/spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeCheckNotModifiedTests.java b/spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeCheckNotModifiedTests.java index 22b59a3963..4f14ca8f49 100644 --- a/spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeCheckNotModifiedTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeCheckNotModifiedTests.java @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.Locale; import java.util.TimeZone; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,7 +36,6 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -57,9 +57,7 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { private MockServerHttpRequest request; - private MockServerHttpResponse response; - - private DefaultServerWebExchange exchange; + private MockServerHttpResponse response = new MockServerHttpResponse(); private Instant currentDate; @@ -77,20 +75,18 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Before public void setUp() throws URISyntaxException { - currentDate = Instant.now().truncatedTo(ChronoUnit.SECONDS); - dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); - dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); - request = new MockServerHttpRequest(method, "http://example.org"); - response = new MockServerHttpResponse(); - exchange = new DefaultServerWebExchange(request, response, new MockWebSessionManager()); + this.currentDate = Instant.now().truncatedTo(ChronoUnit.SECONDS); + this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); + this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); } + @Test public void checkNotModifiedNon2xxStatus() { - request.getHeaders().setIfModifiedSince(this.currentDate.toEpochMilli()); - response.setStatusCode(HttpStatus.NOT_MODIFIED); + this.request = request().ifModifiedSince(this.currentDate.toEpochMilli()).build(); + this.response.setStatusCode(HttpStatus.NOT_MODIFIED); - assertFalse(exchange.checkNotModified(this.currentDate)); + assertFalse(createExchange().checkNotModified(this.currentDate)); assertEquals(304, response.getStatusCode().value()); assertEquals(-1, response.getHeaders().getLastModified()); } @@ -98,18 +94,18 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test // SPR-14559 public void checkNotModifiedInvalidIfNoneMatchHeader() { String eTag = "\"etagvalue\""; - request.getHeaders().setIfNoneMatch("missingquotes"); - assertFalse(exchange.checkNotModified(eTag)); + this.request = request().ifNoneMatch("missingquotes").build(); + assertFalse(createExchange().checkNotModified(eTag)); assertNull(response.getStatusCode()); assertEquals(eTag, response.getHeaders().getETag()); } @Test public void checkNotModifiedHeaderAlreadySet() { - request.getHeaders().setIfModifiedSince(currentDate.toEpochMilli()); - response.getHeaders().add("Last-Modified", CURRENT_TIME); + this.request = request().ifModifiedSince(currentDate.toEpochMilli()).build(); + this.response.getHeaders().add("Last-Modified", CURRENT_TIME); - assertTrue(exchange.checkNotModified(currentDate)); + assertTrue(createExchange().checkNotModified(currentDate)); assertEquals(304, response.getStatusCode().value()); assertEquals(1, response.getHeaders().get("Last-Modified").size()); assertEquals(CURRENT_TIME, response.getHeaders().getFirst("Last-Modified")); @@ -117,9 +113,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedTimestamp() throws Exception { - request.getHeaders().setIfModifiedSince(currentDate.toEpochMilli()); + this.request = request().ifModifiedSince(currentDate.toEpochMilli()).build(); - assertTrue(exchange.checkNotModified(currentDate)); + assertTrue(createExchange().checkNotModified(currentDate)); assertEquals(304, response.getStatusCode().value()); assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified()); @@ -128,9 +124,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkModifiedTimestamp() { Instant oneMinuteAgo = currentDate.minusSeconds(60); - request.getHeaders().setIfModifiedSince(oneMinuteAgo.toEpochMilli()); + this.request = request().ifModifiedSince(oneMinuteAgo.toEpochMilli()).build(); - assertFalse(exchange.checkNotModified(currentDate)); + assertFalse(createExchange().checkNotModified(currentDate)); assertNull(response.getStatusCode()); assertEquals(currentDate.toEpochMilli(), response.getHeaders().getLastModified()); @@ -139,9 +135,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedETag() { String eTag = "\"Foo\""; - request.getHeaders().setIfNoneMatch(eTag); + this.request = request().ifNoneMatch(eTag).build(); - assertTrue(exchange.checkNotModified(eTag)); + assertTrue(createExchange().checkNotModified(eTag)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -150,9 +146,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedETagWithSeparatorChars() { String eTag = "\"Foo, Bar\""; - request.getHeaders().setIfNoneMatch(eTag); + this.request = request().ifNoneMatch(eTag).build(); - assertTrue(exchange.checkNotModified(eTag)); + assertTrue(createExchange().checkNotModified(eTag)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -163,9 +159,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkModifiedETag() { String currentETag = "\"Foo\""; String oldEtag = "Bar"; - request.getHeaders().setIfNoneMatch(oldEtag); + this.request = request().ifNoneMatch(oldEtag).build(); - assertFalse(exchange.checkNotModified(currentETag)); + assertFalse(createExchange().checkNotModified(currentETag)); assertNull(response.getStatusCode()); assertEquals(currentETag, response.getHeaders().getETag()); @@ -175,9 +171,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkNotModifiedUnpaddedETag() { String eTag = "Foo"; String paddedEtag = String.format("\"%s\"", eTag); - request.getHeaders().setIfNoneMatch(paddedEtag); + this.request = request().ifNoneMatch(paddedEtag).build(); - assertTrue(exchange.checkNotModified(eTag)); + assertTrue(createExchange().checkNotModified(eTag)); assertEquals(304, response.getStatusCode().value()); assertEquals(paddedEtag, response.getHeaders().getETag()); @@ -187,9 +183,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkModifiedUnpaddedETag() { String currentETag = "Foo"; String oldEtag = "Bar"; - request.getHeaders().setIfNoneMatch(oldEtag); + this.request = request().ifNoneMatch(oldEtag).build(); - assertFalse(exchange.checkNotModified(currentETag)); + assertFalse(createExchange().checkNotModified(currentETag)); assertNull(response.getStatusCode()); assertEquals(String.format("\"%s\"", currentETag), response.getHeaders().getETag()); @@ -198,9 +194,8 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedWildcardIsIgnored() { String eTag = "\"Foo\""; - request.getHeaders().setIfNoneMatch("*"); - - assertFalse(exchange.checkNotModified(eTag)); + this.request = request().ifNoneMatch("*").build(); + assertFalse(createExchange().checkNotModified(eTag)); assertNull(response.getStatusCode()); assertEquals(eTag, response.getHeaders().getETag()); @@ -209,10 +204,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedETagAndTimestamp() { String eTag = "\"Foo\""; - request.getHeaders().setIfNoneMatch(eTag); - request.getHeaders().setIfModifiedSince(currentDate.toEpochMilli()); + this.request = request().ifNoneMatch(eTag).ifModifiedSince(currentDate.toEpochMilli()).build(); - assertTrue(exchange.checkNotModified(eTag, currentDate)); + assertTrue(createExchange().checkNotModified(eTag, currentDate)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -223,11 +217,10 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedETagAndModifiedTimestamp() { String eTag = "\"Foo\""; - request.getHeaders().setIfNoneMatch(eTag); Instant oneMinuteAgo = currentDate.minusSeconds(60); - request.getHeaders().setIfModifiedSince(oneMinuteAgo.toEpochMilli()); + this.request = request().ifNoneMatch(eTag).ifModifiedSince(oneMinuteAgo.toEpochMilli()).build(); - assertTrue(exchange.checkNotModified(eTag, currentDate)); + assertTrue(createExchange().checkNotModified(eTag, currentDate)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -238,10 +231,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkModifiedETagAndNotModifiedTimestamp() throws Exception { String currentETag = "\"Foo\""; String oldEtag = "\"Bar\""; - request.getHeaders().setIfNoneMatch(oldEtag); - request.getHeaders().setIfModifiedSince(currentDate.toEpochMilli()); + this.request = request().ifNoneMatch(oldEtag).ifModifiedSince(currentDate.toEpochMilli()).build(); - assertFalse(exchange.checkNotModified(currentETag, currentDate)); + assertFalse(createExchange().checkNotModified(currentETag, currentDate)); assertNull(response.getStatusCode()); assertEquals(currentETag, response.getHeaders().getETag()); @@ -252,9 +244,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkNotModifiedETagWeakStrong() { String eTag = "\"Foo\""; String weakEtag = String.format("W/%s", eTag); - request.getHeaders().setIfNoneMatch(eTag); + this.request = request().ifNoneMatch(eTag).build(); - assertTrue(exchange.checkNotModified(weakEtag)); + assertTrue(createExchange().checkNotModified(weakEtag)); assertEquals(304, response.getStatusCode().value()); assertEquals(weakEtag, response.getHeaders().getETag()); @@ -263,9 +255,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedETagStrongWeak() { String eTag = "\"Foo\""; - request.getHeaders().setIfNoneMatch(String.format("W/%s", eTag)); + this.request = request().ifNoneMatch(String.format("W/%s", eTag)).build(); - assertTrue(exchange.checkNotModified(eTag)); + assertTrue(createExchange().checkNotModified(eTag)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -275,9 +267,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { public void checkNotModifiedMultipleETags() { String eTag = "\"Bar\""; String multipleETags = String.format("\"Foo\", %s", eTag); - request.getHeaders().setIfNoneMatch(multipleETags); + this.request = request().ifNoneMatch(multipleETags).build(); - assertTrue(exchange.checkNotModified(eTag)); + assertTrue(createExchange().checkNotModified(eTag)); assertEquals(304, response.getStatusCode().value()); assertEquals(eTag, response.getHeaders().getETag()); @@ -286,10 +278,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedTimestampWithLengthPart() throws Exception { long epochTime = dateFormat.parse(CURRENT_TIME).getTime(); - request.setHttpMethod(HttpMethod.GET); - request.setHeader("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774"); + this.request = request().header("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774").build(); - assertTrue(exchange.checkNotModified(Instant.ofEpochMilli(epochTime))); + assertTrue(createExchange().checkNotModified(Instant.ofEpochMilli(epochTime))); assertEquals(304, response.getStatusCode().value()); assertEquals(epochTime, response.getHeaders().getLastModified()); @@ -298,10 +289,9 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkModifiedTimestampWithLengthPart() throws Exception { long epochTime = dateFormat.parse(CURRENT_TIME).getTime(); - request.setHttpMethod(HttpMethod.GET); - request.setHeader("If-Modified-Since", "Tue, 08 Apr 2014 09:57:42 GMT; length=13774"); + this.request = request().header("If-Modified-Since", "Tue, 08 Apr 2014 09:57:42 GMT; length=13774").build(); - assertFalse(exchange.checkNotModified(Instant.ofEpochMilli(epochTime))); + assertFalse(createExchange().checkNotModified(Instant.ofEpochMilli(epochTime))); assertNull(response.getStatusCode()); assertEquals(epochTime, response.getHeaders().getLastModified()); @@ -310,10 +300,10 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedTimestampConditionalPut() throws Exception { Instant oneMinuteAgo = currentDate.minusSeconds(60); - request.setHttpMethod(HttpMethod.PUT); - request.getHeaders().setIfUnmodifiedSince(currentDate.toEpochMilli()); + long millis = currentDate.toEpochMilli(); + this.request = MockServerHttpRequest.put("http://example.org").ifUnmodifiedSince(millis).build(); - assertFalse(exchange.checkNotModified(oneMinuteAgo)); + assertFalse(createExchange().checkNotModified(oneMinuteAgo)); assertNull(response.getStatusCode()); assertEquals(-1, response.getHeaders().getLastModified()); } @@ -321,12 +311,22 @@ public class DefaultServerWebExchangeCheckNotModifiedTests { @Test public void checkNotModifiedTimestampConditionalPutConflict() throws Exception { Instant oneMinuteAgo = currentDate.minusSeconds(60); - request.setHttpMethod(HttpMethod.PUT); - request.getHeaders().setIfUnmodifiedSince(oneMinuteAgo.toEpochMilli()); + long millis = oneMinuteAgo.toEpochMilli(); + this.request = MockServerHttpRequest.put("http://example.org").ifUnmodifiedSince(millis).build(); - assertTrue(exchange.checkNotModified(currentDate)); + assertTrue(createExchange().checkNotModified(currentDate)); assertEquals(412, response.getStatusCode().value()); assertEquals(-1, response.getHeaders().getLastModified()); } + @NotNull + private MockServerHttpRequest.BaseBuilder request() { + return MockServerHttpRequest.get("http://example.org"); + } + + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, this.response); + } + } diff --git a/spring-web/src/test/java/org/springframework/web/server/handler/ExceptionHandlingHttpHandlerTests.java b/spring-web/src/test/java/org/springframework/web/server/handler/ExceptionHandlingHttpHandlerTests.java index 2e478bc243..04c8e34ecd 100644 --- a/spring-web/src/test/java/org/springframework/web/server/handler/ExceptionHandlingHttpHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/handler/ExceptionHandlingHttpHandlerTests.java @@ -20,7 +20,6 @@ import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; @@ -28,8 +27,6 @@ import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebExceptionHandler; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; -import org.springframework.web.server.session.WebSessionManager; import static org.junit.Assert.assertEquals; @@ -48,10 +45,9 @@ public class ExceptionHandlingHttpHandlerTests { @Before public void setUp() throws Exception { - WebSessionManager sessionManager = new MockWebSessionManager(); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "http://localhost:8080"); + MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080").build(); this.response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange(request, this.response, sessionManager); + this.exchange = new DefaultServerWebExchange(request, this.response); this.targetHandler = new StubWebHandler(new IllegalStateException("boo")); } diff --git a/spring-web/src/test/java/org/springframework/web/server/handler/FilteringWebHandlerTests.java b/spring-web/src/test/java/org/springframework/web/server/handler/FilteringWebHandlerTests.java index 91ce3894fd..1a99795507 100644 --- a/spring-web/src/test/java/org/springframework/web/server/handler/FilteringWebHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/handler/FilteringWebHandlerTests.java @@ -22,7 +22,6 @@ import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; @@ -34,7 +33,10 @@ import org.springframework.web.server.WebFilterChain; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * @author Rossen Stoyanchev @@ -50,7 +52,7 @@ public class FilteringWebHandlerTests { @Before public void setUp() throws Exception { - this.request = new MockServerHttpRequest(HttpMethod.GET, "http://localhost"); + this.request = MockServerHttpRequest.get("http://localhost").build(); this.response = new MockServerHttpResponse(); } diff --git a/spring-web/src/test/java/org/springframework/web/server/handler/ResponseStatusExceptionHandlerTests.java b/spring-web/src/test/java/org/springframework/web/server/handler/ResponseStatusExceptionHandlerTests.java index 9235d8179b..0e44abf893 100644 --- a/spring-web/src/test/java/org/springframework/web/server/handler/ResponseStatusExceptionHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/handler/ResponseStatusExceptionHandlerTests.java @@ -18,19 +18,17 @@ package org.springframework.web.server.handler; import java.time.Duration; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Test; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ResponseStatusException; -import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; -import org.springframework.web.server.session.MockWebSessionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; @@ -44,37 +42,36 @@ public class ResponseStatusExceptionHandlerTests { private ResponseStatusExceptionHandler handler; - private MockServerHttpResponse response; + private MockServerHttpRequest request; - private ServerWebExchange exchange; + private MockServerHttpResponse response; @Before public void setUp() throws Exception { this.handler = new ResponseStatusExceptionHandler(); + this.request = MockServerHttpRequest.get("/").build(); this.response = new MockServerHttpResponse(); - this.exchange = new DefaultServerWebExchange( - new MockServerHttpRequest(HttpMethod.GET, "/path"), this.response, - new MockWebSessionManager()); } @Test public void handleException() throws Exception { Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, ""); - this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5)); - + this.handler.handle(createExchange(), ex).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode()); } @Test public void unresolvedException() throws Exception { Throwable expected = new IllegalStateException(); - Mono mono = this.handler.handle(this.exchange, expected); + Mono mono = this.handler.handle(createExchange(), expected); + StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(expected, actual)).verify(); + } - StepVerifier.create(mono) - .consumeErrorWith(actual -> assertSame(expected, actual)) - .verify(); + @NotNull + private DefaultServerWebExchange createExchange() { + return new DefaultServerWebExchange(this.request, this.response); } } diff --git a/spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java b/spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java index 97f8db0a7a..548a0bae8b 100644 --- a/spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java @@ -26,7 +26,6 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; @@ -45,18 +44,20 @@ import static org.junit.Assert.assertSame; */ public class DefaultWebSessionManagerTests { - private final DefaultWebSessionManager manager = new DefaultWebSessionManager(); + private DefaultWebSessionManager manager; - private final TestWebSessionIdResolver idResolver = new TestWebSessionIdResolver(); + private TestWebSessionIdResolver idResolver; - private DefaultServerWebExchange exchange; + private ServerWebExchange exchange; @Before public void setUp() throws Exception { + this.manager = new DefaultWebSessionManager(); + this.idResolver = new TestWebSessionIdResolver(); this.manager.setSessionIdResolver(this.idResolver); - MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "/path"); + MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); this.exchange = new DefaultServerWebExchange(request, response, this.manager); }