Skip to content

Commit e290ae2

Browse files
AlexejTimoninrstoyanchev
authored andcommitted
Improve Javadoc for ClientRequest#from
See gh-27220
1 parent 8670b1c commit e290ae2

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,8 +125,8 @@ default Optional<Object> attribute(String name) {
125125
// Static builder methods
126126

127127
/**
128-
* Create a builder with the method, URI, headers, and cookies of the given request.
129-
* @param other the request to copy the method, URI, headers, and cookies from
128+
* Create a builder with the method, URI, headers, cookies, attributes, and body of the given request.
129+
* @param other the request to copy the method, URI, headers, cookies, attributes, and body from
130130
* @return the created builder
131131
*/
132132
static Builder from(ClientRequest other) {

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,8 @@ public void from() throws URISyntaxException {
5656
ClientRequest other = ClientRequest.create(GET, URI.create("https://example.com"))
5757
.header("foo", "bar")
5858
.cookie("baz", "qux")
59+
.attribute("attributeKey", "attributeValue")
60+
.attribute("anotherAttributeKey", "anotherAttributeValue")
5961
.httpRequest(request -> {})
6062
.build();
6163
ClientRequest result = ClientRequest.from(other)
@@ -69,6 +71,37 @@ public void from() throws URISyntaxException {
6971
assertThat(result.cookies().size()).isEqualTo(1);
7072
assertThat(result.cookies().getFirst("baz")).isEqualTo("quux");
7173
assertThat(result.httpRequest()).isNotNull();
74+
assertThat(result.attributes().get("attributeKey")).isEqualTo("attributeValue");
75+
assertThat(result.attributes().get("anotherAttributeKey")).isEqualTo("anotherAttributeValue");
76+
}
77+
78+
@Test
79+
public void fromCopiesBody() {
80+
String body = "foo";
81+
BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
82+
byte[] bodyBytes = body.getBytes(UTF_8);
83+
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
84+
85+
return response.writeWith(Mono.just(buffer));
86+
};
87+
88+
ClientRequest other = ClientRequest.create(POST, URI.create("https://example.com"))
89+
.body(inserter).build();
90+
91+
ClientRequest result = ClientRequest.from(other).build();
92+
93+
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
94+
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
95+
96+
ExchangeStrategies strategies = mock(ExchangeStrategies.class);
97+
given(strategies.messageWriters()).willReturn(messageWriters);
98+
99+
MockClientHttpRequest request = new MockClientHttpRequest(POST, "/");
100+
result.writeTo(request, strategies).block();
101+
102+
String copiedBody = request.getBodyAsString().block();
103+
104+
assertThat(copiedBody).isEqualTo("foo");
72105
}
73106

74107
@Test

0 commit comments

Comments
 (0)