Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-1859: ApacheHttpClient - follow redirect via Request.Options #1861

Merged
merged 1 commit into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}