[HTTPCLIENT-2053] Add SC_PERMANENT_REDIRECT (408) to DefaultRedirectStrategy

This closes #215
This commit is contained in:
Michael Osipov 2020-02-22 14:45:13 +01:00
parent 98b9d436f8
commit 55bd1654fc
2 changed files with 37 additions and 1 deletions

View File

@ -74,6 +74,8 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
private final Log log = LogFactory.getLog(getClass());
public static final int SC_PERMANENT_REDIRECT = 308;
/**
* @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
*/
@ -120,6 +122,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
return isRedirectable(method) && locationHeader != null;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
case SC_PERMANENT_REDIRECT:
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
@ -225,7 +228,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
return new HttpGet(uri);
} else {
final int status = response.getStatusLine().getStatusCode();
return status == HttpStatus.SC_TEMPORARY_REDIRECT
return (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == SC_PERMANENT_REDIRECT)
? RequestBuilder.copy(request).setUri(uri).build()
: new HttpGet(uri);
}

View File

@ -112,6 +112,18 @@ public class TestDefaultRedirectStrategy {
Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
}
@Test
public void testIsRedirectedPermanentRedirect() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Redirect");
Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
final HttpPost httppost = new HttpPost("http://localhost/");
Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
}
@Test
public void testIsRedirectedSeeOther() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@ -385,6 +397,27 @@ public class TestDefaultRedirectStrategy {
Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
}
@Test
public void testGetRedirectRequestForPermanentRedirect() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
DefaultRedirectStrategy.SC_PERMANENT_REDIRECT, "Permanent Redirect");
response.addHeader("Location", "http://localhost/stuff");
final HttpContext context1 = new BasicHttpContext();
final HttpUriRequest redirect1 = redirectStrategy.getRedirect(
new HttpTrace("http://localhost/"), response, context1);
Assert.assertEquals("TRACE", redirect1.getMethod());
final HttpContext context2 = new BasicHttpContext();
final HttpPost httppost = new HttpPost("http://localhost/");
final HttpEntity entity = new BasicHttpEntity();
httppost.setEntity(entity);
final HttpUriRequest redirect2 = redirectStrategy.getRedirect(
httppost, response, context2);
Assert.assertEquals("POST", redirect2.getMethod());
Assert.assertTrue(redirect2 instanceof HttpEntityEnclosingRequest);
Assert.assertSame(entity, ((HttpEntityEnclosingRequest) redirect2).getEntity());
}
@Test(expected=ProtocolException.class)
public void testCreateLocationURIInvalid() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();