Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
4.5
-
None
-
Local
Description
Trying to simulate redirect on HTPP POST with some body. When server return 302 status code, redirect changes from POST to GET and looses body but does not throw any exception. However, when server returns 307 status code, redirect trows org.apache.http.client.ClientProtocolException.
Here is the junit testcase:
package com.foo.http.examples; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.bootstrap.HttpServer; import org.apache.http.impl.bootstrap.ServerBootstrap; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.message.BasicNameValuePair; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class ApacheHttpClientTest { private static HttpServer localServer; private static CloseableHttpClient httpClient; private static RequestHandlerToTestRedirect serverRequestHandler; @BeforeClass public static void setUp() throws Exception { serverRequestHandler = new RequestHandlerToTestRedirect(); localServer = ServerBootstrap .bootstrap().setServerInfo("TEST/1.1").setListenerPort(5555) .registerHandler("*", serverRequestHandler).create(); localServer.start(); RequestConfig defaultRequestConfig = RequestConfig .custom().setMaxRedirects(2).build(); httpClient = HttpClients .custom().setRedirectStrategy(new LaxRedirectStrategy()) .setDefaultRequestConfig(defaultRequestConfig).build(); } @AfterClass public static void shutDown() throws Exception { if (localServer != null) { localServer.shutdown(1, TimeUnit.SECONDS); } if (httpClient != null) { httpClient.close(); } } @Test public void redirect302_success() throws IOException { serverRequestHandler.setStatusCode(302); HttpPost post = new HttpPost("http://localhost:5555/"); List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); nvps.add(new BasicNameValuePair("param", "value")); post.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(post); assertEquals(200, response.getStatusLine().getStatusCode()); response.close(); } @Test public void redirect307_failure() throws IOException { serverRequestHandler.setStatusCode(307); HttpPost post = new HttpPost("http://localhost:5555/"); List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); nvps.add(new BasicNameValuePair("param", "value")); post.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(post); assertEquals(200, response.getStatusLine().getStatusCode()); response.close(); } }
package com.foo.http.examples; import java.io.IOException; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; public class RequestHandlerToTestRedirect implements HttpRequestHandler { private static int counter = 1; private int statusCode = 200; public void setStatusCode(int code) { statusCode = code; counter = 1; } @Override public void handle( HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode(statusCode); if(counter > 2) { response.setStatusCode(200); } response.setHeader("location", "/" + counter++); } }