21
21
import java .net .URISyntaxException ;
22
22
import java .nio .charset .StandardCharsets ;
23
23
import java .time .Duration ;
24
+ import java .util .function .Function ;
24
25
25
26
import javax .net .ssl .SSLHandshakeException ;
26
27
62
63
@ DirtiesUrlFactories
63
64
abstract class AbstractClientHttpRequestFactoryBuilderTests <T extends ClientHttpRequestFactory > {
64
65
66
+ private static final Function <HttpMethod , HttpStatus > ALWAYS_FOUND = (method ) -> HttpStatus .FOUND ;
67
+
65
68
private final Class <T > requestFactoryType ;
66
69
67
70
private final ClientHttpRequestFactoryBuilder <T > builder ;
@@ -120,24 +123,31 @@ void connectWithSslBundle(String httpMethod) throws Exception {
120
123
}
121
124
}
122
125
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 );
126
130
}
127
131
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 );
131
138
}
132
139
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 );
137
146
}
138
147
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 );
141
151
TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory (0 );
142
152
WebServer webServer = webServerFactory
143
153
.getWebServer ((context ) -> context .addServlet ("test" , TestServlet .class ).addMapping ("/" ));
@@ -146,12 +156,11 @@ private void testRedirect(ClientHttpRequestFactorySettings settings, HttpStatus
146
156
int port = webServer .getPort ();
147
157
URI uri = new URI ("http://localhost:%s" .formatted (port ) + "/redirect" );
148
158
ClientHttpRequestFactory requestFactory = this .builder .build (settings );
149
- ClientHttpRequest request = requestFactory .createRequest (uri , HttpMethod . GET );
159
+ ClientHttpRequest request = requestFactory .createRequest (uri , httpMethod );
150
160
ClientHttpResponse response = request .execute ();
151
161
assertThat (response .getStatusCode ()).isEqualTo (expectedStatus );
152
162
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" );
155
164
}
156
165
}
157
166
finally {
@@ -178,6 +187,10 @@ protected final SslBundle sslBundle() {
178
187
return SslBundle .of (stores , SslBundleKey .of ("password" ));
179
188
}
180
189
190
+ protected HttpStatus getExpectedRedirect (HttpMethod httpMethod ) {
191
+ return HttpStatus .OK ;
192
+ }
193
+
181
194
protected abstract long connectTimeout (T requestFactory );
182
195
183
196
protected abstract long readTimeout (T requestFactory );
0 commit comments