Skip to content

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

Merged
wilkinsona merged 2 commits intospring-projects:mainfrom
apoorvdarshan:fix/testresttemplate-default-cookie-handling
Mar 12, 2026
Merged

Update TestRestTemplate's default cookie handling to match RestTemplate's#49261
wilkinsona merged 2 commits intospring-projects:mainfrom
apoorvdarshan:fix/testresttemplate-default-cookie-handling

Conversation

@apoorvdarshan
Copy link
Contributor

@apoorvdarshan apoorvdarshan commented Feb 18, 2026

Summary

  • TestRestTemplate previously disabled cookies by default (StandardCookieSpec.IGNORE), differing from RestTemplate which uses the HTTP client library's default
  • Aligns the defaults so TestRestTemplate no longer explicitly ignores cookies
  • Adds withCookies(HttpCookies) method for explicit cookie control, following the same pattern as withRedirects(HttpRedirects) from Align TestRestTemplate default redirect settings to be the same as RestTemplate #43431
  • Introduces HttpCookies enum, HttpComponentsCookieSpec adapter, and cookies() on RestTemplateBuilder
  • Deprecates HttpClientOption.ENABLE_COOKIES

Fixes gh-48607

Test plan

  • TestRestTemplateTests — verifies default cookie spec is now null (library default), withCookies(ENABLE) returns "strict", withCookies(DISABLE) returns "ignoreCookies"
  • HttpClientSettingsTests — verifies withCookies() and updated orElse() with cookies field
  • HttpClientAutoConfigurationTests — verifies settings construction with new record shape
  • Existing redirect tests remain green

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 18, 2026
@apoorvdarshan apoorvdarshan force-pushed the fix/testresttemplate-default-cookie-handling branch from dff9449 to 7e42613 Compare February 19, 2026 06:24
Copy link
Member

@wilkinsona wilkinsona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @apoorvdarshan. In addition to the two comments I've made, I think some changes to the various other …HttpClientBuilder classes are missing. The required changes will be similar to the changes that you've made for HttpComponentsHttpClientBuilder to honour the cookies configuration.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Feb 25, 2026
@apoorvdarshan apoorvdarshan force-pushed the fix/testresttemplate-default-cookie-handling branch from 7e42613 to 1a8c7ba Compare February 25, 2026 15:39
Copy link
Contributor Author

@apoorvdarshan apoorvdarshan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review feedback @wilkinsona! Both items addressed:

  1. Updated the ENABLE_COOKIES deprecation to "for removal in 4.3.0"
  2. Added an explicit deprecated four-argument HttpClientSettings constructor that delegates to the new five-argument canonical constructor to preserve backward compatibility

@wilkinsona
Copy link
Member

wilkinsona commented Feb 25, 2026

Thanks. Did you see this additional comment?

I think some changes to the various other …HttpClientBuilder classes are missing. The required changes will be similar to the changes that you've made for HttpComponentsHttpClientBuilder to honour the cookies configuration.

@apoorvdarshan apoorvdarshan force-pushed the fix/testresttemplate-default-cookie-handling branch from 1a8c7ba to 56c5733 Compare February 25, 2026 15:59
@apoorvdarshan
Copy link
Contributor Author

Thanks for pointing that out! I've added cookie handling to the other builders:

  • JdkHttpClientBuilder: Maps HttpCookies to a CookieHandlerENABLE/ENABLE_WHEN_POSSIBLE sets a CookieManager, DISABLE leaves the default (no cookie handler)
  • JettyHttpClientBuilder: Maps HttpCookies to a HttpCookieStoreDISABLE sets HttpCookieStore.Empty, ENABLE/ENABLE_WHEN_POSSIBLE keeps the library default (cookies enabled)

Reactor Netty's HttpClient doesn't have automatic cookie management at the transport level, so no changes were made there.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 25, 2026
@wilkinsona wilkinsona self-assigned this Mar 12, 2026
@wilkinsona wilkinsona removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Mar 12, 2026
@wilkinsona wilkinsona added this to the 4.1.x milestone Mar 12, 2026
@wilkinsona wilkinsona added the type: enhancement A general enhancement label Mar 12, 2026
@wilkinsona wilkinsona force-pushed the fix/testresttemplate-default-cookie-handling branch from 56c5733 to e898779 Compare March 12, 2026 13:03
wilkinsona added a commit to apoorvdarshan/spring-boot that referenced this pull request Mar 12, 2026
…stTemplate's"

See spring-projectsgh-49261

Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
@wilkinsona wilkinsona force-pushed the fix/testresttemplate-default-cookie-handling branch from e898779 to 5721f57 Compare March 12, 2026 13:05
@wilkinsona wilkinsona modified the milestones: 4.1.x, 4.1.0-M3 Mar 12, 2026
@wilkinsona wilkinsona merged commit 5ef0c31 into spring-projects:main Mar 12, 2026
4 checks passed
@wilkinsona
Copy link
Member

Thank you, @apoorvdarshan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update TestRestTemplate's default cookie handling to be the same as RestTemplate's

3 participants