Skip to content

Commit 224556c

Browse files
committed
Force followRedirects on the OkHttpClient when needed
1 parent cca3887 commit 224556c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public feign.Response execute(feign.Request input, feign.Request.Options options
154154
throws IOException {
155155
okhttp3.OkHttpClient requestScoped;
156156
if (delegate.connectTimeoutMillis() != options.connectTimeoutMillis()
157-
|| delegate.readTimeoutMillis() != options.readTimeoutMillis()) {
157+
|| delegate.readTimeoutMillis() != options.readTimeoutMillis()
158+
|| delegate.followRedirects() != options.isFollowRedirects()) {
158159
requestScoped = delegate.newBuilder()
159160
.connectTimeout(options.connectTimeoutMillis(), TimeUnit.MILLISECONDS)
160161
.readTimeout(options.readTimeoutMillis(), TimeUnit.MILLISECONDS)

okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import feign.client.AbstractClientTest;
2424
import feign.Feign;
2525
import java.util.Collections;
26+
import java.util.concurrent.TimeUnit;
2627
import okhttp3.mockwebserver.MockResponse;
2728
import org.assertj.core.data.MapEntry;
2829
import org.junit.Test;
@@ -60,9 +61,15 @@ public void testContentTypeWithoutCharset() throws Exception {
6061
public void testNoFollowRedirect() throws Exception {
6162
server.enqueue(
6263
new MockResponse().setResponseCode(302).addHeader("Location", server.url("redirect")));
64+
// Enqueue a response to fail fast if the redirect is followed, instead of waiting for the
65+
// timeout
66+
server.enqueue(new MockResponse().setBody("Hello"));
6367

6468
OkHttpClientTestInterface api = newBuilder()
65-
.options(new Request.Options(1000, 1000, false))
69+
// Use the same connect and read timeouts as the OkHttp default
70+
.options(
71+
new Request.Options(10_000, TimeUnit.MILLISECONDS, 10_000, TimeUnit.MILLISECONDS,
72+
false))
6673
.target(OkHttpClientTestInterface.class, "http://localhost:" + server.getPort());
6774

6875
Response response = api.get();
@@ -83,7 +90,9 @@ public void testFollowRedirect() throws Exception {
8390
server.enqueue(new MockResponse().setBody(expectedBody));
8491

8592
OkHttpClientTestInterface api = newBuilder()
86-
.options(new Request.Options(1000, 1000, true))
93+
// Use the same connect and read timeouts as the OkHttp default
94+
.options(
95+
new Request.Options(10_000, TimeUnit.MILLISECONDS, 10_000, TimeUnit.MILLISECONDS, true))
8796
.target(OkHttpClientTestInterface.class, "http://localhost:" + server.getPort());
8897

8998
Response response = api.get();

0 commit comments

Comments
 (0)