Skip to content

Commit 3c2dd73

Browse files
authored
Merge pull request #1669 from microsoft/feat/interceptor-overrides
Support overriding default interceptor options
2 parents a6c9623 + 1687efa commit 3c2dd73

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,20 @@ private KiotaClientFactory() {}
8787
*/
8888
@Nonnull public static OkHttpClient.Builder create(
8989
@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) {
90+
return create(authenticationProvider, new RequestOption[0]);
91+
}
92+
93+
/**
94+
* Creates an OkHttpClient Builder with the Authorization handler and optional custom default middleware configurations
95+
* @param authenticationProvider authentication provider to use for the AuthorizationHandler.
96+
* @param requestOptions The request options to use for the interceptors.
97+
* @return an OkHttpClient Builder instance.
98+
*/
99+
@Nonnull public static OkHttpClient.Builder create(
100+
@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider,
101+
@Nonnull final RequestOption[] requestOptions) {
90102
ArrayList<Interceptor> interceptors =
91-
new ArrayList<>(Arrays.asList(createDefaultInterceptors()));
103+
new ArrayList<>(Arrays.asList(createDefaultInterceptors(requestOptions)));
92104
interceptors.add(new AuthorizationHandler(authenticationProvider));
93105
return create(interceptors);
94106
}

components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.microsoft.kiota.http;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.mock;
45

56
import com.microsoft.kiota.RequestOption;
7+
import com.microsoft.kiota.authentication.AccessTokenProvider;
8+
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
9+
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
610
import com.microsoft.kiota.http.middleware.ChaosHandler;
711
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler;
812
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler;
@@ -99,6 +103,56 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException {
99103
}
100104
}
101105

106+
@Test
107+
void testCreateWithAuthProviderAndRequestOptions() throws IOException {
108+
RetryHandlerOption retryHandlerOption =
109+
new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0);
110+
UrlReplaceHandlerOption urlReplaceHandlerOption =
111+
new UrlReplaceHandlerOption(new HashMap<>(), false);
112+
113+
final ArrayList<RequestOption> options = new ArrayList<>();
114+
options.add(urlReplaceHandlerOption);
115+
options.add(retryHandlerOption);
116+
117+
OkHttpClient client =
118+
KiotaClientFactory.create(
119+
new BaseBearerTokenAuthenticationProvider(
120+
mock(AccessTokenProvider.class)),
121+
options.toArray(new RequestOption[0]))
122+
.build();
123+
List<Interceptor> clientInterceptors = client.interceptors();
124+
assertNotNull(clientInterceptors);
125+
// including the Authorization Handler
126+
assertEquals(7, clientInterceptors.size());
127+
for (Interceptor interceptor : clientInterceptors) {
128+
if (interceptor instanceof RetryHandler) {
129+
RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions();
130+
assertEquals(0, handlerOption.delay());
131+
assertEquals(0, handlerOption.maxRetries());
132+
}
133+
134+
if (interceptor instanceof UrlReplaceHandler) {
135+
UrlReplaceHandlerOption handlerOption =
136+
((UrlReplaceHandler) interceptor).getUrlReplaceHandlerOption();
137+
assertTrue(handlerOption.getReplacementPairs().isEmpty());
138+
assertFalse(handlerOption.isEnabled());
139+
}
140+
141+
assertTrue(
142+
interceptor instanceof UrlReplaceHandler
143+
|| interceptor instanceof RedirectHandler
144+
|| interceptor instanceof RetryHandler
145+
|| interceptor instanceof ParametersNameDecodingHandler
146+
|| interceptor instanceof UserAgentHandler
147+
|| interceptor instanceof HeadersInspectionHandler
148+
|| interceptor instanceof ChaosHandler
149+
|| interceptor instanceof AuthorizationHandler,
150+
"Array should contain instances of"
151+
+ " UrlReplaceHandler,RedirectHandler,RetryHandler,ParametersNameDecodingHandler,UserAgentHandler,"
152+
+ " HeadersInspectionHandler, and ChaosHandler");
153+
}
154+
}
155+
102156
private static RetryHandler getDisabledRetryHandler() {
103157
RetryHandlerOption retryHandlerOption =
104158
new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0);

0 commit comments

Comments
 (0)