Update TestRestTemplate's default cookie handling to match RestTemplate's

See gh-49261

Signed-off-by: Apoorv Darshan <ad13dtu@gmail.com>
This commit is contained in:
Apoorv Darshan
2026-03-12 12:13:27 +00:00
committed by Andy Wilkinson
parent 5ca8e0956a
commit 085d522570
12 changed files with 222 additions and 21 deletions
@@ -41,6 +41,7 @@ import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpClientSettings;
import org.springframework.boot.http.client.HttpComponentsClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpComponentsHttpClientBuilder.TlsSocketStrategyFactory;
import org.springframework.boot.http.client.HttpCookies;
import org.springframework.boot.http.client.HttpRedirects;
import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
@@ -69,8 +70,7 @@ import org.springframework.web.util.UriTemplateHandler;
* status code}.
* <p>
* A {@code TestRestTemplate} can optionally carry Basic authentication headers. If Apache
* Http Client 4.3.2 or better is available (recommended) it will be used as the client,
* and by default configured to ignore cookies.
* Http Client 4.3.2 or better is available (recommended) it will be used as the client.
* <p>
* Note: To prevent injection problems this class intentionally does not extend
* {@link RestTemplate}. If you need access to the underlying {@link RestTemplate} use
@@ -160,10 +160,13 @@ public class TestRestTemplate {
return builder;
}
@SuppressWarnings("deprecation")
private static HttpComponentsClientHttpRequestFactoryBuilder applyHttpClientOptions(
HttpComponentsClientHttpRequestFactoryBuilder builder, HttpClientOption[] httpClientOptions) {
builder = builder.withDefaultRequestConfigCustomizer(
new CookieSpecCustomizer(HttpClientOption.ENABLE_COOKIES.isPresent(httpClientOptions)));
if (HttpClientOption.ENABLE_COOKIES.isPresent(httpClientOptions)) {
builder = builder.withDefaultRequestConfigCustomizer(
new CookieSpecCustomizer(true));
}
if (HttpClientOption.SSL.isPresent(httpClientOptions)) {
builder = builder.withTlsSocketStrategyFactory(new SelfSignedTlsSocketStrategyFactory());
}
@@ -974,6 +977,19 @@ public class TestRestTemplate {
return withClientSettings((settings) -> settings.withRedirects(redirects));
}
/**
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
* except that it will apply the given {@link HttpCookies}. The request factory used is
* a new instance of the underlying {@link RestTemplate}'s request factory type (when
* possible).
* @param cookies the new cookie settings
* @return the new template
* @since 4.1.0
*/
public TestRestTemplate withCookies(HttpCookies cookies) {
return withClientSettings((settings) -> settings.withCookies(cookies));
}
/**
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
* except that it will apply the given {@link HttpClientSettings}. The request factory
@@ -1036,7 +1052,10 @@ public class TestRestTemplate {
/**
* Enable cookies.
* @deprecated since 4.1.0 for removal in 4.3.0 in favor of
* {@link TestRestTemplate#withCookies(HttpCookies)}
*/
@Deprecated(since = "4.1.0", forRemoval = true)
ENABLE_COOKIES,
/**
@@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpClientSettings;
import org.springframework.boot.http.client.HttpCookies;
import org.springframework.boot.http.client.HttpRedirects;
import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.boot.resttestclient.TestRestTemplate.HttpClientOption;
@@ -140,11 +141,26 @@ class TestRestTemplateTests {
}
@Test
@SuppressWarnings("removal")
void options() {
RequestConfig config = getRequestConfig(new TestRestTemplate(HttpClientOption.ENABLE_COOKIES));
assertThat(config.getCookieSpec()).isEqualTo("strict");
}
@Test
void defaultCookieSpecMatchesRestTemplate() {
RequestConfig config = getRequestConfig(new TestRestTemplate());
assertThat(config.getCookieSpec()).isNull();
}
@Test
void withCookies() {
TestRestTemplate template = new TestRestTemplate();
assertThat(getRequestConfig(template).getCookieSpec()).isNull();
assertThat(getRequestConfig(template.withCookies(HttpCookies.ENABLE)).getCookieSpec()).isEqualTo("strict");
assertThat(getRequestConfig(template.withCookies(HttpCookies.DISABLE)).getCookieSpec()).isEqualTo("ignoreCookies");
}
@Test
void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
RestTemplateBuilder builder = new RestTemplateBuilder()