Skip to content

Commit 1ddb88b

Browse files
raghusammetaNdiritu
authored andcommitted
fix: issue where custom interceptors would fail to override default interceptors
1 parent 6b807f7 commit 1ddb88b

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,7 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
7575
*/
7676
@Nonnull
7777
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) {
78-
final OkHttpClient.Builder builder = create(graphClientOption);
79-
//Skip adding interceptor if that class of interceptor already exist.
80-
final List<String> appliedInterceptors = new ArrayList<>();
81-
for(Interceptor interceptor: builder.interceptors()) {
82-
appliedInterceptors.add(interceptor.getClass().toString());
83-
}
84-
for (Interceptor interceptor:interceptors){
85-
if(appliedInterceptors.contains(interceptor.getClass().toString())) {
86-
continue;
87-
}
88-
builder.addInterceptor(interceptor);
89-
}
90-
return builder;
78+
return KiotaClientFactory.create(interceptors);
9179
}
9280

9381
/**

src/test/java/com/microsoft/graph/core/requests/GraphClientFactoryTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
import java.io.IOException;
1212
import java.net.URI;
1313

14+
import org.jetbrains.annotations.NotNull;
15+
import org.junit.jupiter.api.Assertions;
1416
import org.junit.jupiter.api.Test;
1517

18+
import com.microsoft.graph.core.CoreConstants;
1619
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
1720
import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider;
21+
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
1822
import com.microsoft.kiota.authentication.AccessTokenProvider;
1923
import com.microsoft.kiota.authentication.AllowedHostsValidator;
2024
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
25+
import com.microsoft.kiota.http.middleware.RedirectHandler;
26+
import com.microsoft.kiota.http.middleware.RetryHandler;
27+
import com.microsoft.kiota.http.middleware.options.RetryHandlerOption;
2128

29+
import okhttp3.Interceptor;
2230
import okhttp3.OkHttpClient;
2331
import okhttp3.Request;
2432
import okhttp3.Response;
@@ -42,6 +50,29 @@ void testCreateWithAuthenticationProvider() throws IOException {
4250
assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization"));
4351
}
4452

53+
@Test
54+
void testCreateWithCustomInterceptorsOverwritesDefaults() throws IOException {
55+
56+
final Interceptor[] interceptors = {new GraphTelemetryHandler(), getDisabledRetryHandler(),
57+
new RedirectHandler()};
58+
final OkHttpClient client = GraphClientFactory.create(interceptors).build();
59+
final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build();
60+
final Response response = client.newCall(request).execute();
61+
62+
for (Interceptor clientInterceptor : client.interceptors()) {
63+
if (clientInterceptor instanceof RetryHandler) {
64+
RetryHandlerOption retryOptions = ((RetryHandler) clientInterceptor).getRetryOptions();
65+
Assertions.assertEquals(0, retryOptions.maxRetries());
66+
Assertions.assertEquals(0, retryOptions.delay());
67+
68+
}
69+
70+
assertTrue(clientInterceptor instanceof GraphTelemetryHandler
71+
|| clientInterceptor instanceof RedirectHandler
72+
|| clientInterceptor instanceof RetryHandler);
73+
}
74+
}
75+
4576
private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() {
4677
final AccessTokenProvider mockAccessTokenProvider = mock(AzureIdentityAccessTokenProvider.class);
4778
when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap()))
@@ -53,4 +84,11 @@ private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvid
5384
.thenReturn(mockAccessTokenProvider);
5485
return mockAuthenticationProvider;
5586
}
87+
88+
private static @NotNull RetryHandler getDisabledRetryHandler() {
89+
RetryHandlerOption retryHandlerOption = new RetryHandlerOption(
90+
(delay, executionCount, request, response) -> false, 0, 0);
91+
RetryHandler retryHandler = new RetryHandler(retryHandlerOption);
92+
return retryHandler;
93+
}
5694
}

src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import com.microsoft.kiota.http.middleware.RedirectHandler;
77
import com.microsoft.kiota.http.middleware.RetryHandler;
88

9+
import com.microsoft.kiota.http.middleware.options.RetryHandlerOption;
910
import okhttp3.Interceptor;
1011
import okhttp3.OkHttpClient;
1112
import okhttp3.Request;
1213
import okhttp3.Response;
14+
import org.jetbrains.annotations.NotNull;
15+
import org.junit.jupiter.api.Assertions;
1316
import org.junit.jupiter.api.Test;
17+
1418
import static org.junit.jupiter.api.Assertions.assertNotNull;
1519
import static org.junit.jupiter.api.Assertions.assertTrue;
1620

@@ -30,22 +34,26 @@ void telemetryHandlerDefaultTests() throws IOException {
3034

3135
assertNotNull(response);
3236
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore));
33-
assertTrue(!response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(CoreConstants.Headers.ANDROID_VERSION_PREFIX)); // Android version is not going to be present on unit tests running on java platform
34-
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
37+
assertTrue(!response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(
38+
CoreConstants.Headers.ANDROID_VERSION_PREFIX)); // Android version is not going to be present on unit tests running on java platform
39+
assertTrue(
40+
response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
3541
}
3642

3743
@Test
3844
void arrayInterceptorsTest() throws IOException {
3945
final String expectedCore = CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + CoreConstants.Headers.VERSION;
4046

41-
final Interceptor[] interceptors = {new GraphTelemetryHandler(), new RetryHandler(), new RedirectHandler()};
47+
final Interceptor[] interceptors = {new GraphTelemetryHandler(), new RetryHandler(),
48+
new RedirectHandler()};
4249
final OkHttpClient client = GraphClientFactory.create(interceptors).build();
4350
final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build();
4451
final Response response = client.newCall(request).execute();
4552

4653
assertNotNull(response);
4754
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore));
48-
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
55+
assertTrue(
56+
response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
4957
}
5058

5159
@Test
@@ -59,7 +67,8 @@ void arrayInterceptorEmptyTest() throws IOException {
5967

6068
assertNotNull(response);
6169
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore));
62-
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
70+
assertTrue(
71+
response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion));
6372
}
6473

6574
@Test
@@ -76,7 +85,7 @@ void testClientOptions() throws IOException {
7685
graphClientOption.setGraphServiceTargetVersion(serviceLibVer);
7786

7887
final String expectedCoreVer =
79-
CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" +coreLibVer;
88+
CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + coreLibVer;
8089
final String expectedClientEndpoint =
8190
CoreConstants.Headers.JAVA_VERSION_PREFIX + "-" + serviceLibVer + "/" + clientLibVer;
8291

@@ -85,7 +94,8 @@ void testClientOptions() throws IOException {
8594
final Response response = client.newCall(request).execute();
8695

8796
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCoreVer));
88-
assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint));
97+
assertTrue(
98+
response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint));
8999
assertTrue(response.request().header(CoreConstants.Headers.CLIENT_REQUEST_ID).contains(requestId));
90100
}
91101
}

0 commit comments

Comments
 (0)