Skip to content

Commit

Permalink
fix #3697: closing the initial interceptor response
Browse files Browse the repository at this point in the history
(cherry picked from commit 36b620b)
  • Loading branch information
shawkins authored and manusa committed Jan 5, 2022
1 parent 5d1c50c commit 98365cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 5.12-SNAPSHOT

#### Bugs
* Fix #3697: addresses response that aren't closed by interceptors that issue new requests

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

}

0 comments on commit 98365cf

Please sign in to comment.