|
22 | 22 | import java.security.NoSuchAlgorithmException; |
23 | 23 | import java.security.cert.X509Certificate; |
24 | 24 | import java.time.Duration; |
25 | | -import java.util.Arrays; |
26 | | -import java.util.HashSet; |
27 | 25 | import java.util.Map; |
28 | 26 | import java.util.Set; |
29 | 27 | import java.util.concurrent.TimeUnit; |
|
60 | 58 | import org.springframework.http.client.ClientHttpRequestFactory; |
61 | 59 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
62 | 60 | import org.springframework.util.Assert; |
| 61 | +import org.springframework.util.ObjectUtils; |
63 | 62 | import org.springframework.web.client.NoOpResponseErrorHandler; |
64 | 63 | import org.springframework.web.client.RequestCallback; |
65 | 64 | import org.springframework.web.client.ResponseExtractor; |
@@ -100,8 +99,6 @@ public class TestRestTemplate { |
100 | 99 |
|
101 | 100 | private final RestTemplateBuilder builder; |
102 | 101 |
|
103 | | - private final HttpClientOption[] httpClientOptions; |
104 | | - |
105 | 102 | private final RestTemplate restTemplate; |
106 | 103 |
|
107 | 104 | /** |
@@ -142,21 +139,34 @@ public TestRestTemplate(String username, String password, HttpClientOption... ht |
142 | 139 | */ |
143 | 140 | public TestRestTemplate(RestTemplateBuilder builder, String username, String password, |
144 | 141 | HttpClientOption... httpClientOptions) { |
145 | | - Assert.notNull(builder, "Builder must not be null"); |
| 142 | + this(createInitialBuilder(builder, username, password, httpClientOptions), null); |
| 143 | + } |
| 144 | + |
| 145 | + private TestRestTemplate(RestTemplateBuilder builder, UriTemplateHandler uriTemplateHandler) { |
146 | 146 | this.builder = builder; |
147 | | - this.httpClientOptions = httpClientOptions; |
| 147 | + this.restTemplate = builder.build(); |
| 148 | + if (uriTemplateHandler != null) { |
| 149 | + this.restTemplate.setUriTemplateHandler(uriTemplateHandler); |
| 150 | + } |
| 151 | + this.restTemplate.setErrorHandler(new NoOpResponseErrorHandler()); |
| 152 | + } |
| 153 | + |
| 154 | + private static RestTemplateBuilder createInitialBuilder(RestTemplateBuilder builder, String username, |
| 155 | + String password, HttpClientOption... httpClientOptions) { |
| 156 | + Assert.notNull(builder, "Builder must not be null"); |
148 | 157 | if (httpClientOptions != null) { |
149 | 158 | ClientHttpRequestFactory requestFactory = builder.buildRequestFactory(); |
150 | 159 | if (requestFactory instanceof HttpComponentsClientHttpRequestFactory) { |
| 160 | + builder = builder.redirects(HttpClientOption.ENABLE_REDIRECTS.isPresent(httpClientOptions) |
| 161 | + ? Redirects.FOLLOW : Redirects.DONT_FOLLOW); |
151 | 162 | builder = builder.requestFactoryBuilder( |
152 | 163 | (settings) -> new CustomHttpComponentsClientHttpRequestFactory(httpClientOptions, settings)); |
153 | 164 | } |
154 | 165 | } |
155 | 166 | if (username != null || password != null) { |
156 | 167 | builder = builder.basicAuthentication(username, password); |
157 | 168 | } |
158 | | - this.restTemplate = builder.build(); |
159 | | - this.restTemplate.setErrorHandler(new NoOpResponseErrorHandler()); |
| 169 | + return builder; |
160 | 170 | } |
161 | 171 |
|
162 | 172 | /** |
@@ -943,9 +953,25 @@ public RestTemplate getRestTemplate() { |
943 | 953 | * @since 1.4.1 |
944 | 954 | */ |
945 | 955 | public TestRestTemplate withBasicAuth(String username, String password) { |
946 | | - TestRestTemplate template = new TestRestTemplate(this.builder, username, password, this.httpClientOptions); |
947 | | - template.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler()); |
948 | | - return template; |
| 956 | + if (username == null && password == null) { |
| 957 | + return this; |
| 958 | + } |
| 959 | + return new TestRestTemplate(this.builder.basicAuthentication(username, password), |
| 960 | + this.restTemplate.getUriTemplateHandler()); |
| 961 | + } |
| 962 | + |
| 963 | + /** |
| 964 | + * Creates a new {@code TestRestTemplate} with the same configuration as this one, |
| 965 | + * except that it will apply the given {@link ClientHttpRequestFactorySettings}. The |
| 966 | + * request factory used is a new instance of the underlying {@link RestTemplate}'s |
| 967 | + * request factory type (when possible). |
| 968 | + * @param requestFactorySettings the new request factory settings |
| 969 | + * @return the new template |
| 970 | + * @since 3.4.1 |
| 971 | + */ |
| 972 | + public TestRestTemplate withRequestFactorySettings(ClientHttpRequestFactorySettings requestFactorySettings) { |
| 973 | + return new TestRestTemplate(this.builder.requestFactorySettings(requestFactorySettings), |
| 974 | + this.restTemplate.getUriTemplateHandler()); |
949 | 975 | } |
950 | 976 |
|
951 | 977 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
@@ -996,7 +1022,11 @@ public enum HttpClientOption { |
996 | 1022 | /** |
997 | 1023 | * Use a {@link TlsSocketStrategy} that trusts self-signed certificates. |
998 | 1024 | */ |
999 | | - SSL |
| 1025 | + SSL; |
| 1026 | + |
| 1027 | + boolean isPresent(HttpClientOption[] options) { |
| 1028 | + return ObjectUtils.containsElement(options, this); |
| 1029 | + } |
1000 | 1030 |
|
1001 | 1031 | } |
1002 | 1032 |
|
@@ -1031,12 +1061,10 @@ public CustomHttpComponentsClientHttpRequestFactory(HttpClientOption[] httpClien |
1031 | 1061 | */ |
1032 | 1062 | public CustomHttpComponentsClientHttpRequestFactory(HttpClientOption[] httpClientOptions, |
1033 | 1063 | ClientHttpRequestFactorySettings settings) { |
1034 | | - Set<HttpClientOption> options = new HashSet<>(Arrays.asList(httpClientOptions)); |
1035 | | - this.cookieSpec = (options.contains(HttpClientOption.ENABLE_COOKIES) ? StandardCookieSpec.STRICT |
| 1064 | + this.cookieSpec = (HttpClientOption.ENABLE_COOKIES.isPresent(httpClientOptions) ? StandardCookieSpec.STRICT |
1036 | 1065 | : StandardCookieSpec.IGNORE); |
1037 | | - this.enableRedirects = options.contains(HttpClientOption.ENABLE_REDIRECTS) |
1038 | | - || settings.redirects() != Redirects.DONT_FOLLOW; |
1039 | | - boolean ssl = options.contains(HttpClientOption.SSL); |
| 1066 | + this.enableRedirects = settings.redirects() != Redirects.DONT_FOLLOW; |
| 1067 | + boolean ssl = HttpClientOption.SSL.isPresent(httpClientOptions); |
1040 | 1068 | if (settings.readTimeout() != null || ssl) { |
1041 | 1069 | setHttpClient(createHttpClient(settings.readTimeout(), ssl)); |
1042 | 1070 | } |
|
0 commit comments