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
This commit is contained in:
parent
585ad5961a
commit
40a6fba443
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<String, String> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
private final Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
|
||||
private HttpMethod method;
|
||||
|
||||
private URI url;
|
||||
|
||||
private BodyInserter<?, ? super ClientHttpRequest> 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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue