Example #1
0
        public async Task GetJson_BreakLimit()
        {
            BlizzardApiConfiguration config = BlizzardApiReaderTests.DefaultConfig.Clone();

            config.Limiter = new List <Limiter>
            {
                new Limiter {
                    RatesPerTimespan = 10, TimeBetweenLimitReset = new TimeSpan(0, 0, 5)
                },
            };
            WebClientMocker webClient = new WebClientMocker();

            webClient.SetupAuth(true);
            webClient.SetupApiRequest(It.IsAny <string>(), HttpStatusCode.OK, ExpectedJson);
            BlizzardApiReader api = new BlizzardApiReader(Options.Create(config), webClient.WebClient);

            List <Exception> exceptions = new List <Exception>();
            int count = config.Limiter.First().RatesPerTimespan;

            for (int i = 0; i < count + 1; i++)
            {
                Exception ex = await Record.ExceptionAsync(() => api.GetJsonAsync("test")).ConfigureAwait(false);

                if (ex != null)
                {
                    exceptions.Add(ex);
                }
            }
            Assert.Single(exceptions);
            Assert.IsType <RateLimitReachedException>(exceptions.First());
            webClient.VerifyAuth(Times.Once());
            webClient.VerifyApiRequest(It.IsAny <string>(), Times.Exactly(count));
        }
Example #2
0
        public async Task GetJson_ApiFailed()
        {
            WebClientMocker webClient = new WebClientMocker();

            webClient.SetupAuth(true);
            webClient.SetupApiRequest(It.IsAny <string>(), HttpStatusCode.InternalServerError, ExpectedJson);
            BlizzardApiReader api = new BlizzardApiReader(BlizzardApiReaderTests.DefaultConfiguration, webClient.WebClient);

            await Assert.ThrowsAsync <BadResponseException>(() => api.GetJsonAsync("test")).ConfigureAwait(false);

            webClient.VerifyAuth(Times.Once());
            webClient.VerifyApiRequest(It.IsAny <string>(), Times.Once());
        }
Example #3
0
        public async Task GetJson_Valid()
        {
            WebClientMocker webClient = new WebClientMocker();

            webClient.SetupAuth(true);
            webClient.SetupApiRequest(It.IsAny <string>(), HttpStatusCode.OK, ExpectedJson);
            BlizzardApiReader api = new BlizzardApiReader(BlizzardApiReaderTests.DefaultConfiguration, webClient.WebClient);

            string jsonResult = await api.GetJsonAsync("test").ConfigureAwait(false);

            Assert.Equal(ExpectedJson, jsonResult);
            webClient.VerifyAuth(Times.Once());
            webClient.VerifyApiRequest(It.IsAny <string>(), Times.Once());
        }