Example #1
0
        public async Task Allows_Setting_Global_HttpClientFactory()
        {
            var factory = new FakeHttpClientFactory();

            ShopifyService.SetGlobalHttpClientFactory(factory);

            var shopService  = new ShopService(Utils.MyShopifyUrl, Utils.AccessToken);
            var orderService = new OrderService(Utils.MyShopifyUrl, Utils.AccessToken);
            var policy       = new LeakyBucketExecutionPolicy();
            var orderFilter  = new OrderListFilter
            {
                Limit = 1
            };

            shopService.SetExecutionPolicy(policy);
            orderService.SetExecutionPolicy(policy);

            var ex1 = await Assert.ThrowsAsync <Exception>(() => shopService.GetAsync());

            var ex2 = await Assert.ThrowsAsync <Exception>(() => orderService.ListAsync(orderFilter));

            Assert.Equal("This is an exception thrown by the FakeHttpClient", ex1.Message);
            Assert.Equal("This is an exception thrown by the FakeHttpClient", ex2.Message);

            // Removing the factory should only remove it for future instances
            ShopifyService.SetGlobalHttpClientFactory(null);

            ex1 = await Assert.ThrowsAsync <Exception>(() => shopService.GetAsync());

            ex2 = await Assert.ThrowsAsync <Exception>(() => orderService.ListAsync(orderFilter));

            Assert.Equal("This is an exception thrown by the FakeHttpClient", ex1.Message);
            Assert.Equal("This is an exception thrown by the FakeHttpClient", ex2.Message);

            // Instantiating the services again should now use the default HttpClientFactory
            shopService  = new ShopService(Utils.MyShopifyUrl, Utils.AccessToken);
            orderService = new OrderService(Utils.MyShopifyUrl, Utils.AccessToken);

            shopService.SetExecutionPolicy(policy);
            orderService.SetExecutionPolicy(policy);

            var shop = await shopService.GetAsync();

            var orders = await orderService.ListAsync(orderFilter);

            Assert.NotNull(shop);
            Assert.NotNull(orders);
        }