|
1 | 1 | package com.microsoft.kiota.http; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.mock; |
4 | 5 |
|
5 | 6 | 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; |
6 | 10 | import com.microsoft.kiota.http.middleware.ChaosHandler; |
7 | 11 | import com.microsoft.kiota.http.middleware.HeadersInspectionHandler; |
8 | 12 | import com.microsoft.kiota.http.middleware.ParametersNameDecodingHandler; |
@@ -99,6 +103,56 @@ void testDefaultInterceptorsWhenRequestOptionsPassedIn() throws IOException { |
99 | 103 | } |
100 | 104 | } |
101 | 105 |
|
| 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 | + |
102 | 156 | private static RetryHandler getDisabledRetryHandler() { |
103 | 157 | RetryHandlerOption retryHandlerOption = |
104 | 158 | new RetryHandlerOption((delay, executionCount, request, response) -> false, 0, 0); |
|
0 commit comments