Skip to content

Commit 97b20e9

Browse files
committed
Add additional tests for redirects with different HTTP methods
Closes gh-42879
1 parent 85b1c55 commit 97b20e9

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/http/client/AbstractClientHttpRequestFactoryBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ protected AbstractClientHttpRequestFactoryBuilder(List<Consumer<T>> customizers)
4141
this.customizers = (customizers != null) ? customizers : Collections.emptyList();
4242
}
4343

44+
protected final List<Consumer<T>> getCustomizers() {
45+
return this.customizers;
46+
}
47+
4448
protected final List<Consumer<T>> mergedCustomizers(Consumer<T> customizer) {
4549
Assert.notNull(this.customizers, "'customizer' must not be null");
4650
return merge(this.customizers, List.of(customizer));

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/http/client/AbstractClientHttpRequestFactoryBuilderTests.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URISyntaxException;
2222
import java.nio.charset.StandardCharsets;
2323
import java.time.Duration;
24+
import java.util.function.Function;
2425

2526
import javax.net.ssl.SSLHandshakeException;
2627

@@ -62,6 +63,8 @@
6263
@DirtiesUrlFactories
6364
abstract class AbstractClientHttpRequestFactoryBuilderTests<T extends ClientHttpRequestFactory> {
6465

66+
private static final Function<HttpMethod, HttpStatus> ALWAYS_FOUND = (method) -> HttpStatus.FOUND;
67+
6568
private final Class<T> requestFactoryType;
6669

6770
private final ClientHttpRequestFactoryBuilder<T> builder;
@@ -120,24 +123,31 @@ void connectWithSslBundle(String httpMethod) throws Exception {
120123
}
121124
}
122125

123-
@Test
124-
void redirectDefault() throws Exception {
125-
testRedirect(null, HttpStatus.OK);
126+
@ParameterizedTest
127+
@ValueSource(strings = { "GET", "POST", "PUT", "PATCH", "DELETE" })
128+
void redirectDefault(String httpMethod) throws Exception {
129+
testRedirect(null, HttpMethod.valueOf(httpMethod), this::getExpectedRedirect);
126130
}
127131

128-
@Test
129-
void redirectFollow() throws Exception {
130-
testRedirect(ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.FOLLOW), HttpStatus.OK);
132+
@ParameterizedTest
133+
@ValueSource(strings = { "GET", "POST", "PUT", "PATCH", "DELETE" })
134+
void redirectFollow(String httpMethod) throws Exception {
135+
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
136+
.withRedirects(Redirects.FOLLOW);
137+
testRedirect(settings, HttpMethod.valueOf(httpMethod), this::getExpectedRedirect);
131138
}
132139

133-
@Test
134-
void redirectDontFollow() throws Exception {
135-
testRedirect(ClientHttpRequestFactorySettings.defaults().withRedirects(Redirects.DONT_FOLLOW),
136-
HttpStatus.FOUND);
140+
@ParameterizedTest
141+
@ValueSource(strings = { "GET", "POST", "PUT", "PATCH", "DELETE" })
142+
void redirectDontFollow(String httpMethod) throws Exception {
143+
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
144+
.withRedirects(Redirects.DONT_FOLLOW);
145+
testRedirect(settings, HttpMethod.valueOf(httpMethod), ALWAYS_FOUND);
137146
}
138147

139-
private void testRedirect(ClientHttpRequestFactorySettings settings, HttpStatus expectedStatus)
140-
throws URISyntaxException, IOException {
148+
protected final void testRedirect(ClientHttpRequestFactorySettings settings, HttpMethod httpMethod,
149+
Function<HttpMethod, HttpStatus> expectedStatusForMethod) throws URISyntaxException, IOException {
150+
HttpStatus expectedStatus = expectedStatusForMethod.apply(httpMethod);
141151
TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0);
142152
WebServer webServer = webServerFactory
143153
.getWebServer((context) -> context.addServlet("test", TestServlet.class).addMapping("/"));
@@ -146,12 +156,11 @@ private void testRedirect(ClientHttpRequestFactorySettings settings, HttpStatus
146156
int port = webServer.getPort();
147157
URI uri = new URI("http://localhost:%s".formatted(port) + "/redirect");
148158
ClientHttpRequestFactory requestFactory = this.builder.build(settings);
149-
ClientHttpRequest request = requestFactory.createRequest(uri, HttpMethod.GET);
159+
ClientHttpRequest request = requestFactory.createRequest(uri, httpMethod);
150160
ClientHttpResponse response = request.execute();
151161
assertThat(response.getStatusCode()).isEqualTo(expectedStatus);
152162
if (expectedStatus == HttpStatus.OK) {
153-
assertThat(response.getBody()).asString(StandardCharsets.UTF_8)
154-
.contains("Received GET request to /redirected");
163+
assertThat(response.getBody()).asString(StandardCharsets.UTF_8).contains("request to /redirected");
155164
}
156165
}
157166
finally {
@@ -178,6 +187,10 @@ protected final SslBundle sslBundle() {
178187
return SslBundle.of(stores, SslBundleKey.of("password"));
179188
}
180189

190+
protected HttpStatus getExpectedRedirect(HttpMethod httpMethod) {
191+
return HttpStatus.OK;
192+
}
193+
181194
protected abstract long connectTimeout(T requestFactory);
182195

183196
protected abstract long readTimeout(T requestFactory);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/http/client/ReflectiveComponentsClientHttpRequestFactoryBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ void connectWithSslBundle(String httpMethod) throws Exception {
5454
}
5555

5656
@Override
57-
void redirectFollow() throws Exception {
57+
void redirectFollow(String httpMethod) throws Exception {
5858
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
5959
.withRedirects(Redirects.FOLLOW);
6060
assertThatIllegalStateException().isThrownBy(() -> ofTestRequestFactory().build(settings))
6161
.withMessage("Unable to set redirect follow using reflection");
6262
}
6363

6464
@Override
65-
void redirectDontFollow() throws Exception {
65+
void redirectDontFollow(String httpMethod) throws Exception {
6666
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
6767
.withRedirects(Redirects.DONT_FOLLOW);
6868
assertThatIllegalStateException().isThrownBy(() -> ofTestRequestFactory().build(settings))

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/http/client/SimpleClientHttpRequestFactoryBuilderTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package org.springframework.boot.http.client;
1818

19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
21+
22+
import org.springframework.http.HttpMethod;
23+
import org.springframework.http.HttpStatus;
1924
import org.springframework.http.client.SimpleClientHttpRequestFactory;
2025
import org.springframework.test.util.ReflectionTestUtils;
2126

@@ -42,4 +47,30 @@ protected long readTimeout(SimpleClientHttpRequestFactory requestFactory) {
4247
return (int) ReflectionTestUtils.getField(requestFactory, "readTimeout");
4348
}
4449

50+
@ParameterizedTest
51+
@ValueSource(strings = { "GET", "POST", "PUT", "DELETE" })
52+
@Override
53+
void redirectDefault(String httpMethod) throws Exception {
54+
super.redirectDefault(httpMethod);
55+
}
56+
57+
@ParameterizedTest
58+
@ValueSource(strings = { "GET", "POST", "PUT", "DELETE" })
59+
@Override
60+
void redirectFollow(String httpMethod) throws Exception {
61+
super.redirectFollow(httpMethod);
62+
}
63+
64+
@ParameterizedTest
65+
@ValueSource(strings = { "GET", "POST", "PUT", "DELETE" })
66+
@Override
67+
void redirectDontFollow(String httpMethod) throws Exception {
68+
super.redirectDontFollow(httpMethod);
69+
}
70+
71+
@Override
72+
protected HttpStatus getExpectedRedirect(HttpMethod httpMethod) {
73+
return (httpMethod != HttpMethod.GET) ? HttpStatus.FOUND : HttpStatus.OK;
74+
}
75+
4576
}

0 commit comments

Comments
 (0)