When migrating a project that uses httpclient 4 any TestRestTemplate that uses detection will drop back to the simple implementation which may silently drop headers.
This issue was found when upgrading https://github.com/spring-guides/gs-rest-service-cors.
That project declares:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
On Spring Boot 2.7 that will allow the "Origin" header to be sent, but on upgrade the TestRestTemplate instance will instead use the simple implementation. This uses sun.net.www.protocol.http.HttpURLConnection which will silently drop restricted "Origin" headers.
The fix is to add the following:
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<scope>test</scope>
</dependency>
Since the error is very difficult to diagnose, it might be nice if we could provide some guidance.
Comment From: martinqcz
I experienced another issue with the TestRestTemplate and Spring Boot 3.0.1. By default it does not allow to use HTTP PATCH method in requests ad throws java.net.ProtocolException: Invalid HTTP method: PATCH If I add org.apache.httpcomponents.client5:httpclient5 dependency, than it starts to work.
Comment From: bclozel
We've added a note in the migration guide.