diff --git a/CHANGELOG.md b/CHANGELOG.md index 292678b30b0..b6d0e025ed5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 5.12-SNAPSHOT #### Bugs +* Fix #3697: addresses response that aren't closed by interceptors that issue new requests #### Improvements diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientBuilderImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientBuilderImpl.java index 212c670bc50..bc7b5a6d60d 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientBuilderImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientBuilderImpl.java @@ -49,7 +49,7 @@ static final class InteceptorAdapter implements Interceptor { private final io.fabric8.kubernetes.client.http.Interceptor interceptor; private final String name; - private InteceptorAdapter(io.fabric8.kubernetes.client.http.Interceptor interceptor, String name) { + InteceptorAdapter(io.fabric8.kubernetes.client.http.Interceptor interceptor, String name) { this.interceptor = interceptor; this.name = name; } @@ -63,6 +63,7 @@ public Response intercept(Chain chain) throws IOException { if (!response.isSuccessful()) { boolean call = interceptor.afterFailure(builderImpl, new OkHttpResponseImpl<>(response, InputStream.class)); if (call) { + response.close(); return chain.proceed(requestBuilder.build()); } } diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/internal/okhttp/OkHttpClientFactoryTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactoryTest.java similarity index 61% rename from kubernetes-client/src/test/java/io/fabric8/kubernetes/client/internal/okhttp/OkHttpClientFactoryTest.java rename to kubernetes-client/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactoryTest.java index b4378b7ed4a..e1024009e38 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/internal/okhttp/OkHttpClientFactoryTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactoryTest.java @@ -14,13 +14,21 @@ * limitations under the License. */ -package io.fabric8.kubernetes.client.internal.okhttp; +package io.fabric8.kubernetes.client.okhttp; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.ConfigBuilder; -import io.fabric8.kubernetes.client.okhttp.OkHttpClientFactory; -import io.fabric8.kubernetes.client.okhttp.OkHttpClientImpl; +import io.fabric8.kubernetes.client.http.BasicBuilder; +import io.fabric8.kubernetes.client.http.HttpResponse; +import io.fabric8.kubernetes.client.http.Interceptor; +import okhttp3.Request; +import okhttp3.Request.Builder; +import okhttp3.Response; +import okhttp3.Interceptor.Chain; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -55,5 +63,24 @@ void shouldRespectMaxRequestsPerHost() { assertEquals(20, client.getOkHttpClient().dispatcher().getMaxRequestsPerHost()); } - + @Test + void inteceptorClosure() throws IOException { + OkHttpClientBuilderImpl.InteceptorAdapter adapter = new OkHttpClientBuilderImpl.InteceptorAdapter(new Interceptor() { + @Override + public boolean afterFailure(BasicBuilder builder, HttpResponse response) { + return true; + } + }, "name"); + + Chain chain = Mockito.mock(Chain.class); + Mockito.when(chain.request()).thenReturn(Mockito.mock(Request.class)); + Mockito.when(chain.request().newBuilder()).thenReturn(Mockito.mock(Builder.class)); + Response response = Mockito.mock(Response.class); + Mockito.when(chain.proceed(Mockito.any())).thenReturn(response); + + adapter.intercept(chain); + + Mockito.verify(response).close(); + } + }