示例#1
0
        public void CreateClient_UriRelative_Throws()
        {
            var configs = new Dictionary <string, ClientConfig>()
            {
                { "Bob", new ClientConfig {
                      BaseAddress = "/test/", Policies = null, RequestHeaders = null
                  } }
            };
            var logger  = new Mock <ILogger>(MockBehavior.Loose);
            var factory = new SimpleHttpClientFactory(configs, logger.Object);

            bool platformException = false;

            try
            {
                factory.CreateClient("Bob");
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e) when(e is ArgumentException || e is UriFormatException)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                platformException = true;
            }

            // When running dotnet test on Windows and Linux, different exceptions are encountered
            Assert.IsTrue(platformException, "The platform specific exception was encountered");
        }
示例#2
0
        public void CreateClient_UriAbsolute_InitializedHeaderValues_Succeeds()
        {
            var headers = new Dictionary <string, string>
            {
                { "header1", "test1" },
                { "header2", "test2" }
            };

            var configs = new Dictionary <string, ClientConfig>
            {
                { "Bob", new ClientConfig {
                      BaseAddress = "http://test.com/", Policies = null, RequestHeaders = headers
                  } }
            };
            var logger  = new Mock <ILogger>(MockBehavior.Loose);
            var factory = new SimpleHttpClientFactory(configs, logger.Object);

            HttpClient client = factory.CreateClient("Bob");

            Assert.IsNotNull(client);
            Assert.AreEqual("http://test.com/", client.BaseAddress.ToString());
            Assert.AreEqual(2, client.DefaultRequestHeaders.Count());

            client.Dispose();
        }
示例#3
0
        public void CreateClient_NameNotRegistered_Throws()
        {
            var logger  = new Mock <ILogger>(MockBehavior.Loose);
            var configs = new Dictionary <string, ClientConfig>();
            var factory = new SimpleHttpClientFactory(configs, logger.Object);

            Assert.ThrowsException <ArgumentException>(
                () => factory.CreateClient("Missing"));
        }
示例#4
0
        public void CreateClient_EmptyConfig_Throws()
        {
            var configs = new Dictionary <string, ClientConfig>()
            {
                { "Bob", new ClientConfig() }
            };
            var logger  = new Mock <ILogger>(MockBehavior.Loose);
            var factory = new SimpleHttpClientFactory(configs, logger.Object);

            Assert.ThrowsException <ArgumentNullException>(
                () => factory.CreateClient("Bob"));
        }
示例#5
0
        public void CreateClient_UriEmpty_Throws()
        {
            var configs = new Dictionary <string, ClientConfig>()
            {
                { "Bob", new ClientConfig {
                      BaseAddress = string.Empty, Policies = null, RequestHeaders = null
                  } }
            };
            var logger  = new Mock <ILogger>(MockBehavior.Loose);
            var factory = new SimpleHttpClientFactory(configs, logger.Object);

            Assert.ThrowsException <UriFormatException>(
                () => factory.CreateClient("Bob"));
        }