Skip to content

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

Open
@quaff

Description

@quaff

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions