Skip to content

Commit

Permalink
Issue-1859: ApacheHttpClient - follow redirect via Request.Options (#…
Browse files Browse the repository at this point in the history
…1861)

Co-authored-by: Olivier Sergeant <olivier.sergeant@boulanger.com>
  • Loading branch information
trumple and Olivier Sergeant committed Nov 27, 2022
1 parent 83b3a52 commit 9ad5af9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options)
: RequestConfig.custom())
.setConnectTimeout(options.connectTimeoutMillis())
.setSocketTimeout(options.readTimeoutMillis())
.setRedirectsEnabled(options.isFollowRedirects())
.build();
requestBuilder.setConfig(requestConfig);

Expand Down
75 changes: 62 additions & 13 deletions httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@
*/
package feign.httpclient;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;
import feign.Feign;
import feign.Feign.Builder;
import feign.FeignException;
import feign.Request.Options;
import feign.client.AbstractClientTest;
import feign.jaxrs.JAXRSContract;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;

/**
* Tests client-specific behavior, such as ensuring Content-Length is sent when specified.
Expand All @@ -40,11 +44,7 @@ public Builder newBuilder() {

@Test
public void queryParamsAreRespectedWhenBodyIsEmpty() throws InterruptedException {
final HttpClient httpClient = HttpClientBuilder.create().build();
final JaxRsTestInterface testInterface = Feign.builder()
.contract(new JAXRSContract())
.client(new ApacheHttpClient(httpClient))
.target(JaxRsTestInterface.class, "http://localhost:" + server.getPort());
final JaxRsTestInterface testInterface = buildTestInterface();

server.enqueue(new MockResponse().setBody("foo"));
server.enqueue(new MockResponse().setBody("foo"));
Expand All @@ -60,6 +60,51 @@ public void queryParamsAreRespectedWhenBodyIsEmpty() throws InterruptedException
assertEquals("", request2.getBody().readString(StandardCharsets.UTF_8));
}

@Test
public void followRedirectIsRespected() throws InterruptedException {
final JaxRsTestInterface testInterface = buildTestInterface();

String redirectPath = "/redirected";
server.enqueue(buildMockResponseWithHeaderLocation(redirectPath));
server.enqueue(new MockResponse().setBody("redirect"));
Options options = buildRequestOptions(true);

assertEquals("redirect", testInterface.withOptions(options));
assertEquals("/withOptions", server.takeRequest().getPath());
assertEquals(redirectPath, server.takeRequest().getPath());
}

@Test
public void notFollowRedirectIsRespected() throws InterruptedException {
final JaxRsTestInterface testInterface = buildTestInterface();

String redirectPath = "/redirected";
server.enqueue(buildMockResponseWithHeaderLocation(redirectPath));
Options options = buildRequestOptions(false);

FeignException feignException =
assertThrows(FeignException.class, () -> testInterface.withOptions(options));
assertEquals(302, feignException.status());
assertEquals("/withOptions", server.takeRequest().getPath());
}

private JaxRsTestInterface buildTestInterface() {
return Feign.builder()
.contract(new JAXRSContract())
.client(new ApacheHttpClient(HttpClientBuilder.create().build()))
.target(JaxRsTestInterface.class, "http://localhost:" + server.getPort());
}

private static Options buildRequestOptions(boolean followRedirects) {
return new Options(1, SECONDS,
1, SECONDS, followRedirects);
}

private MockResponse buildMockResponseWithHeaderLocation(String redirectPath) {
return new MockResponse().setResponseCode(302).addHeader("location",
"http://localhost:" + server.getPort() + redirectPath);
}

@Path("/")
public interface JaxRsTestInterface {
@PUT
Expand All @@ -69,5 +114,9 @@ public interface JaxRsTestInterface {
@PUT
@Path("/withoutBody")
public String withoutBody(@QueryParam("foo") String foo);

@GET
@Path("/withOptions")
public String withOptions(Options options);
}
}

0 comments on commit 9ad5af9

Please sign in to comment.