Skip to content

Commit 40a6fba

Browse files
committed
Allow to change URL/method in ClientRequest.Builder
This commit exposes the ClientRequest's URL and HttpMethod fields via a setter, so that they can be changed more easily in a request that was created via ClientRequest.from(ClientRequest). Issue: SPR-16093
1 parent 585ad59 commit 40a6fba

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ static Builder method(HttpMethod method, URI url) {
134134
*/
135135
interface Builder {
136136

137+
/**
138+
* Set the method of the request.
139+
* @param method the new method
140+
* @return this builder
141+
*/
142+
Builder method(HttpMethod method);
143+
144+
/**
145+
* Set the url of the request.
146+
* @param url the new url
147+
* @return this builder
148+
*/
149+
Builder url(URI url);
150+
137151
/**
138152
* Add the given header value(s) under the given name.
139153
* @param headerName the header name

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949
*/
5050
class DefaultClientRequestBuilder implements ClientRequest.Builder {
5151

52-
private final HttpMethod method;
53-
54-
private final URI url;
55-
5652
private final HttpHeaders headers = new HttpHeaders();
5753

5854
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
5955

6056
private final Map<String, Object> attributes = new LinkedHashMap<>();
6157

58+
private HttpMethod method;
59+
60+
private URI url;
61+
6262
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
6363

6464

@@ -67,6 +67,19 @@ public DefaultClientRequestBuilder(HttpMethod method, URI url) {
6767
this.url = url;
6868
}
6969

70+
@Override
71+
public ClientRequest.Builder method(HttpMethod method) {
72+
Assert.notNull(method, "'method' must not be null");
73+
this.method = method;
74+
return this;
75+
}
76+
77+
@Override
78+
public ClientRequest.Builder url(URI url) {
79+
Assert.notNull(url, "'url' must not be null");
80+
this.url = url;
81+
return this;
82+
}
7083

7184
@Override
7285
public ClientRequest.Builder header(String headerName, String... headerValues) {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.mockito.Mockito.*;
4141
import static org.springframework.http.HttpMethod.DELETE;
4242
import static org.springframework.http.HttpMethod.GET;
43+
import static org.springframework.http.HttpMethod.OPTIONS;
4344
import static org.springframework.http.HttpMethod.POST;
4445

4546
/**
@@ -67,9 +68,22 @@ public void from() throws Exception {
6768
@Test
6869
public void method() throws Exception {
6970
URI url = new URI("http://example.com");
70-
ClientRequest result = ClientRequest.method(DELETE, url).build();
71-
assertEquals(url, result.url());
72-
assertEquals(DELETE, result.method());
71+
ClientRequest.Builder builder = ClientRequest.method(DELETE, url);
72+
assertEquals(DELETE, builder.build().method());
73+
74+
builder.method(OPTIONS);
75+
assertEquals(OPTIONS, builder.build().method());
76+
}
77+
78+
@Test
79+
public void url() throws Exception {
80+
URI url1 = new URI("http://example.com/foo");
81+
URI url2 = new URI("http://example.com/bar");
82+
ClientRequest.Builder builder = ClientRequest.method(DELETE, url1);
83+
assertEquals(url1, builder.build().url());
84+
85+
builder.url(url2);
86+
assertEquals(url2, builder.build().url());
7387
}
7488

7589
@Test

0 commit comments

Comments
 (0)