@@ -81,29 +81,24 @@ public void UsesLongestConstructor()
81
81
Assert . AreSame ( clientOptions , client . Options ) ;
82
82
}
83
83
84
- [ Test ]
85
- public void ThrowsWhenUnableToReadCompositeObject ( )
86
- {
87
- IConfiguration configuration = GetConfiguration (
88
- new KeyValuePair < string , string > ( "composite:non-existent" , "_" ) ) ;
89
-
90
- var clientOptions = new TestClientOptions ( ) ;
91
- var exception = Assert . Throws < InvalidOperationException > ( ( ) => ClientFactory . CreateClient ( typeof ( TestClient ) , typeof ( TestClientOptions ) , clientOptions , configuration , null ) ) ;
92
- Assert . AreEqual ( "Unable to convert section 'composite' to parameter type 'Azure.Core.Extensions.Tests.TestClient+CompositeObject', unable to find matching constructor." , exception . Message ) ;
93
- }
94
-
95
84
[ Test ]
96
85
public void ThrowsExceptionWithInformationAboutArguments ( )
97
86
{
98
- IConfiguration configuration = GetConfiguration ( ) ;
87
+ IConfiguration configuration = GetConfiguration (
88
+ new KeyValuePair < string , string > ( "some section:a" , "a" ) ,
89
+ new KeyValuePair < string , string > ( "some section:b:c" , "b" )
90
+ ) . GetSection ( "some section" ) ;
99
91
100
92
var clientOptions = new TestClientOptions ( ) ;
101
- var exception = Assert . Throws < InvalidOperationException > ( ( ) => ClientFactory . CreateClient ( typeof ( TestClient ) , typeof ( TestClientOptions ) , clientOptions , configuration , null ) ) ;
102
- Assert . AreEqual ( "Unable to find matching constructor. Define one of the follow sets of configuration parameters:" + Environment . NewLine +
103
- "1. connectionString" + Environment . NewLine +
104
- "2. uri" + Environment . NewLine +
105
- "3. uri, composite" + Environment . NewLine +
106
- "4. composite" + Environment . NewLine ,
93
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => ClientFactory . CreateClient ( typeof ( TestClientWithCredentials ) , typeof ( TestClientOptions ) , clientOptions , configuration , null ) ) ;
94
+ Assert . AreEqual ( "Unable to find matching constructor while trying to create an instance of TestClientWithCredentials." + Environment . NewLine +
95
+ "Expected one of the follow sets of configuration parameters:" + Environment . NewLine +
96
+ "1. uri" + Environment . NewLine +
97
+ "2. uri, credential:key" + Environment . NewLine +
98
+ "3. uri, credential:signature" + Environment . NewLine +
99
+ "4. uri" + Environment . NewLine +
100
+ "" + Environment . NewLine +
101
+ "Found the following configuration keys: b, b:c, a" ,
107
102
exception . Message ) ;
108
103
}
109
104
@@ -205,6 +200,57 @@ public void IgnoresConstructorWhenCredentialsNull()
205
200
Assert . AreSame ( clientOptions , client . Options ) ;
206
201
}
207
202
203
+ [ Test ]
204
+ public void IgnoresNonTokenCredentialConstructors ( )
205
+ {
206
+ IConfiguration configuration = GetConfiguration (
207
+ new KeyValuePair < string , string > ( "uri" , "http://localhost" ) ,
208
+ new KeyValuePair < string , string > ( "credential" , "managedidentity" ) ,
209
+ new KeyValuePair < string , string > ( "clientId" , "secret" )
210
+ ) ;
211
+
212
+ var clientOptions = new TestClientOptions ( ) ;
213
+ var client = ( TestClientWithCredentials ) ClientFactory . CreateClient ( typeof ( TestClientWithCredentials ) , typeof ( TestClientOptions ) , clientOptions , configuration , new DefaultAzureCredential ( ) ) ;
214
+
215
+ Assert . AreEqual ( "http://localhost/" , client . Uri . ToString ( ) ) ;
216
+ Assert . AreSame ( clientOptions , client . Options ) ;
217
+ Assert . NotNull ( client . Credential ) ;
218
+ }
219
+
220
+ [ Test ]
221
+ public void CanUseAzureKeyCredential ( )
222
+ {
223
+ IConfiguration configuration = GetConfiguration (
224
+ new KeyValuePair < string , string > ( "uri" , "http://localhost" ) ,
225
+ new KeyValuePair < string , string > ( "credential:key" , "key" ) ,
226
+ new KeyValuePair < string , string > ( "clientId" , "secret" )
227
+ ) ;
228
+
229
+ var clientOptions = new TestClientOptions ( ) ;
230
+ var client = ( TestClientWithCredentials ) ClientFactory . CreateClient ( typeof ( TestClientWithCredentials ) , typeof ( TestClientOptions ) , clientOptions , configuration , new DefaultAzureCredential ( ) ) ;
231
+
232
+ Assert . AreEqual ( "http://localhost/" , client . Uri . ToString ( ) ) ;
233
+ Assert . AreSame ( clientOptions , client . Options ) ;
234
+ Assert . AreEqual ( "key" , client . AzureKeyCredential . Key ) ;
235
+ }
236
+
237
+ [ Test ]
238
+ public void CanUseAzureSasCredential ( )
239
+ {
240
+ IConfiguration configuration = GetConfiguration (
241
+ new KeyValuePair < string , string > ( "uri" , "http://localhost" ) ,
242
+ new KeyValuePair < string , string > ( "credential:signature" , "key" ) ,
243
+ new KeyValuePair < string , string > ( "clientId" , "secret" )
244
+ ) ;
245
+
246
+ var clientOptions = new TestClientOptions ( ) ;
247
+ var client = ( TestClientWithCredentials ) ClientFactory . CreateClient ( typeof ( TestClientWithCredentials ) , typeof ( TestClientOptions ) , clientOptions , configuration , new DefaultAzureCredential ( ) ) ;
248
+
249
+ Assert . AreEqual ( "http://localhost/" , client . Uri . ToString ( ) ) ;
250
+ Assert . AreSame ( clientOptions , client . Options ) ;
251
+ Assert . AreEqual ( "key" , client . AzureSasCredential . Signature ) ;
252
+ }
253
+
208
254
private IConfiguration GetConfiguration ( params KeyValuePair < string , string > [ ] items )
209
255
{
210
256
return new ConfigurationBuilder ( ) . AddInMemoryCollection ( items ) . Build ( ) ;
0 commit comments