|
| 1 | +package com.microsoft.graph.core.requests; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyMap; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URI; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import com.microsoft.kiota.authentication.AccessTokenProvider; |
| 17 | +import com.microsoft.kiota.authentication.AllowedHostsValidator; |
| 18 | +import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider; |
| 19 | + |
| 20 | +import okhttp3.OkHttpClient; |
| 21 | +import okhttp3.Request; |
| 22 | +import okhttp3.Response; |
| 23 | + |
| 24 | +class GraphClientFactoryTest { |
| 25 | + |
| 26 | + private static final String ACCESS_TOKEN_STRING = "token"; |
| 27 | + |
| 28 | + @Test |
| 29 | + void testCreateWithAuthenticationProvider() throws IOException { |
| 30 | + final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = |
| 31 | + getMockAuthenticationProvider(); |
| 32 | + OkHttpClient graphClient = GraphClientFactory.create(mockAuthenticationProvider).addInterceptor(new MockResponseHandler()).build(); |
| 33 | + |
| 34 | + Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build(); |
| 35 | + Response response = graphClient.newCall(request).execute(); |
| 36 | + |
| 37 | + assertEquals(200, response.code()); |
| 38 | + assertNotNull(response.request()); |
| 39 | + assertTrue(response.request().headers().names().contains("Authorization")); |
| 40 | + assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); |
| 41 | + } |
| 42 | + |
| 43 | + private static BaseBearerTokenAuthenticationProvider getMockAuthenticationProvider() { |
| 44 | + final AccessTokenProvider mockAccessTokenProvider = mock(AccessTokenProvider.class); |
| 45 | + final AllowedHostsValidator allowedHostsValidator = |
| 46 | + new AllowedHostsValidator("graph.microsoft.com"); |
| 47 | + when(mockAccessTokenProvider.getAllowedHostsValidator()).thenReturn(allowedHostsValidator); |
| 48 | + when(mockAccessTokenProvider.getAuthorizationToken(any(URI.class), anyMap())) |
| 49 | + .thenReturn(ACCESS_TOKEN_STRING); |
| 50 | + final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = |
| 51 | + mock(BaseBearerTokenAuthenticationProvider.class); |
| 52 | + when(mockAuthenticationProvider.getAccessTokenProvider()) |
| 53 | + .thenReturn(mockAccessTokenProvider); |
| 54 | + return mockAuthenticationProvider; |
| 55 | + } |
| 56 | +} |
0 commit comments