mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Improve converter config in RestTestClient.Builder
Compose consumers and apply them together from the build method to allow multiple parties to supply consumers that collaborate on the same HttpMessageConverters.Builder. See gh-35578
This commit is contained in:
+12
-1
@@ -19,6 +19,8 @@ package org.springframework.test.web.servlet.client;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
@@ -53,6 +55,8 @@ class DefaultRestTestClientBuilder<B extends RestTestClient.Builder<B>> implemen
|
||||
|
||||
private final RestClient.Builder restClientBuilder;
|
||||
|
||||
private @Nullable Consumer<HttpMessageConverters.ClientBuilder> convertersConfigurer;
|
||||
|
||||
private Consumer<EntityExchangeResult<?>> entityResultConsumer = result -> {};
|
||||
|
||||
|
||||
@@ -66,6 +70,7 @@ class DefaultRestTestClientBuilder<B extends RestTestClient.Builder<B>> implemen
|
||||
|
||||
DefaultRestTestClientBuilder(DefaultRestTestClientBuilder<B> other) {
|
||||
this.restClientBuilder = other.restClientBuilder.clone();
|
||||
this.convertersConfigurer = other.convertersConfigurer;
|
||||
this.entityResultConsumer = other.entityResultConsumer;
|
||||
}
|
||||
|
||||
@@ -132,7 +137,8 @@ class DefaultRestTestClientBuilder<B extends RestTestClient.Builder<B>> implemen
|
||||
|
||||
@Override
|
||||
public <T extends B> T configureMessageConverters(Consumer<HttpMessageConverters.ClientBuilder> configurer) {
|
||||
this.restClientBuilder.configureMessageConverters(configurer);
|
||||
this.convertersConfigurer = (this.convertersConfigurer != null ?
|
||||
this.convertersConfigurer.andThen(configurer) : configurer);
|
||||
return self();
|
||||
}
|
||||
|
||||
@@ -154,6 +160,11 @@ class DefaultRestTestClientBuilder<B extends RestTestClient.Builder<B>> implemen
|
||||
|
||||
@Override
|
||||
public RestTestClient build() {
|
||||
|
||||
if (this.convertersConfigurer != null) {
|
||||
this.restClientBuilder.configureMessageConverters(this.convertersConfigurer);
|
||||
}
|
||||
|
||||
return new DefaultRestTestClient(
|
||||
this.restClientBuilder, this.entityResultConsumer, new DefaultRestTestClientBuilder<>(this));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user