Skip to content

Commit

Permalink
Merge pull request #1669 from microsoft/feat/interceptor-overrides
Browse files Browse the repository at this point in the history
Support overriding default interceptor options
  • Loading branch information
Ndiritu authored Nov 14, 2024
2 parents a6c9623 + 1687efa commit 3c2dd73
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Interceptor> interceptors =
new ArrayList<>(Arrays.asList(createDefaultInterceptors()));
new ArrayList<>(Arrays.asList(createDefaultInterceptors(requestOptions)));
interceptors.add(new AuthorizationHandler(authenticationProvider));
return create(interceptors);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<RequestOption> 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<Interceptor> 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);
Expand Down

0 comments on commit 3c2dd73

Please sign in to comment.