public void Does_throw_304_when_not_ModifiedSince()
        {
            var client  = GetClient();
            var request = new SetCache {
                LastModified = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            client.RequestFilter = req =>
                                   req.IfModifiedSince = request.LastModified.Value;

            client.ResponseFilter = res =>
            {
                Assert.That(res.ContentLength, Is.EqualTo(0));
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=3600"));
            };

            try
            {
                var response = client.Get(request);
                Assert.Fail("Should throw 304 NotModified");
            }
            catch (Exception ex)
            {
                if (!ex.IsNotModified())
                {
                    throw;
                }
            }
        }
 public object Any(SetCache request)
 {
     return(new HttpResult(request)
     {
         Age = request.Age,
         ETag = request.ETag,
         MaxAge = request.MaxAge,
         Expires = request.Expires,
         LastModified = request.LastModified,
         CacheControl = request.CacheControl.GetValueOrDefault(CacheControl.None),
     });
 }
Ejemplo n.º 3
0
        public void CachedServiceClient_does_return_cached_ETag_Requests_when_MustRevalidate()
        {
            var client = GetCachedServiceClient();

            var request = new SetCache {
                ETag = "etag", CacheControl = CacheControl.MustRevalidate
            };

            var response = client.Get(request);

            Assert.That(client.CacheHits, Is.EqualTo(0));
            Assert.That(response, Is.EqualTo(request));

            response = client.Get(request);
            Assert.That(client.CacheHits, Is.EqualTo(1));
            Assert.That(response, Is.EqualTo(request));
        }
Ejemplo n.º 4
0
        public void CachedServiceClient_does_return_cached_LastModified_Requests()
        {
            var client = GetCachedServiceClient();

            var request = new SetCache {
                LastModified = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            var response = client.Get(request);

            Assert.That(client.CacheHits, Is.EqualTo(0));
            Assert.That(response, Is.EqualTo(request));

            response = client.Get(request);
            Assert.That(client.CacheHits, Is.EqualTo(1));
            Assert.That(response, Is.EqualTo(request));
        }
        public void Does_set_LastModified_and_Default_MaxAge()
        {
            var client  = GetClient();
            var request = new SetCache {
                LastModified = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            client.ResponseFilter = res =>
            {
                Assert.That(res.Headers[HttpHeaders.LastModified], Is.EqualTo(request.LastModified.Value.ToUniversalTime().ToString("r")));
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=600"));
            };

            var response = client.Get(request);

            Assert.That(response, Is.EqualTo(request));
        }
        public void Does_not_set_Etag_and_Default_MaxAge_on_POST()
        {
            var client = GetClient();

            client.ResponseFilter = res =>
            {
                Assert.That(res.Headers[HttpHeaders.ETag], Is.Null);
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.Null);
            };

            var request = new SetCache {
                ETag = "etag"
            };
            var response = client.Post(request);

            Assert.That(response, Is.EqualTo(request));
        }
        public void Does_set_Etag_and_Default_MaxAge()
        {
            var client = GetClient();

            client.ResponseFilter = res =>
            {
                Assert.That(res.Headers[HttpHeaders.ETag], Is.EqualTo("etag".Quoted()));
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=600"));
            };

            var request = new SetCache {
                ETag = "etag"
            };
            var response = client.Get(request);

            Assert.That(response, Is.EqualTo(request));
        }
        public void CachedServiceClient_does_return_cached_ETag_Requests()
        {
            var client = new CachedServiceClient(GetClient());

            var request = new SetCache {
                ETag = "etag"
            };

            var response = client.Get(request);

            Assert.That(client.CacheHits, Is.EqualTo(0));
            Assert.That(response, Is.EqualTo(request));

            response = client.Get(request);
            Assert.That(client.CacheHits, Is.EqualTo(1));
            Assert.That(response, Is.EqualTo(request));
        }
        public void Returns_response_when_ModifiedSince_LastModified()
        {
            var client  = GetClient();
            var request = new SetCache {
                LastModified = new DateTime(2016, 1, 1, 0, 0, 0)
            };

            client.RequestFilter = req =>
                                   req.IfModifiedSince = request.LastModified.Value + TimeSpan.FromSeconds(1);

            client.ResponseFilter = res =>
                                    Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=600"));

            var response = client.Get(request);

            Assert.That(response, Is.EqualTo(request));
        }
        public void Returns_response_when_etag_does_not_match()
        {
            var client = GetClient();

            client.RequestFilter = req =>
                                   req.Headers[HttpHeaders.IfNoneMatch] = "etag".Quoted();

            client.ResponseFilter = res =>
            {
                Assert.That(res.Headers[HttpHeaders.ETag], Is.EqualTo("etag-alt".Quoted()));
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=600"));
            };

            var request = new SetCache {
                ETag = "etag-alt"
            };
            var response = client.Get(request);

            Assert.That(response, Is.EqualTo(request));
        }
        public void Does_set_Etag_MaxAge_and_CacheControl()
        {
            var client = GetClient();

            client.ResponseFilter = res =>
            {
                Assert.That(res.Headers[HttpHeaders.Age], Is.EqualTo("864000"));
                Assert.That(res.Headers[HttpHeaders.ETag], Is.EqualTo("etag".Quoted()));
                Assert.That(res.Headers[HttpHeaders.CacheControl], Is.EqualTo("max-age=86400, public, must-revalidate, no-store"));
            };

            var request = new SetCache
            {
                ETag         = "etag",
                Age          = TimeSpan.FromDays(10),
                MaxAge       = TimeSpan.FromDays(1),
                CacheControl = CacheControl.Public | CacheControl.NoStore | CacheControl.MustRevalidate,
            };
            var response = client.Get(request);

            Assert.That(response, Is.EqualTo(request));
        }