Polishing contribution

See gh-24786
This commit is contained in:
Rossen Stoyanchev
2020-03-26 21:13:29 +00:00
parent f17b0125ff
commit 7aa06b8610
9 changed files with 45 additions and 81 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -149,18 +149,13 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset)
.orElse(StandardCharsets.UTF_8);
return getBody()
.reduce(bufferFactory().allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;
return DataBufferUtils.join(getBody())
.map(buffer -> {
String s = buffer.toString(charset);
DataBufferUtils.release(buffer);
return s;
})
.map(buffer -> bufferToString(buffer, charset));
}
private static String bufferToString(DataBuffer buffer, Charset charset) {
Assert.notNull(charset, "'charset' must not be null");
return buffer.toString(charset);
.defaultIfEmpty("");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -122,19 +122,13 @@ public class MockClientHttpResponse implements ClientHttpResponse {
* charset of the Content-Type response or otherwise as "UTF-8".
*/
public Mono<String> getBodyAsString() {
Charset charset = getCharset();
return Flux.from(getBody())
.reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;
return DataBufferUtils.join(getBody())
.map(buffer -> {
String s = buffer.toString(getCharset());
DataBufferUtils.release(buffer);
return s;
})
.map(buffer -> dumpString(buffer, charset));
}
private static String dumpString(DataBuffer buffer, Charset charset) {
Assert.notNull(charset, "'charset' must not be null");
return buffer.toString(charset);
.defaultIfEmpty("");
}
private Charset getCharset() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -142,20 +142,13 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse {
Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset)
.orElse(StandardCharsets.UTF_8);
return getBody()
.reduce(bufferFactory().allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;
return DataBufferUtils.join(getBody())
.map(buffer -> {
String s = buffer.toString(charset);
DataBufferUtils.release(buffer);
return s;
})
.map(buffer -> bufferToString(buffer, charset));
}
private static String bufferToString(DataBuffer buffer, Charset charset) {
Assert.notNull(charset, "'charset' must not be null");
String str = buffer.toString(charset);
DataBufferUtils.release(buffer);
return str;
.defaultIfEmpty("");
}
}