Polish MockClientHttpRequest
This commit is contained in:
parent
7f555785dd
commit
d74e09a925
|
@ -58,7 +58,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
new IllegalStateException("The body is not set. " +
|
new IllegalStateException("The body is not set. " +
|
||||||
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
|
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
|
||||||
|
|
||||||
private Function<Flux<DataBuffer>, Mono<Void>> writeHandler = initDefaultWriteHandler();
|
private Function<Flux<DataBuffer>, Mono<Void>> writeHandler;
|
||||||
|
|
||||||
|
|
||||||
public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
|
public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
|
||||||
|
@ -68,16 +68,29 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
public MockClientHttpRequest(HttpMethod httpMethod, URI url) {
|
public MockClientHttpRequest(HttpMethod httpMethod, URI url) {
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
this.writeHandler = body -> {
|
||||||
|
|
||||||
private Function<Flux<DataBuffer>, Mono<Void>> initDefaultWriteHandler() {
|
|
||||||
return body -> {
|
|
||||||
this.body = body.cache();
|
this.body = body.cache();
|
||||||
return this.body.then();
|
return this.body.then();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure a custom handler for writing the request body.
|
||||||
|
*
|
||||||
|
* <p>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<Void>}
|
||||||
|
* when the body has been "written" (i.e. consumed).
|
||||||
|
*/
|
||||||
|
public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> writeHandler) {
|
||||||
|
Assert.notNull(writeHandler, "'writeHandler' is required");
|
||||||
|
this.writeHandler = writeHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpMethod getMethod() {
|
public HttpMethod getMethod() {
|
||||||
return this.httpMethod;
|
return this.httpMethod;
|
||||||
|
@ -93,6 +106,32 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
return this.bufferFactory;
|
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<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
||||||
|
return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
||||||
|
return writeWith(Flux.from(body).flatMap(p -> p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> setComplete() {
|
||||||
|
return writeWith(Flux.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the request body, or an error stream if the body was never set
|
* Return the request body, or an error stream if the body was never set
|
||||||
* or when {@link #setWriteHandler} is configured.
|
* or when {@link #setWriteHandler} is configured.
|
||||||
|
@ -126,44 +165,4 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
return new String(bytes, charset);
|
return new String(bytes, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Configure a custom handler for writing the request body.
|
|
||||||
*
|
|
||||||
* <p>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<Void>}
|
|
||||||
* when the body has been "written" (i.e. consumed).
|
|
||||||
*/
|
|
||||||
public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> 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<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
|
||||||
return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
|
||||||
return writeWith(Flux.from(body).flatMap(p -> p));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> setComplete() {
|
|
||||||
return writeWith(Flux.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
new IllegalStateException("The body is not set. " +
|
new IllegalStateException("The body is not set. " +
|
||||||
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
|
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
|
||||||
|
|
||||||
private Function<Flux<DataBuffer>, Mono<Void>> writeHandler = initDefaultWriteHandler();
|
private Function<Flux<DataBuffer>, Mono<Void>> writeHandler;
|
||||||
|
|
||||||
|
|
||||||
public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
|
public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
|
||||||
|
@ -68,16 +68,29 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
public MockClientHttpRequest(HttpMethod httpMethod, URI url) {
|
public MockClientHttpRequest(HttpMethod httpMethod, URI url) {
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
this.writeHandler = body -> {
|
||||||
|
|
||||||
private Function<Flux<DataBuffer>, Mono<Void>> initDefaultWriteHandler() {
|
|
||||||
return body -> {
|
|
||||||
this.body = body.cache();
|
this.body = body.cache();
|
||||||
return this.body.then();
|
return this.body.then();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure a custom handler for writing the request body.
|
||||||
|
*
|
||||||
|
* <p>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<Void>}
|
||||||
|
* when the body has been "written" (i.e. consumed).
|
||||||
|
*/
|
||||||
|
public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> writeHandler) {
|
||||||
|
Assert.notNull(writeHandler, "'writeHandler' is required");
|
||||||
|
this.writeHandler = writeHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpMethod getMethod() {
|
public HttpMethod getMethod() {
|
||||||
return this.httpMethod;
|
return this.httpMethod;
|
||||||
|
@ -93,6 +106,32 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
return this.bufferFactory;
|
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<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
||||||
|
return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
||||||
|
return writeWith(Flux.from(body).flatMap(p -> p));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> setComplete() {
|
||||||
|
return writeWith(Flux.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the request body, or an error stream if the body was never set
|
* Return the request body, or an error stream if the body was never set
|
||||||
* or when {@link #setWriteHandler} is configured.
|
* or when {@link #setWriteHandler} is configured.
|
||||||
|
@ -126,44 +165,4 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
return new String(bytes, charset);
|
return new String(bytes, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Configure a custom handler for writing the request body.
|
|
||||||
*
|
|
||||||
* <p>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<Void>}
|
|
||||||
* when the body has been "written" (i.e. consumed).
|
|
||||||
*/
|
|
||||||
public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> 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<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
|
||||||
return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
|
||||||
return writeWith(Flux.from(body).flatMap(p -> p));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> setComplete() {
|
|
||||||
return writeWith(Flux.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue