Example #1
0
        public async Task ClientRequest_WithExpires_AddsToCache()
        {
            // arrange
            var state            = new TestState();
            var expectedResponse = state.AddHttpRequest(1);

            expectedResponse.Content.Headers.Expires = DateTime.UtcNow.AddDays(1);

            // act
            var response = await state.ExecuteRequest();

            // assert
            Predicate <Tuple <string, CachingHttpClient.CachedValues> > assert = AssertResult;

            state.Dependencies
            .Verify(x => x.Cache.Put(Match.Create(assert)), Times.Once);

            bool AssertResult(Tuple <string, CachingHttpClient.CachedValues> input)
            {
                CustomAssert.AssertDateAlmost(
                    expectedResponse.Content.Headers.Expires.Value.UtcDateTime,
                    ((Headers.CacheSettings.ExpirySettings.Soft)input.Item2.CacheSettings.ExpirySettings).Item.MustRevalidateAtUtc);

                return(true);
            }
        }
Example #2
0
        public async Task ClientRequest_WithMaxAge_AddsToCache()
        {
            // arrange
            var state            = new TestState();
            var expectedResponse = state.AddHttpRequest(1);

            expectedResponse.Headers.CacheControl = new CacheControlHeaderValue
            {
                MaxAge = TimeSpan.FromDays(1)
            };

            // act
            var response = await state.ExecuteRequest();

            // assert
            Predicate <Tuple <string, CachingHttpClient.CachedValues> > assert = AssertResult;

            state.Dependencies
            .Verify(x => x.Cache.Put(Match.Create(assert)), Times.Once);

            bool AssertResult(Tuple <string, CachingHttpClient.CachedValues> input)
            {
                Assert.AreEqual("G$:$:http://www.com/", input.Item1);
                CustomAssert.AssertCachedResponse(1, input.Item2.HttpResponse);
                CustomAssert.AssertDateAlmost(
                    DateTime.UtcNow.AddDays(1),
                    ((Headers.CacheSettings.ExpirySettings.HardUtc)input.Item2.CacheSettings.ExpirySettings).Item);
                return(true);
            }
        }
Example #3
0
        public async Task ClientRequest_WithWeakETag_Caches()
        {
            // arrange
            var state            = new TestState();
            var expectedResponse = state.AddHttpRequest(1);

            expectedResponse.Headers.ETag = new EntityTagHeaderValue("\"etg\"", true);

            // act
            var response = await state.ExecuteRequest();

            // assert
            Predicate <Tuple <string, CachingHttpClient.CachedValues> > assert = AssertResult;

            state.Dependencies
            .Verify(x => x.Cache.Put(Match.Create(assert)), Times.Once);

            bool AssertResult(Tuple <string, CachingHttpClient.CachedValues> input)
            {
                // if there is an ETag, expires should be now, and not the past expiry
                var settings = ((Soft)input.Item2.CacheSettings.ExpirySettings).Item;

                CustomAssert.AssertDateAlmost(DateTime.UtcNow, settings.MustRevalidateAtUtc);

                var etag = ((Headers.CacheSettings.Validator.ETag)settings.Validator).Item;
                var weak = (Headers.CacheSettings.EntityTag.Weak)etag;

                Assert.AreEqual("\"etg\"", weak.Item);

                return(true);
            }
        }
Example #4
0
        public async Task ClientRequest_WithWeakETag_ServerReturnsAnotherETag_CachesCorrectly()
        {
            // arrange
            var state = SetUpWeakETagScenario("\"etg 2\"");

            // act
            var response = await state.ExecuteRequest();

            // assert
            Predicate <Tuple <string, CachingHttpClient.CachedValues> > assertCachePut = AssertCachePut;

            state.Dependencies
            .Verify(x => x.Cache.Put(Match.Create(assertCachePut)), Times.Once);

            bool AssertCachePut(Tuple <string, CachingHttpClient.CachedValues> input)
            {
                Assert.AreEqual("server value", response.Headers.GetValues("x-custom-header").First());

                var settings = ((Soft)input.Item2.CacheSettings.ExpirySettings).Item;

                CustomAssert.AssertDateAlmost(DateTime.UtcNow, settings.MustRevalidateAtUtc);

                var etag = ((Headers.CacheSettings.Validator.ETag)settings.Validator).Item;
                var weak = (Headers.CacheSettings.EntityTag.Weak)etag;

                Assert.AreEqual("\"etg 2\"", weak.Item);

                return(true);
            }
        }
Example #5
0
        public void GetCacheTime_WithoutValidate_ReturnsHardExpiryBasedOnMaxAge()
        {
            // arrange
            var cacheHeaders = BuildHeaders(
                immutable: true,
                maxAge: TimeSpan.FromDays(1));

            // act
            var result = ((ExpirySettings.HardUtc)build(cacheHeaders).Value.ExpirySettings).Item;

            // assert
            CustomAssert.AssertDateAlmost(DateTime.UtcNow.AddDays(1), result);
        }
Example #6
0
        public void GetCacheTime_WithNoETag_SetsCorectValidator()
        {
            // arrange
            var cacheHeaders = BuildHeaders(
                immutable: true,
                sharedCache: true,
                maxAge: TimeSpan.FromDays(1),
                sMaxAge: TimeSpan.FromDays(2),
                exipiresUtc: DateTime.UtcNow.AddDays(3));

            // act
            var result = ((ExpirySettings.Soft)build(cacheHeaders).Value.ExpirySettings).Item;

            // assert
            var exDate = ((Validator.ExpirationDateUtc)result.Validator).Item;

            CustomAssert.AssertDateAlmost(DateTime.UtcNow.AddDays(3), exDate);
        }