Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ default Optional<Object> attribute(String name) {
// Static builder methods

/**
* Create a builder with the method, URI, headers, and cookies of the given request.
* @param other the request to copy the method, URI, headers, and cookies from
* Create a builder with the method, URI, headers, cookies and body of the given request.
* @param other the request to copy the method, URI, headers, cookies and body from
* @return the created builder
*/
static Builder from(ClientRequest other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ public void from() throws URISyntaxException {
assertThat(result.httpRequest()).isNotNull();
}

@Test
public void fromCopiesBody() {
String body = "foo";
BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
byte[] bodyBytes = body.getBytes(UTF_8);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);

return response.writeWith(Mono.just(buffer));
};

ClientRequest other = ClientRequest.create(POST, URI.create("https://example.com"))
.body(inserter).build();

ClientRequest result = ClientRequest.from(other).build();

List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));

ExchangeStrategies strategies = mock(ExchangeStrategies.class);
given(strategies.messageWriters()).willReturn(messageWriters);

MockClientHttpRequest request = new MockClientHttpRequest(POST, "/");
result.writeTo(request, strategies).block();

String copiedBody = request.getBodyAsString().block();

assertThat(copiedBody).isEqualTo("foo");
}

@Test
public void method() throws URISyntaxException {
URI url = new URI("https://example.com");
Expand Down