@@ -34,26 +34,6 @@ public ManagedIdentityCredentialTests(bool isAsync) : base(isAsync)
3434
3535 private const string ExpectedToken = "mock-msi-access-token" ;
3636
37- [ Test ]
38- public async Task VerifyExpiringTokenRefresh ( )
39- {
40- int callCount = 0 ;
41-
42- var mockClient = new MockManagedIdentityClient ( CredentialPipeline . GetInstance ( null ) )
43- {
44- TokenFactory = ( ) => { callCount ++ ; return new AccessToken ( Guid . NewGuid ( ) . ToString ( ) , DateTimeOffset . UtcNow . AddMinutes ( 2 ) ) ; }
45- } ;
46-
47- var cred = InstrumentClient ( new ManagedIdentityCredential ( mockClient ) ) ;
48-
49- for ( int i = 0 ; i < 5 ; i ++ )
50- {
51- await cred . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ;
52- }
53-
54- Assert . AreEqual ( 5 , callCount ) ;
55- }
56-
5737 [ NonParallelizable ]
5838 [ Test ]
5939 public async Task VerifyImdsRequestWithClientIdMockAsync ( )
@@ -84,6 +64,38 @@ public async Task VerifyImdsRequestWithClientIdMockAsync()
8464 Assert . IsTrue ( query . Contains ( $ "{ Constants . ManagedIdentityClientId } =mock-client-id") ) ;
8565 }
8666
67+ [ NonParallelizable ]
68+ [ Test ]
69+ public async Task VerifyImdsSendsProbeOnlyOnFirstRequest ( )
70+ {
71+ using var environment = new TestEnvVar ( new ( ) { { "MSI_ENDPOINT" , null } , { "MSI_SECRET" , null } , { "IDENTITY_ENDPOINT" , null } , { "IDENTITY_HEADER" , null } , { "AZURE_POD_IDENTITY_AUTHORITY_HOST" , null } } ) ;
72+
73+ int probeCount = 0 ;
74+ var mockTransport = new MockTransport ( req =>
75+ {
76+ if ( ! req . Headers . TryGetValue ( "Metadata" , out var _ ) )
77+ {
78+ probeCount ++ ;
79+ return CreateErrorMockResponse ( 400 , "mock error" ) ;
80+ }
81+ else
82+ {
83+ return CreateMockResponse ( 200 , ExpectedToken ) ;
84+ }
85+ } ) ;
86+ var options = new TokenCredentialOptions ( ) { Transport = mockTransport , IsChainedCredential = true } ;
87+ var pipeline = CredentialPipeline . GetInstance ( options ) ;
88+ ManagedIdentityCredential credential = InstrumentClient ( new ManagedIdentityCredential (
89+ new ManagedIdentityClient (
90+ new ManagedIdentityClientOptions ( ) { Pipeline = pipeline , ManagedIdentityId = ManagedIdentityId . FromUserAssignedClientId ( "mock-client-id" ) , IsForceRefreshEnabled = true , Options = options } ) ) ) ;
91+
92+ await credential . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ;
93+ AccessToken actualToken = await credential . GetTokenAsync ( new TokenRequestContext ( MockScopes . Default ) ) ;
94+
95+ Assert . AreEqual ( ExpectedToken , actualToken . Token ) ;
96+ Assert . AreEqual ( 1 , probeCount , "Probe was sent more than once." ) ;
97+ }
98+
8799 [ NonParallelizable ]
88100 [ Test ]
89101 public async Task ImdsWithEmptyClientIdIsIgnoredMockAsync ( )
0 commit comments