|
19 | 19 | import org.junit.jupiter.api.Assertions; |
20 | 20 | import org.junit.jupiter.api.Test; |
21 | 21 |
|
| 22 | +import com.azure.core.credential.AccessToken; |
| 23 | +import com.azure.core.credential.TokenCredential; |
22 | 24 | import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider; |
23 | 25 | import com.microsoft.graph.core.authentication.AzureIdentityAuthenticationProvider; |
24 | 26 | import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler; |
|
39 | 41 | import okhttp3.OkHttpClient; |
40 | 42 | import okhttp3.Request; |
41 | 43 | import okhttp3.Response; |
| 44 | +import reactor.core.publisher.Mono; |
42 | 45 |
|
43 | 46 | class GraphClientFactoryTest { |
44 | 47 |
|
@@ -104,6 +107,22 @@ void testCreateWithAuthenticationProvider() throws IOException { |
104 | 107 | assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); |
105 | 108 | } |
106 | 109 |
|
| 110 | + @Test |
| 111 | + void testCreateWithTokenCredential() throws IOException { |
| 112 | + final TokenCredential tokenCredential = mock(TokenCredential.class); |
| 113 | + when(tokenCredential.getTokenSync(any())).thenReturn(new AccessToken(ACCESS_TOKEN_STRING, null)); |
| 114 | + when(tokenCredential.getToken(any())).thenReturn(Mono.just(new AccessToken(ACCESS_TOKEN_STRING, null))); |
| 115 | + |
| 116 | + final OkHttpClient graphClient = GraphClientFactory.create(tokenCredential).addInterceptor(new MockResponseHandler()).build(); |
| 117 | + Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build(); |
| 118 | + Response response = graphClient.newCall(request).execute(); |
| 119 | + |
| 120 | + assertEquals(200, response.code()); |
| 121 | + assertNotNull(response.request()); |
| 122 | + assertTrue(response.request().headers().names().contains("Authorization")); |
| 123 | + assertEquals("Bearer " + ACCESS_TOKEN_STRING, response.request().header("Authorization")); |
| 124 | + } |
| 125 | + |
107 | 126 | @Test |
108 | 127 | void testCreateWithAuthenticationProviderAndCustomRequestOptions() throws IOException { |
109 | 128 | final BaseBearerTokenAuthenticationProvider mockAuthenticationProvider = |
|
0 commit comments