Пример #1
0
        internal async Task <SortedList <byte, HttpRequestMessage> > Fetch(
            ConfigurationBase fetchConfig,
            Dictionary <string, SettingsWithPreferences> responsesRegistry,
            byte fetchInvokeCount = 1)
        {
            // Arrange

            byte requestCount = 1;
            var  requests     = new SortedList <byte, HttpRequestMessage>();

            var handlerMock = new Mock <HttpClientHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .Callback <HttpRequestMessage, CancellationToken>((message, _) =>
            {
                requests.Add(requestCount++, message);
            })
            .Returns <HttpRequestMessage, CancellationToken>((message, _) => Task.FromResult(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(responsesRegistry[message.RequestUri.Host]))
            }))
            .Verifiable();

            IConfigFetcher fetcher = new HttpConfigFetcher(
                fetchConfig.CreateUri(),
                "DEMO",
                Mock.Of <ILogger>(),
                handlerMock.Object,
                new ConfigDeserializer(Mock.Of <ILogger>(), JsonSerializer.Create()),
                fetchConfig.IsCustomBaseUrl);

            // Act

            byte i = 0;

            do
            {
                await fetcher.Fetch(ProjectConfig.Empty);

                i++;
            } while (fetchInvokeCount > i);

            // Assert

            return(requests);
        }
Пример #2
0
        public async Task WithDataGovernanceSetting_ShouldUseProperCdnUrl(DataGovernance dataGovernance, string expectedUrl)
        {
            // Arrange

            var configuration = new AutoPollConfiguration
            {
                SdkKey         = "DEMO",
                DataGovernance = dataGovernance
            };

            byte requestCount = 0;
            var  requests     = new SortedList <byte, HttpRequestMessage>();

            var handlerMock = new Mock <HttpClientHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .Callback <HttpRequestMessage, CancellationToken>((message, _) =>
            {
                requests.Add(requestCount++, message);
            })
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("{\"p\": {\"u\": \"http://example.com\", \"r\": 0}}"),
            })
            .Verifiable();

            IConfigFetcher fetcher = new HttpConfigFetcher(
                configuration.CreateUri(),
                "DEMO",
                Mock.Of <ILogger>(),
                handlerMock.Object,
                Mock.Of <IConfigDeserializer>(),
                configuration.IsCustomBaseUrl);

            // Act

            await fetcher.Fetch(ProjectConfig.Empty);

            // Assert

            handlerMock.VerifyAll();
            Assert.AreEqual(1, requestCount);
            Assert.AreEqual(new Uri(expectedUrl).Host, requests[0].RequestUri.Host);
        }
Пример #3
0
        public async Task HttpConfigFetcher_WithCustomHttpClientHandler_ShouldUsePassedHandler()
        {
            // Arrange

            var myHandler = new MyFakeHttpClientHandler();

            var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new MyCounterLogger(), myHandler, Mock.Of <IConfigDeserializer>(), false);

            // Act

            await instance.Fetch(ProjectConfig.Empty);

            // Assert

            Assert.AreEqual(1, myHandler.SendInvokeCount);
        }
Пример #4
0
        public async Task HttpConfigFetcher_ThrowAnException_ShouldReturnPassedConfig()
        {
            // Arrange

            var myHandler = new ExceptionThrowerHttpClientHandler(new WebException());

            var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new MyCounterLogger(), myHandler, Mock.Of <IConfigDeserializer>(), false);

            var lastConfig = new ProjectConfig("{ }", DateTime.UtcNow, "\"ETAG\"");

            // Act

            var actual = await instance.Fetch(lastConfig);

            // Assert

            Assert.AreEqual(lastConfig, actual);
        }
Пример #5
0
        public async Task HttpConfigFetcher_ResponseHttpCodeIsUnexpected_ShouldReturnsPassedConfig()
        {
            // Arrange

            var myHandler = new MyFakeHttpClientHandler(HttpStatusCode.Forbidden);

            var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new MyCounterLogger(), myHandler, Mock.Of <IConfigDeserializer>(), false);

            var lastConfig = new ProjectConfig("{ }", DateTime.UtcNow, "\"ETAG\"");

            // Act

            var actual = await instance.Fetch(lastConfig);

            // Assert

            Assert.AreEqual(lastConfig, actual);
        }