From d74e09a92505060a8826d5ad713db4dec4384a74 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 29 Jun 2018 17:37:45 -0400 Subject: [PATCH] Polish MockClientHttpRequest --- .../reactive/MockClientHttpRequest.java | 89 +++++++++---------- .../reactive/test/MockClientHttpRequest.java | 89 +++++++++---------- 2 files changed, 88 insertions(+), 90 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java index 34af6572f4..72beb08d05 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java @@ -58,7 +58,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { new IllegalStateException("The body is not set. " + "Did handling complete with success? Is a custom \"writeHandler\" configured?")); - private Function, Mono> writeHandler = initDefaultWriteHandler(); + private Function, Mono> writeHandler; public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) { @@ -68,16 +68,29 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { public MockClientHttpRequest(HttpMethod httpMethod, URI url) { this.httpMethod = httpMethod; this.url = url; - } - - private Function, Mono> initDefaultWriteHandler() { - return body -> { + this.writeHandler = body -> { this.body = body.cache(); return this.body.then(); }; } + /** + * Configure a custom handler for writing the request body. + * + *

The default write handler consumes and caches the request body so it + * may be accessed subsequently, e.g. in test assertions. Use this property + * when the request body is an infinite stream. + * + * @param writeHandler the write handler to use returning {@code Mono} + * when the body has been "written" (i.e. consumed). + */ + public void setWriteHandler(Function, Mono> writeHandler) { + Assert.notNull(writeHandler, "'writeHandler' is required"); + this.writeHandler = writeHandler; + } + + @Override public HttpMethod getMethod() { return this.httpMethod; @@ -93,6 +106,32 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { return this.bufferFactory; } + @Override + protected void applyHeaders() { + } + + @Override + protected void applyCookies() { + getCookies().values().stream().flatMap(Collection::stream) + .forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString())); + } + + @Override + public Mono writeWith(Publisher body) { + return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body)))); + } + + @Override + public Mono writeAndFlushWith(Publisher> body) { + return writeWith(Flux.from(body).flatMap(p -> p)); + } + + @Override + public Mono setComplete() { + return writeWith(Flux.empty()); + } + + /** * Return the request body, or an error stream if the body was never set * or when {@link #setWriteHandler} is configured. @@ -126,44 +165,4 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { return new String(bytes, charset); } - /** - * Configure a custom handler for writing the request body. - * - *

The default write handler consumes and caches the request body so it - * may be accessed subsequently, e.g. in test assertions. Use this property - * when the request body is an infinite stream. - * - * @param writeHandler the write handler to use returning {@code Mono} - * when the body has been "written" (i.e. consumed). - */ - public void setWriteHandler(Function, Mono> writeHandler) { - Assert.notNull(writeHandler, "'writeHandler' is required"); - this.writeHandler = writeHandler; - } - - @Override - protected void applyHeaders() { - } - - @Override - protected void applyCookies() { - getCookies().values().stream().flatMap(Collection::stream) - .forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString())); - } - - @Override - public Mono writeWith(Publisher body) { - return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body)))); - } - - @Override - public Mono writeAndFlushWith(Publisher> body) { - return writeWith(Flux.from(body).flatMap(p -> p)); - } - - @Override - public Mono setComplete() { - return writeWith(Flux.empty()); - } - } 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 index d2f98b3c48..ad387d201e 100644 --- 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 @@ -58,7 +58,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { new IllegalStateException("The body is not set. " + "Did handling complete with success? Is a custom \"writeHandler\" configured?")); - private Function, Mono> writeHandler = initDefaultWriteHandler(); + private Function, Mono> writeHandler; public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) { @@ -68,16 +68,29 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { public MockClientHttpRequest(HttpMethod httpMethod, URI url) { this.httpMethod = httpMethod; this.url = url; - } - - private Function, Mono> initDefaultWriteHandler() { - return body -> { + this.writeHandler = body -> { this.body = body.cache(); return this.body.then(); }; } + /** + * Configure a custom handler for writing the request body. + * + *

The default write handler consumes and caches the request body so it + * may be accessed subsequently, e.g. in test assertions. Use this property + * when the request body is an infinite stream. + * + * @param writeHandler the write handler to use returning {@code Mono} + * when the body has been "written" (i.e. consumed). + */ + public void setWriteHandler(Function, Mono> writeHandler) { + Assert.notNull(writeHandler, "'writeHandler' is required"); + this.writeHandler = writeHandler; + } + + @Override public HttpMethod getMethod() { return this.httpMethod; @@ -93,6 +106,32 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { return this.bufferFactory; } + @Override + protected void applyHeaders() { + } + + @Override + protected void applyCookies() { + getCookies().values().stream().flatMap(Collection::stream) + .forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString())); + } + + @Override + public Mono writeWith(Publisher body) { + return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body)))); + } + + @Override + public Mono writeAndFlushWith(Publisher> body) { + return writeWith(Flux.from(body).flatMap(p -> p)); + } + + @Override + public Mono setComplete() { + return writeWith(Flux.empty()); + } + + /** * Return the request body, or an error stream if the body was never set * or when {@link #setWriteHandler} is configured. @@ -126,44 +165,4 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { return new String(bytes, charset); } - /** - * Configure a custom handler for writing the request body. - * - *

The default write handler consumes and caches the request body so it - * may be accessed subsequently, e.g. in test assertions. Use this property - * when the request body is an infinite stream. - * - * @param writeHandler the write handler to use returning {@code Mono} - * when the body has been "written" (i.e. consumed). - */ - public void setWriteHandler(Function, Mono> writeHandler) { - Assert.notNull(writeHandler, "'writeHandler' is required"); - this.writeHandler = writeHandler; - } - - @Override - protected void applyHeaders() { - } - - @Override - protected void applyCookies() { - getCookies().values().stream().flatMap(Collection::stream) - .forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString())); - } - - @Override - public Mono writeWith(Publisher body) { - return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body)))); - } - - @Override - public Mono writeAndFlushWith(Publisher> body) { - return writeWith(Flux.from(body).flatMap(p -> p)); - } - - @Override - public Mono setComplete() { - return writeWith(Flux.empty()); - } - }