public TestClientWithCredentials(Uri uri, TokenCredential credential, TestClientOptions options) : base(uri, options)
 {
     if (credential == null)
     {
         throw new ArgumentNullException(nameof(credential));
     }
     Credential = credential;
 }
Beispiel #2
0
        public void FailsToConvertInvalidGuidConfiguration()
        {
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("guid", "no it its not"));

            var clientOptions = new TestClientOptions();

            Assert.Throws <FormatException>(() => ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null));
        }
Beispiel #3
0
        public void SelectsConstructorBaseOnConfiguration()
        {
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("connectionstring", "CS"));

            var clientOptions = new TestClientOptions();
            var client        = (TestClient)ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual("CS", client.ConnectionString);
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #4
0
        public void IgnoresConstructorWhenCredentialsNull()
        {
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("uri", "http://localhost"));

            var clientOptions = new TestClientOptions();
            var client        = (TestClientWithCredentials)ClientFactory.CreateClient(typeof(TestClientWithCredentials), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual("http://localhost/", client.Uri.ToString());
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #5
0
        public void ConvertsCompositeObjectsConstructorParameters()
        {
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("composite:c", "http://localhost"));

            var clientOptions = new TestClientOptions();
            var client        = (TestClient)ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual("http://localhost/", client.Composite.C.ToString());
            Assert.AreSame(clientOptions, client.Options);
        }
        public TestClient(string connectionString, TestClientOptions options)
        {
            if (connectionString == "throw")
            {
                throw new ArgumentException("Throwing");
            }

            ConnectionString = connectionString;
            Options          = options;
        }
Beispiel #7
0
        public void ThrowsWhenUnableToReadCompositeObject()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("composite:non-existent", "_"));

            var clientOptions = new TestClientOptions();
            var exception     = Assert.Throws <InvalidOperationException>(() => ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null));

            Assert.AreEqual("Unable to convert section 'composite' to parameter type 'Azure.Core.Extensions.Tests.TestClient+CompositeObject', unable to find matching constructor.", exception.Message);
        }
Beispiel #8
0
        public void ConvertsUriConstructorParameters()
        {
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("uri", "http://localhost"));

            var factory       = new ConfigurationClientFactory();
            var clientOptions = new TestClientOptions();
            var client        = (TestClient)factory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration);

            Assert.AreEqual("http://localhost/", client.Uri.ToString());
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #9
0
        public void ConvertsGuidConstructorParameters()
        {
            var            guidValue     = Guid.NewGuid().ToString();
            IConfiguration configuration = GetConfiguration(new KeyValuePair <string, string>("guid", guidValue));

            var clientOptions = new TestClientOptions();
            var client        = (TestClient)ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual(guidValue, client.Guid.ToString());
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #10
0
        public void ThrowsExceptionWithInformationAboutArguments()
        {
            IConfiguration configuration = GetConfiguration();

            var clientOptions = new TestClientOptions();
            var exception     = Assert.Throws <InvalidOperationException>(() => ConfigurationClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration));

            Assert.AreEqual("Unable to find matching constructor. Define one of the follow sets of configuration parameters:" + Environment.NewLine +
                            "1. connectionString" + Environment.NewLine +
                            "2. uri" + Environment.NewLine,
                            exception.Message);
        }
Beispiel #11
0
        public void ConvertsCompositeObjectsConstructorParameters2()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("composite:a", "a"),
                new KeyValuePair <string, string>("composite:b", "b"));

            var clientOptions = new TestClientOptions();
            var client        = (TestClient)ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual("a", client.Composite.A);
            Assert.AreEqual("b", client.Composite.B);
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #12
0
        public void UsesLongestConstructor()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("composite:c", "http://localhost"),
                new KeyValuePair <string, string>("uri", "http://otherhost")
                );

            var clientOptions = new TestClientOptions();
            var client        = (TestClient)ClientFactory.CreateClient(typeof(TestClient), typeof(TestClientOptions), clientOptions, configuration, null);

            Assert.AreEqual("http://localhost/", client.Composite.C.ToString());
            Assert.AreEqual("http://otherhost/", client.Uri.ToString());
            Assert.AreSame(clientOptions, client.Options);
        }
Beispiel #13
0
        public void GlobalOptionsAppliedToAzureComponentFactoryCreateClientOptions()
        {
            var configuration = GetConfiguration();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClients(builder => builder.ConfigureDefaults(clientOptions => clientOptions.Diagnostics.ApplicationId = "AppId"));

            ServiceProvider       provider = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory  = provider.GetService <AzureComponentFactory>();
            TestClientOptions     options  = (TestClientOptions)factory.CreateClientOptions(typeof(TestClientOptions), configuration["TestClient"], null);

            Assert.AreEqual("AppId", options.Diagnostics.ApplicationId);
        }
Beispiel #14
0
        public void IgnoresNonTokenCredentialConstructors()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("uri", "http://localhost"),
                new KeyValuePair <string, string>("credential", "managedidentity"),
                new KeyValuePair <string, string>("clientId", "secret")
                );

            var clientOptions = new TestClientOptions();
            var client        = (TestClientWithCredentials)ClientFactory.CreateClient(typeof(TestClientWithCredentials), typeof(TestClientOptions), clientOptions, configuration, new DefaultAzureCredential());

            Assert.AreEqual("http://localhost/", client.Uri.ToString());
            Assert.AreSame(clientOptions, client.Options);
            Assert.NotNull(client.Credential);
        }
Beispiel #15
0
        public void CanUseAzureSasCredential()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("uri", "http://localhost"),
                new KeyValuePair <string, string>("credential:signature", "key"),
                new KeyValuePair <string, string>("clientId", "secret")
                );

            var clientOptions = new TestClientOptions();
            var client        = (TestClientWithCredentials)ClientFactory.CreateClient(typeof(TestClientWithCredentials), typeof(TestClientOptions), clientOptions, configuration, new DefaultAzureCredential());

            Assert.AreEqual("http://localhost/", client.Uri.ToString());
            Assert.AreSame(clientOptions, client.Options);
            Assert.AreEqual("key", client.AzureSasCredential.Signature);
        }
Beispiel #16
0
        public void CanReadClientOptionsFromConfiguration()
        {
            var configuration = GetConfiguration(
                new KeyValuePair <string, string>("TestClient:Property", "client option value"));

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddAzureClientsCore();

            ServiceProvider       provider = serviceCollection.BuildServiceProvider();
            AzureComponentFactory factory  = provider.GetService <AzureComponentFactory>();
            TestClientOptions     options  = (TestClientOptions)factory.CreateClientOptions(typeof(TestClientOptions), null, configuration.GetSection("TestClient"));

            Assert.AreEqual("client option value", options.Property);
        }
Beispiel #17
0
        public void ThrowsExceptionWithInformationAboutArguments()
        {
            IConfiguration configuration = GetConfiguration(
                new KeyValuePair <string, string>("some section:a", "a"),
                new KeyValuePair <string, string>("some section:b:c", "b")
                ).GetSection("some section");

            var clientOptions = new TestClientOptions();
            var exception     = Assert.Throws <InvalidOperationException>(() => ClientFactory.CreateClient(typeof(TestClientWithCredentials), typeof(TestClientOptions), clientOptions, configuration, null));

            Assert.AreEqual("Unable to find matching constructor while trying to create an instance of TestClientWithCredentials." + Environment.NewLine +
                            "Expected one of the follow sets of configuration parameters:" + Environment.NewLine +
                            "1. uri" + Environment.NewLine +
                            "2. uri, credential:key" + Environment.NewLine +
                            "3. uri, credential:signature" + Environment.NewLine +
                            "4. uri" + Environment.NewLine +
                            "" + Environment.NewLine +
                            "Found the following configuration keys: b, b:c, a",
                            exception.Message);
        }
 public TestClientWithCredentials(Uri uri, TokenCredential credential, TestClientOptions options) : base(uri, options)
 {
     Credential = credential;
 }
 public TestClientWithCredentials(Uri uri, TestClientOptions options) : base(uri, options)
 {
 }
 public TestClient(CompositeObject composite, TestClientOptions options)
 {
     Composite = composite;
     Options   = options;
 }
 public TestClient(Uri uri, CompositeObject composite, TestClientOptions options)
 {
     Uri       = uri;
     Composite = composite;
     Options   = options;
 }
 public TestClient(Uri uri, TestClientOptions options)
 {
     Uri     = uri;
     Options = options;
 }
Beispiel #23
0
 public TestClient(Guid guid, TestClientOptions options)
 {
     Guid    = guid;
     Options = options;
 }