2121import java .net .URISyntaxException ;
2222import java .nio .charset .StandardCharsets ;
2323import java .time .Duration ;
24+ import java .util .function .Function ;
2425
2526import javax .net .ssl .SSLHandshakeException ;
2627
6263@ DirtiesUrlFactories
6364abstract 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 );
0 commit comments