diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java index 59a94ddca34..89a8fd41ecf 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java @@ -134,6 +134,20 @@ public interface ClientRequest { */ interface Builder { + /** + * Set the method of the request. + * @param method the new method + * @return this builder + */ + Builder method(HttpMethod method); + + /** + * Set the url of the request. + * @param url the new url + * @return this builder + */ + Builder url(URI url); + /** * Add the given header value(s) under the given name. * @param headerName the header name diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java index a13205d0b65..28a7fcd4874 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java @@ -49,16 +49,16 @@ import org.springframework.web.reactive.function.BodyInserters; */ class DefaultClientRequestBuilder implements ClientRequest.Builder { - private final HttpMethod method; - - private final URI url; - private final HttpHeaders headers = new HttpHeaders(); private final MultiValueMap cookies = new LinkedMultiValueMap<>(); private final Map attributes = new LinkedHashMap<>(); + private HttpMethod method; + + private URI url; + private BodyInserter inserter = BodyInserters.empty(); @@ -67,6 +67,19 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder { this.url = url; } + @Override + public ClientRequest.Builder method(HttpMethod method) { + Assert.notNull(method, "'method' must not be null"); + this.method = method; + return this; + } + + @Override + public ClientRequest.Builder url(URI url) { + Assert.notNull(url, "'url' must not be null"); + this.url = url; + return this; + } @Override public ClientRequest.Builder header(String headerName, String... headerValues) { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java index 6901adbc08d..da23dfe507b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java @@ -40,6 +40,7 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import static org.springframework.http.HttpMethod.DELETE; import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.OPTIONS; import static org.springframework.http.HttpMethod.POST; /** @@ -67,9 +68,22 @@ public class DefaultClientRequestBuilderTests { @Test public void method() throws Exception { URI url = new URI("http://example.com"); - ClientRequest result = ClientRequest.method(DELETE, url).build(); - assertEquals(url, result.url()); - assertEquals(DELETE, result.method()); + ClientRequest.Builder builder = ClientRequest.method(DELETE, url); + assertEquals(DELETE, builder.build().method()); + + builder.method(OPTIONS); + assertEquals(OPTIONS, builder.build().method()); + } + + @Test + public void url() throws Exception { + URI url1 = new URI("http://example.com/foo"); + URI url2 = new URI("http://example.com/bar"); + ClientRequest.Builder builder = ClientRequest.method(DELETE, url1); + assertEquals(url1, builder.build().url()); + + builder.url(url2); + assertEquals(url2, builder.build().url()); } @Test