Add API versioning to RestTestClient

See gh-34428
This commit is contained in:
rstoyanchev
2025-07-29 20:54:16 +01:00
parent 862ffee385
commit 6cc1310274
4 changed files with 162 additions and 2 deletions
@@ -226,6 +226,12 @@ class DefaultRestTestClient implements RestTestClient {
return this;
}
@Override
public RequestBodySpec apiVersion(Object version) {
this.requestHeadersUriSpec.apiVersion(version);
return this;
}
@Override
public RequestHeadersSpec<?> body(Object body) {
this.requestHeadersUriSpec.body(body);
@@ -235,8 +241,8 @@ class DefaultRestTestClient implements RestTestClient {
@Override
public ResponseSpec exchange() {
return new DefaultResponseSpec(
this.requestHeadersUriSpec.exchangeForRequiredValue((request, response) ->
new ExchangeResult(request, response, this.uriTemplate), false));
this.requestHeadersUriSpec.exchangeForRequiredValue(
(request, response) -> new ExchangeResult(request, response, this.uriTemplate), false));
}
}
@@ -31,6 +31,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.RouterFunctionMockMvcBuilder;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.client.RestClient;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.function.RouterFunction;
@@ -94,6 +95,18 @@ class DefaultRestTestClientBuilder<B extends RestTestClient.Builder<B>> implemen
return self();
}
@Override
public <T extends B> T defaultApiVersion(Object version) {
this.restClientBuilder.defaultApiVersion(version);
return self();
}
@Override
public <T extends B> T apiVersionInserter(ApiVersionInserter apiVersionInserter) {
this.restClientBuilder.apiVersionInserter(apiVersionInserter);
return self();
}
@SuppressWarnings("unchecked")
protected <T extends B> T self() {
return (T) this;
@@ -41,6 +41,8 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.test.web.servlet.setup.RouterFunctionMockMvcBuilder;
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ApiVersionFormatter;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.client.RestClient;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.function.RouterFunction;
@@ -241,6 +243,24 @@ public interface RestTestClient {
*/
<T extends B> T defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
/**
* Global option to specify an API version to add to every request,
* if not already set.
* @param version the version to use
* @return this builder
* @since 7.0
*/
<T extends B> T defaultApiVersion(Object version);
/**
* Configure an {@link ApiVersionInserter} to abstract how an API version
* specified via {@link RequestHeadersSpec#apiVersion(Object)}
* is inserted into the request.
* @param apiVersionInserter the inserter to use
* @since 7.0
*/
<T extends B> T apiVersionInserter(ApiVersionInserter apiVersionInserter);
/**
* Build the {@link RestTestClient} instance.
*/
@@ -403,6 +423,17 @@ public interface RestTestClient {
*/
S headers(Consumer<HttpHeaders> headersConsumer);
/**
* Set an API version for the request. The version is inserted into the
* request by the {@linkplain Builder#apiVersionInserter(ApiVersionInserter)
* configured} {@code ApiVersionInserter}.
* @param version the API version of the request; this can be a String or
* some Object that can be formatted by the inserter &mdash; for example,
* through an {@link ApiVersionFormatter}
* @since 7.0
*/
S apiVersion(Object version);
/**
* Set the attribute with the given name to the given value.
* @param name the name of the attribute to add