1
1
/*
2
- * Copyright 2012-2024 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import java .util .stream .Stream ;
27
27
28
28
import org .apache .hc .client5 .http .config .RequestConfig ;
29
+ import org .apache .hc .client5 .http .impl .DefaultRedirectStrategy ;
30
+ import org .apache .hc .client5 .http .impl .classic .RedirectExec ;
31
+ import org .apache .hc .client5 .http .protocol .RedirectStrategy ;
32
+ import org .assertj .core .extractor .Extractors ;
29
33
import org .junit .jupiter .api .Test ;
30
34
31
35
import org .springframework .boot .http .client .ClientHttpRequestFactoryBuilder ;
32
36
import org .springframework .boot .http .client .ClientHttpRequestFactorySettings ;
33
37
import org .springframework .boot .http .client .ClientHttpRequestFactorySettings .Redirects ;
34
- import org .springframework .boot .test .web .client .TestRestTemplate .CustomHttpComponentsClientHttpRequestFactory ;
35
38
import org .springframework .boot .test .web .client .TestRestTemplate .HttpClientOption ;
36
39
import org .springframework .boot .web .client .RestTemplateBuilder ;
37
40
import org .springframework .core .ParameterizedTypeReference ;
@@ -137,6 +140,7 @@ void authenticated() {
137
140
}
138
141
139
142
@ Test
143
+ @ SuppressWarnings ("removal" )
140
144
void options () {
141
145
RequestConfig config = getRequestConfig (
142
146
new TestRestTemplate (HttpClientOption .ENABLE_REDIRECTS , HttpClientOption .ENABLE_COOKIES ));
@@ -155,25 +159,29 @@ void jdkBuilderCanBeSpecifiedWithSpecificRedirects() {
155
159
}
156
160
157
161
@ Test
162
+ @ SuppressWarnings ("removal" )
158
163
void httpComponentsAreBuiltConsideringSettingsInRestTemplateBuilder () {
159
164
RestTemplateBuilder builder = new RestTemplateBuilder ()
160
165
.requestFactoryBuilder (ClientHttpRequestFactoryBuilder .httpComponents ());
161
- assertThat (getRequestConfig ((RestTemplateBuilder ) null ). isRedirectsEnabled ()). isFalse ( );
162
- assertThat (getRequestConfig (null , HttpClientOption .ENABLE_REDIRECTS ). isRedirectsEnabled ()). isTrue ( );
163
- assertThat (getRequestConfig (builder ). isRedirectsEnabled ()). isFalse ( );
164
- assertThat (getRequestConfig (builder , HttpClientOption .ENABLE_REDIRECTS ). isRedirectsEnabled ()). isTrue ( );
165
- assertThat (getRequestConfig (builder .redirects (Redirects .DONT_FOLLOW )). isRedirectsEnabled ()). isFalse ( );
166
- assertThat (getRequestConfig (builder .redirects (Redirects .DONT_FOLLOW ), HttpClientOption .ENABLE_REDIRECTS )
167
- .isRedirectsEnabled ()). isTrue ( );
166
+ assertThat (getRedirectStrategy ((RestTemplateBuilder ) null )). matches ( this :: isDontFollowStrategy );
167
+ assertThat (getRedirectStrategy (null , HttpClientOption .ENABLE_REDIRECTS )). matches ( this :: isFollowStrategy );
168
+ assertThat (getRedirectStrategy (builder )). matches ( this :: isDontFollowStrategy );
169
+ assertThat (getRedirectStrategy (builder , HttpClientOption .ENABLE_REDIRECTS )). matches ( this :: isFollowStrategy );
170
+ assertThat (getRedirectStrategy (builder .redirects (Redirects .DONT_FOLLOW ))). matches ( this :: isDontFollowStrategy );
171
+ assertThat (getRedirectStrategy (builder .redirects (Redirects .DONT_FOLLOW ), HttpClientOption .ENABLE_REDIRECTS ) )
172
+ .matches ( this :: isFollowStrategy );
168
173
}
169
174
170
175
@ Test
171
176
void withRequestFactorySettingsRedirectsForHttpComponents () {
172
177
TestRestTemplate template = new TestRestTemplate ();
173
- assertThat (getRequestConfig (template ).isRedirectsEnabled ()).isFalse ();
174
- assertThat (getRequestConfig (template
175
- .withRequestFactorySettings (ClientHttpRequestFactorySettings .defaults ().withRedirects (Redirects .FOLLOW )))
176
- .isRedirectsEnabled ()).isTrue ();
178
+ assertThat (getRedirectStrategy (template )).matches (this ::isDontFollowStrategy );
179
+ assertThat (getRedirectStrategy (template
180
+ .withRequestFactorySettings (ClientHttpRequestFactorySettings .defaults ().withRedirects (Redirects .FOLLOW ))))
181
+ .matches (this ::isFollowStrategy );
182
+ assertThat (getRedirectStrategy (template .withRequestFactorySettings (
183
+ ClientHttpRequestFactorySettings .defaults ().withRedirects (Redirects .DONT_FOLLOW ))))
184
+ .matches (this ::isDontFollowStrategy );
177
185
}
178
186
179
187
@ Test
@@ -196,17 +204,36 @@ void withRequestFactorySettingsUpdateRedirectsForJdk() {
196
204
.followRedirects ()).isEqualTo (Redirect .NEVER );
197
205
}
198
206
199
- private RequestConfig getRequestConfig (RestTemplateBuilder builder , HttpClientOption ... httpClientOptions ) {
207
+ private RequestConfig getRequestConfig (TestRestTemplate template ) {
208
+ ClientHttpRequestFactory requestFactory = template .getRestTemplate ().getRequestFactory ();
209
+ return (RequestConfig ) Extractors .byName ("httpClient.defaultConfig" ).apply (requestFactory );
210
+ }
211
+
212
+ private RedirectStrategy getRedirectStrategy (RestTemplateBuilder builder , HttpClientOption ... httpClientOptions ) {
200
213
builder = (builder != null ) ? builder : new RestTemplateBuilder ();
201
214
TestRestTemplate template = new TestRestTemplate (builder , null , null , httpClientOptions );
202
- return getRequestConfig (template );
215
+ return getRedirectStrategy (template );
203
216
}
204
217
205
- private RequestConfig getRequestConfig (TestRestTemplate template ) {
206
- CustomHttpComponentsClientHttpRequestFactory factory = (CustomHttpComponentsClientHttpRequestFactory ) template
207
- .getRestTemplate ()
208
- .getRequestFactory ();
209
- return factory .createRequestConfig ();
218
+ private RedirectStrategy getRedirectStrategy (TestRestTemplate template ) {
219
+ ClientHttpRequestFactory requestFactory = template .getRestTemplate ().getRequestFactory ();
220
+ Object chain = Extractors .byName ("httpClient.execChain" ).apply (requestFactory );
221
+ while (chain != null ) {
222
+ Object handler = Extractors .byName ("handler" ).apply (chain );
223
+ if (handler instanceof RedirectExec ) {
224
+ return (RedirectStrategy ) Extractors .byName ("redirectStrategy" ).apply (handler );
225
+ }
226
+ chain = Extractors .byName ("next" ).apply (chain );
227
+ }
228
+ return null ;
229
+ }
230
+
231
+ private boolean isFollowStrategy (RedirectStrategy redirectStrategy ) {
232
+ return redirectStrategy instanceof DefaultRedirectStrategy ;
233
+ }
234
+
235
+ private boolean isDontFollowStrategy (RedirectStrategy redirectStrategy ) {
236
+ return redirectStrategy .getClass ().getName ().contains ("NoFollow" );
210
237
}
211
238
212
239
private HttpClient getJdkHttpClient (TestRestTemplate template ) {
0 commit comments