Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overriding default interceptor options #1669

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading