Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable redirects by default in TestRestTemplate for all HTTP clients #27360

Open
quaff opened this issue Jul 16, 2021 · 4 comments
Open

Disable redirects by default in TestRestTemplate for all HTTP clients #27360

quaff opened this issue Jul 16, 2021 · 4 comments
Labels
theme: http-client-config Issues related to configuration of HTTP clients type: enhancement A general enhancement

Comments

@quaff
Copy link
Contributor

quaff commented Jul 16, 2021

My test is broken after rest-assured dependency removed, I found it caused by TestRestTemplate inconsistent follow redirects. TestRestTemplate will use apache httpclient since org.apache.httpcomponents: httpclient is introduced by rest-assured, following redirects is disabled by default, thanks to #1497, you can enable it by passing HttpClientOption.ENABLE_REDIRECTS.
If apache httpclient is not present, TestRestTemplate will fallback to okhttp3:

Map<String, String> candidates = new LinkedHashMap<>();
candidates.put("org.apache.http.client.HttpClient",
"org.springframework.http.client.HttpComponentsClientHttpRequestFactory");
candidates.put("okhttp3.OkHttpClient", "org.springframework.http.client.OkHttp3ClientHttpRequestFactory");
REQUEST_FACTORY_CANDIDATES = Collections.unmodifiableMap(candidates);

okhttp3 enable following redirects by default, I think TestRestTemplate should keep same behavior not matter which underlying implementation, disabling following redirects is sensible since test want to know which url is redirected to, and it would be better if this option can be mutated.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 16, 2021
@quaff
Copy link
Contributor Author

quaff commented Jul 16, 2021

Implementation Default
HttpComponentsClientHttpRequestFactory false
OkHttp3ClientHttpRequestFactory true
SimpleClientHttpRequestFactory true

@wilkinsona
Copy link
Member

Thanks for the suggestion. As described in the javadoc, the current behaviour is expected:

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 and redirects.

We can explore the possibility of doing the same for other clients.

@wilkinsona wilkinsona changed the title Disable follow redirects for TestRestTemplate by default Disable redirects by default in TestRestTemplate for all HTTP clients Jul 16, 2021
@wilkinsona wilkinsona added the type: enhancement A general enhancement label Jul 16, 2021
@quaff
Copy link
Contributor Author

quaff commented Jul 16, 2021

Or provide a mutate method withFollowRedirects like withBasicAuth for TestRestTemplate.

@wilkinsona wilkinsona removed the status: waiting-for-triage An issue we've not yet triaged label Jan 19, 2022
@wilkinsona wilkinsona added this to the General Backlog milestone Jan 19, 2022
@philwebb philwebb added the theme: http-client-config Issues related to configuration of HTTP clients label Oct 25, 2024
@quaff
Copy link
Contributor Author

quaff commented Nov 22, 2024

@philwebb TestRestTemplate use FOLLOW_WHEN_POSSIBLE since 3.4.0, it breaks tests that rely on DONT_FOLLOW.

EDIT: it's caused by followRedirects of jdk.internal.net.http.HttpClientImpl changed from NEVER to NORMAL after upgrading to 3.4.0.

quaff added a commit to quaff/spring-boot that referenced this issue Nov 22, 2024
quaff added a commit to quaff/spring-boot that referenced this issue Nov 25, 2024
rahul-chekuri added a commit to rahul-chekuri/gate that referenced this issue Dec 19, 2024
…emoval of Apache HttpClient in Spring Framework 6

Spring Boot 3 uses Spring Framework 6, which no longer includes support for Apache HttpClient. As a result, tests failed due to the underlying OkHttp3 client redirecting requests differently.

Error Details:
```
AuthConfigTest > forwardWithCorrectCreds() FAILED
    org.opentest4j.AssertionFailedError:
    expected: 302 FOUND
     but was: 200 OK
        at java.base@17.0.13/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base@17.0.13/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base@17.0.13/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base@17.0.13/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at app//com.netflix.spinnaker.gate.AuthConfigTest.forwardWithCorrectCreds(AuthConfigTest.java:134)
```

For more details, refer below docs:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#apache-httpclient-in-resttemplate
spring-projects/spring-boot#27360 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: http-client-config Issues related to configuration of HTTP clients type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants