From 7876bab5e1a5652b6e35d3f98ec2dae669690de0 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Mon, 11 Nov 2024 21:19:47 +0200 Subject: [PATCH] feat: Support overriding default interceptor options when creating OkHttp clients with authentication enabled --- .../kiota/http/KiotaClientFactory.java | 16 +++++- .../kiota/http/KiotaClientFactoryTest.java | 54 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java index 992bb0ca6..6976836a5 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java @@ -87,8 +87,20 @@ private KiotaClientFactory() {} */ @Nonnull public static OkHttpClient.Builder create( @Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) { + return create(authenticationProvider, new RequestOption[0]); + } + + /** + * Creates an OkHttpClient Builder with the Authorization handler and optional custom default middleware configurations + * @param authenticationProvider authentication provider to use for the AuthorizationHandler. + * @param requestOptions The request options to use for the interceptors. + * @return an OkHttpClient Builder instance. + */ + @Nonnull public static OkHttpClient.Builder create( + @Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, + @Nonnull final RequestOption[] requestOptions) { ArrayList interceptors = - new ArrayList<>(Arrays.asList(createDefaultInterceptors())); + new ArrayList<>(Arrays.asList(createDefaultInterceptors(requestOptions))); interceptors.add(new AuthorizationHandler(authenticationProvider)); return create(interceptors); } @@ -173,7 +185,7 @@ private KiotaClientFactory() {} * @deprecated Use {@link #createDefaultInterceptors()} instead. */ @Deprecated - @Nonnull public static List createDefaultInterceptorsAsList() { + @Nonnull private static List createDefaultInterceptorsAsList() { return new ArrayList<>(Arrays.asList(createDefaultInterceptors())); } } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java index 7dd3eda0f..a6c300549 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java @@ -1,8 +1,12 @@ package com.microsoft.kiota.http; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; import com.microsoft.kiota.RequestOption; +import com.microsoft.kiota.authentication.AccessTokenProvider; +import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; +import com.microsoft.kiota.http.middleware.AuthorizationHandler; import com.microsoft.kiota.http.middleware.ChaosHandler; import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; @@ -99,6 +103,56 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { } } + @Test + void testCreateWithAuthProviderAndRequestOptions() throws IOException { + RetryHandlerOption retryHandlerOption = + new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0); + UrlReplaceHandlerOption urlReplaceHandlerOption = + new UrlReplaceHandlerOption(new HashMap<>(), false); + + final ArrayList options = new ArrayList<>(); + options.add(urlReplaceHandlerOption); + options.add(retryHandlerOption); + + OkHttpClient client = + KiotaClientFactory.create( + new BaseBearerTokenAuthenticationProvider( + mock(AccessTokenProvider.class)), + options.toArray(new RequestOption[0])) + .build(); + List clientInterceptors = client.interceptors(); + assertNotNull(clientInterceptors); + // including the Authorization Handler + assertEquals(7, clientInterceptors.size()); + for (Interceptor interceptor : clientInterceptors) { + if (interceptor instanceof RetryHandler) { + RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions(); + assertEquals(0, handlerOption.delay()); + assertEquals(0, handlerOption.maxRetries()); + } + + if (interceptor instanceof UrlReplaceHandler) { + UrlReplaceHandlerOption handlerOption = + ((UrlReplaceHandler) interceptor).getUrlReplaceHandlerOption(); + assertTrue(handlerOption.getReplacementPairs().isEmpty()); + assertFalse(handlerOption.isEnabled()); + } + + assertTrue( + interceptor instanceof UrlReplaceHandler + || interceptor instanceof RedirectHandler + || interceptor instanceof RetryHandler + || interceptor instanceof ParametersNameDecodingHandler + || interceptor instanceof UserAgentHandler + || interceptor instanceof HeadersInspectionHandler + || interceptor instanceof ChaosHandler + || interceptor instanceof AuthorizationHandler, + "Array should contain instances of" + + " UrlReplaceHandler,RedirectHandler,RetryHandler,ParametersNameDecodingHandler,UserAgentHandler," + + " HeadersInspectionHandler, and ChaosHandler"); + } + } + private static RetryHandler getDisabledRetryHandler() { RetryHandlerOption retryHandlerOption = new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0);