コード例 #1
0
        public async Task TryCreate_CachesResultsForFortyMinutes()
        {
            // Arrange
            var source  = "http://some-site/index.json";
            var content = @"{ version: '3.1.0-beta' }";
            var handler = new Mock <TestMessageHandler>(new Dictionary <string, string> {
                { source, content }
            })
            {
                CallBase = true
            };
            var sequence = new MockSequence();

            handler
            .InSequence(sequence)
            .Setup(h => h.SendAsyncPublic(It.IsAny <HttpRequestMessage>()))
            .Returns(() =>
            {
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound)));
            });

            handler
            .InSequence(sequence)
            .Setup(h => h.SendAsyncPublic(It.IsAny <HttpRequestMessage>()))
            .Returns(() =>
            {
                var response     = new HttpResponseMessage();
                response.Content = new StringContent(content);
                return(Task.FromResult(response));
            });

            var provider         = new TestableSerivceIndexResourceProvider();
            var sourceRepository = new SourceRepository(new PackageSource(source),
                                                        new INuGetResourceProvider[] { new TestHttpHandlerProvider(handler.Object), provider });
            var startTime = DateTime.UtcNow;

            // Act - 1
            provider.SetUtcNow(startTime);
            var result = await provider.TryCreate(sourceRepository, default(CancellationToken));

            // Assert - 1
            Assert.False(result.Item1);
            Assert.Null(result.Item2);

            // Act - 2
            provider.SetUtcNow(startTime.AddMinutes(45));
            result = await provider.TryCreate(sourceRepository, default(CancellationToken));

            // Assert - 2
            Assert.True(result.Item1);
            var resource = Assert.IsType <ServiceIndexResourceV3>(result.Item2);

            Assert.NotNull(resource.Index);
        }
コード例 #2
0
        public async Task TryCreate_CachesResultsForFortyMinutes()
        {
            // Arrange
            var source = "http://some-site/index.json";
            var content = @"{ version: '3.1.0-beta' }";
            var handler = new Mock<TestMessageHandler>(new Dictionary<string, string> { { source, content } })
            {
                CallBase = true
            };
            var sequence = new MockSequence();
            handler
                .InSequence(sequence)
                .Setup(h => h.SendAsyncPublic(It.IsAny<HttpRequestMessage>()))
                .Returns(() =>
                {
                    return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound));
                });

            handler
                .InSequence(sequence)
                .Setup(h => h.SendAsyncPublic(It.IsAny<HttpRequestMessage>()))
                .Returns(() =>
                {
                    var response = new HttpResponseMessage();
                    response.Content = new StringContent(content);
                    return Task.FromResult(response);
                });

            var provider = new TestableSerivceIndexResourceProvider();
            var sourceRepository = new SourceRepository(new PackageSource(source),
                new INuGetResourceProvider[] { new TestHttpHandlerProvider(handler.Object), provider });
            var startTime = DateTime.UtcNow;

            // Act - 1
            provider.SetUtcNow(startTime);
            var result = await provider.TryCreate(sourceRepository, default(CancellationToken));

            // Assert - 1
            Assert.False(result.Item1);
            Assert.Null(result.Item2);

            // Act - 2
            provider.SetUtcNow(startTime.AddMinutes(45));
            result = await provider.TryCreate(sourceRepository, default(CancellationToken));

            // Assert - 2
            Assert.True(result.Item1);
            var resource = Assert.IsType<ServiceIndexResourceV3>(result.Item2);
            Assert.NotNull(resource.Index);
        }