From 253d4f56880391d5e6a9d2b782150c545e219487 Mon Sep 17 00:00:00 2001 From: Raghu Sammeta Date: Mon, 30 Sep 2024 19:34:23 -0400 Subject: [PATCH] fix issue where user specified interceptors where being overridden by default interceptors --- .../core/requests/GraphClientFactory.java | 4 +- .../middleware/GraphTelemetryHandlerTest.java | 40 +++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java index baa38bd7..ae542852 100644 --- a/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java +++ b/src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java @@ -75,13 +75,13 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication */ @Nonnull public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) { - final OkHttpClient.Builder builder = create(graphClientOption); + final OkHttpClient.Builder builder = KiotaClientFactory.create(interceptors); //Skip adding interceptor if that class of interceptor already exist. final List appliedInterceptors = new ArrayList<>(); for(Interceptor interceptor: builder.interceptors()) { appliedInterceptors.add(interceptor.getClass().toString()); } - for (Interceptor interceptor:interceptors){ + for (Interceptor interceptor:createDefaultGraphInterceptors(graphClientOption)){ if(appliedInterceptors.contains(interceptor.getClass().toString())) { continue; } diff --git a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java index b03f4061..23b0e6fc 100644 --- a/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/core/requests/middleware/GraphTelemetryHandlerTest.java @@ -6,11 +6,15 @@ import com.microsoft.kiota.http.middleware.RedirectHandler; import com.microsoft.kiota.http.middleware.RetryHandler; +import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -30,22 +34,35 @@ void telemetryHandlerDefaultTests() throws IOException { assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - 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 - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + 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 + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test void arrayInterceptorsTest() throws IOException { final String expectedCore = CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + CoreConstants.Headers.VERSION; - final Interceptor[] interceptors = {new GraphTelemetryHandler(), new RetryHandler(), new RedirectHandler()}; + final Interceptor[] interceptors = {new GraphTelemetryHandler(), getDisabledRetryHandler(), + new RedirectHandler()}; final OkHttpClient client = GraphClientFactory.create(interceptors).build(); final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build(); final Response response = client.newCall(request).execute(); + for (Interceptor clientInterceptor : client.interceptors()) { + if (clientInterceptor instanceof RetryHandler) { + RetryHandlerOption retryOptions = ((RetryHandler) clientInterceptor).getRetryOptions(); + Assertions.assertEquals(0, retryOptions.maxRetries()); + Assertions.assertEquals(0, retryOptions.delay()); + + } + } + assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test @@ -59,7 +76,8 @@ void arrayInterceptorEmptyTest() throws IOException { assertNotNull(response); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCore)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(defaultSDKVersion)); } @Test @@ -76,7 +94,7 @@ void testClientOptions() throws IOException { graphClientOption.setGraphServiceTargetVersion(serviceLibVer); final String expectedCoreVer = - CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" +coreLibVer; + CoreConstants.Headers.GRAPH_VERSION_PREFIX + "/" + coreLibVer; final String expectedClientEndpoint = CoreConstants.Headers.JAVA_VERSION_PREFIX + "-" + serviceLibVer + "/" + clientLibVer; @@ -85,9 +103,17 @@ void testClientOptions() throws IOException { final Response response = client.newCall(request).execute(); assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedCoreVer)); - assertTrue(response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint)); + assertTrue( + response.request().header(CoreConstants.Headers.SDK_VERSION_HEADER_NAME).contains(expectedClientEndpoint)); assertTrue(response.request().header(CoreConstants.Headers.CLIENT_REQUEST_ID).contains(requestId)); } + + private static @NotNull RetryHandler getDisabledRetryHandler() { + RetryHandlerOption retryHandlerOption = new RetryHandlerOption( + (delay, executionCount, request, response) -> false, 0, 0); + RetryHandler retryHandler = new RetryHandler(retryHandlerOption); + return retryHandler; + } }