-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds the ability to pass options to default interceptors
* Fix default handler overide , when user passed in interceptor should overide any default implementations * fix code based on comments * Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret <vincentbiret@hotmail.com> * Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret <vincentbiret@hotmail.com> * Update components/http/okHttp/src/main/java/com/microsoft/kiota/http/KiotaClientFactory.java Co-authored-by: Vincent Biret <vincentbiret@hotmail.com> * fix comments * chore: linting * fix: makes options non nullable Signed-off-by: Vincent Biret <vibiret@microsoft.com> * chore: linting Signed-off-by: Vincent Biret <vibiret@microsoft.com> * chore: linting Signed-off-by: Vincent Biret <vibiret@microsoft.com> * fix: adds missing doc comment fix: deprecates extraneous method Signed-off-by: Vincent Biret <vibiret@microsoft.com> * fix: misaliagments between methods API surface * feat: adds create method for options Signed-off-by: Vincent Biret <vibiret@microsoft.com> * fix: aligns API surface parameter types Signed-off-by: Vincent Biret <vibiret@microsoft.com> * fix; interceptors order Signed-off-by: Vincent Biret <vibiret@microsoft.com> --------- Signed-off-by: Vincent Biret <vibiret@microsoft.com> Co-authored-by: Raghu Sammeta <rsammeta@proofpoint.com> Co-authored-by: Vincent Biret <vincentbiret@hotmail.com> Co-authored-by: Vincent Biret <vibiret@microsoft.com>
- Loading branch information
1 parent
731286c
commit d1c97c1
Showing
2 changed files
with
204 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
components/http/okHttp/src/test/java/com/microsoft/kiota/http/KiotaClientFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.microsoft.kiota.http; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.microsoft.kiota.RequestOption; | ||
import com.microsoft.kiota.http.middleware.ChaosHandler; | ||
import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; | ||
import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; | ||
import com.microsoft.kiota.http.middleware.RedirectHandler; | ||
import com.microsoft.kiota.http.middleware.RetryHandler; | ||
import com.microsoft.kiota.http.middleware.UrlReplaceHandler; | ||
import com.microsoft.kiota.http.middleware.UserAgentHandler; | ||
import com.microsoft.kiota.http.middleware.options.RetryHandlerOption; | ||
import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class KiotaClientFactoryTest { | ||
|
||
@Test | ||
void testCreatesDefaultInterceptors() throws IOException { | ||
OkHttpClient client = KiotaClientFactory.create().build(); | ||
assertNotNull(client.interceptors()); | ||
assertEquals(6, client.interceptors().size()); | ||
} | ||
|
||
@Test | ||
void testDefaultInterceptorsWhenPassedIn() throws IOException { | ||
OkHttpClient client = | ||
KiotaClientFactory.create( | ||
new Interceptor[] {getDisabledRetryHandler(), new ChaosHandler()}) | ||
.build(); | ||
List<Interceptor> interceptors = client.interceptors(); | ||
assertNotNull(interceptors); | ||
assertEquals(2, interceptors.size()); | ||
for (Interceptor interceptor : interceptors) { | ||
if (interceptor instanceof RetryHandler) { | ||
RetryHandlerOption handlerOption = ((RetryHandler) interceptor).getRetryOptions(); | ||
assertEquals(0, handlerOption.delay()); | ||
assertEquals(0, handlerOption.maxRetries()); | ||
} | ||
|
||
assertTrue( | ||
interceptor instanceof RetryHandler || interceptor instanceof ChaosHandler, | ||
"Array should contain instances of RetryHandler and ChaosHandler"); | ||
} | ||
} | ||
|
||
@Test | ||
void testDefaultInterceptorsWhenRequestOptionsPassedIn() 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); | ||
|
||
Interceptor[] interceptors = | ||
KiotaClientFactory.createDefaultInterceptors(options.toArray(new RequestOption[0])); | ||
OkHttpClient client = KiotaClientFactory.create(interceptors).build(); | ||
List<Interceptor> clientInterceptors = client.interceptors(); | ||
assertNotNull(interceptors); | ||
assertEquals(6, 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, | ||
"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); | ||
RetryHandler retryHandler = new RetryHandler(retryHandlerOption); | ||
return retryHandler; | ||
} | ||
} |