Polish 'Add `redirects(...)` method to `RestTemplateBuilder`'
Remove deprecations and new methods in `TestRestTemplate` in favor of passing in a configured `RestTemplateBuilder`. See gh-43258
This commit is contained in:
parent
f450b28a7c
commit
8b83afdb68
|
@ -948,22 +948,6 @@ public class TestRestTemplate {
|
|||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code TestRestTemplate} with the same configuration as this one,
|
||||
* except that it will use the given {@code redirects} strategies. The request factory
|
||||
* used is a new instance of the underlying {@link RestTemplate}'s request factory
|
||||
* type (when possible).
|
||||
* @param redirects the redirects
|
||||
* @return the new template
|
||||
* @since 3.4.1
|
||||
*/
|
||||
public TestRestTemplate withRedirects(Redirects redirects) {
|
||||
TestRestTemplate template = new TestRestTemplate(this.builder.redirects(redirects), null, null,
|
||||
this.httpClientOptions);
|
||||
template.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
|
||||
return template;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private RequestEntity<?> createRequestEntityWithRootAppliedUri(RequestEntity<?> requestEntity) {
|
||||
return new RequestEntity(requestEntity.getBody(), requestEntity.getHeaders(), requestEntity.getMethod(),
|
||||
|
@ -1006,10 +990,7 @@ public class TestRestTemplate {
|
|||
|
||||
/**
|
||||
* Enable redirects.
|
||||
* @deprecated since 3.4.1 for removal in 3.6.0 in favor of
|
||||
* {@link #withRedirects(Redirects)}
|
||||
*/
|
||||
@Deprecated(since = "3.4.1", forRemoval = true)
|
||||
ENABLE_REDIRECTS,
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,12 +20,15 @@ import java.io.IOException;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpClient.Redirect;
|
||||
import java.util.Base64;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
|
||||
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate.HttpClientOption;
|
||||
|
@ -39,6 +42,7 @@ import org.springframework.http.RequestEntity;
|
|||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
|
@ -131,30 +135,55 @@ class TestRestTemplateTests {
|
|||
assertBasicAuthorizationCredentials(restTemplate, "user", "password");
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
void options() {
|
||||
TestRestTemplate template = new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS);
|
||||
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
|
||||
.getRestTemplate()
|
||||
.getRequestFactory();
|
||||
RequestConfig config = factory.createRequestConfig();
|
||||
RequestConfig config = getRequestConfig(
|
||||
new TestRestTemplate(HttpClientOption.ENABLE_REDIRECTS, HttpClientOption.ENABLE_COOKIES));
|
||||
assertThat(config.isRedirectsEnabled()).isTrue();
|
||||
assertThat(config.getCookieSpec()).isEqualTo("strict");
|
||||
}
|
||||
|
||||
@Test
|
||||
void redirects() {
|
||||
TestRestTemplate template = new TestRestTemplate().withRedirects(Redirects.DONT_FOLLOW);
|
||||
void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder()
|
||||
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.jdk());
|
||||
TestRestTemplate templateWithRedirects = new TestRestTemplate(builder.redirects(Redirects.FOLLOW));
|
||||
assertThat(getJdkHttpClient(templateWithRedirects).followRedirects()).isEqualTo(Redirect.NORMAL);
|
||||
TestRestTemplate templateWithoutRedirects = new TestRestTemplate(builder.redirects(Redirects.DONT_FOLLOW));
|
||||
assertThat(getJdkHttpClient(templateWithoutRedirects).followRedirects()).isEqualTo(Redirect.NEVER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpComponentsAreBuildConsideringSettingsInRestTemplateBuilder() {
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder()
|
||||
.requestFactoryBuilder(ClientHttpRequestFactoryBuilder.httpComponents());
|
||||
assertThat(getRequestConfig((RestTemplateBuilder) null).isRedirectsEnabled()).isTrue();
|
||||
assertThat(getRequestConfig(null, HttpClientOption.ENABLE_REDIRECTS).isRedirectsEnabled()).isTrue();
|
||||
assertThat(getRequestConfig(builder).isRedirectsEnabled()).isTrue();
|
||||
assertThat(getRequestConfig(builder, HttpClientOption.ENABLE_REDIRECTS).isRedirectsEnabled()).isTrue();
|
||||
assertThat(getRequestConfig(builder.redirects(Redirects.DONT_FOLLOW)).isRedirectsEnabled()).isFalse();
|
||||
assertThat(getRequestConfig(builder.redirects(Redirects.DONT_FOLLOW), HttpClientOption.ENABLE_REDIRECTS)
|
||||
.isRedirectsEnabled()).isTrue();
|
||||
}
|
||||
|
||||
private RequestConfig getRequestConfig(RestTemplateBuilder builder, HttpClientOption... httpClientOptions) {
|
||||
builder = (builder != null) ? builder : new RestTemplateBuilder();
|
||||
TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions);
|
||||
return getRequestConfig(template);
|
||||
}
|
||||
|
||||
private RequestConfig getRequestConfig(TestRestTemplate template) {
|
||||
CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory) template
|
||||
.getRestTemplate()
|
||||
.getRequestFactory();
|
||||
RequestConfig config = factory.createRequestConfig();
|
||||
assertThat(config.isRedirectsEnabled()).isFalse();
|
||||
return factory.createRequestConfig();
|
||||
}
|
||||
|
||||
template = new TestRestTemplate().withRedirects(Redirects.FOLLOW);
|
||||
factory = (CustomHttpComponentsClientHttpRequestFactory) template.getRestTemplate().getRequestFactory();
|
||||
config = factory.createRequestConfig();
|
||||
assertThat(config.isRedirectsEnabled()).isTrue();
|
||||
private HttpClient getJdkHttpClient(TestRestTemplate templateWithRedirects) {
|
||||
JdkClientHttpRequestFactory requestFactory = (JdkClientHttpRequestFactory) templateWithRedirects
|
||||
.getRestTemplate()
|
||||
.getRequestFactory();
|
||||
return (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue