hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pavel Obraztsov (JIRA)" <j...@apache.org>
Subject [jira] [Created] (HTTPCLIENT-1680) 307 redirect throws ClientProtocolException using POST method
Date Fri, 28 Aug 2015 15:07:45 GMT
Pavel Obraztsov created HTTPCLIENT-1680:
-------------------------------------------

             Summary: 307 redirect throws ClientProtocolException using POST method
                 Key: HTTPCLIENT-1680
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1680
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient
    Affects Versions: 4.5
         Environment: Local
            Reporter: Pavel Obraztsov


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:

{code}
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();
  }
}
{code}

{code}
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++);
    
  }
}
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message