Ejemplo n.º 1
0
        public async Task Get_NoStore()
        {
            // DefaultCacheDuration set but caching forbidden with NoStore so
            // should not be cached.

            var cache = new MemoryCache(new MemoryCacheOptions());

            var original = new HttpResponseMessage(HttpStatusCode.OK);

            original.Headers.CacheControl         = new CacheControlHeaderValue();
            original.Headers.CacheControl.NoStore = true;

            var backing = new MockHttpMessageHandler(async r => original);

            var handler = new CachingHttpHandler(backing, cache);

            handler.DefaultCacheDuration = new TimeSpan(0, 5, 0);

            var client = new HttpClient(handler);

            var response1 = await client.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, "http://example.com"));

            var response2 = await client.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, "http://example.com"));

            Assert.AreEqual(2, backing.CallCount); // not cached
        }
Ejemplo n.º 2
0
        public async Task Get_DefaultCacheDuration()
        {
            // No cache control headers, so the request may be cached, and with
            // DefaultCacheDuration so it should be.

            var cache = new MemoryCache(new MemoryCacheOptions());

            var original = new HttpResponseMessage(HttpStatusCode.OK);

            original.Content = new StringContent("Hello, World!");

            var backing = new MockHttpMessageHandler(async r => original);

            var handler = new CachingHttpHandler(backing, cache);

            handler.DefaultCacheDuration = new TimeSpan(0, 5, 0);

            var client = new HttpClient(handler);

            var response1 = await client.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, "http://example.com"));

            var response2 = await client.SendAsync(
                new HttpRequestMessage(HttpMethod.Get, "http://example.com"));

            Assert.AreEqual(1, backing.CallCount); // cached
        }