Example #1
0
        public async Task ServesFreshContent_If_PragmaNoCache(string method)
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();

                    var initialResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));

                    // verify the response is cached
                    var cachedResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));
                    await AssertCachedResponseAsync(initialResponse, cachedResponse);

                    // assert cached response no longer served
                    client.DefaultRequestHeaders.Pragma.Clear();
                    client.DefaultRequestHeaders.Pragma.Add(new System.Net.Http.Headers.NameValueHeaderValue("no-cache"));
                    var subsequentResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #2
0
        public async Task ServesCachedContent_IfCachedVaryByNotUpdated_OnCacheMiss()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Response.Headers[HeaderNames.Vary] = context.Request.Headers[HeaderNames.Pragma]);

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();
                    client.DefaultRequestHeaders.From = "*****@*****.**";
                    client.DefaultRequestHeaders.Pragma.Clear();
                    client.DefaultRequestHeaders.Pragma.Add(new System.Net.Http.Headers.NameValueHeaderValue("From"));
                    client.DefaultRequestHeaders.MaxForwards = 1;
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.From = "*****@*****.**";
                    client.DefaultRequestHeaders.Pragma.Clear();
                    client.DefaultRequestHeaders.Pragma.Add(new System.Net.Http.Headers.NameValueHeaderValue("From"));
                    client.DefaultRequestHeaders.MaxForwards = 2;
                    var otherResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.From = "*****@*****.**";
                    client.DefaultRequestHeaders.Pragma.Clear();
                    client.DefaultRequestHeaders.Pragma.Add(new System.Net.Http.Headers.NameValueHeaderValue("From"));
                    client.DefaultRequestHeaders.MaxForwards = 1;
                    var subsequentResponse = await client.GetAsync("");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
        public async Task ServesFreshContent_IfIHttpSendFileFeature_Used()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(
                app =>
            {
                app.Use(async(context, next) =>
                {
                    context.Features.Set <IHttpSendFileFeature>(new DummySendFileFeature());
                    await next.Invoke();
                });
            },
                contextAction: async context => await context.Features.Get <IHttpSendFileFeature>().SendFileAsync("dummy", 0, 0, CancellationToken.None));

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #4
0
        public async Task Serves304_IfIfNoneMatch_Satisfied()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context =>
            {
                context.Response.GetTypedHeaders().ETag = new EntityTagHeaderValue("\"E1\"");
                context.Response.Headers[HeaderNames.ContentLocation] = "/";
                context.Response.Headers[HeaderNames.Vary]            = HeaderNames.From;
            });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("?Expires=90");

                    client.DefaultRequestHeaders.IfNoneMatch.Add(new System.Net.Http.Headers.EntityTagHeaderValue("\"E1\""));
                    var subsequentResponse = await client.GetAsync("");

                    initialResponse.EnsureSuccessStatusCode();
                    Assert.Equal(System.Net.HttpStatusCode.NotModified, subsequentResponse.StatusCode);
                    Assert304Headers(initialResponse, subsequentResponse);
                }
            }
        }
Example #5
0
        public async Task ServesCachedContent_If_PathCasingDiffers(string method)
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, "path"));

                    var subsequentResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, "PATH"));

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #6
0
        public async Task ServesFreshContent_Get_Head()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, ""));

                    var subsequentResponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, ""));

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
        public async Task ServesCachedContent_IfVaryQueryKeys_Matches()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Features.Get <Microsoft.AspNetCore.ResponseCaching.IResponseCachingFeature>().VaryByQueryKeys = new[] { "query" });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("?query=value");

                    var subsequentResponse = await client.GetAsync("?query=value");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #8
0
        public async Task ServesFreshContent_Post()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.PostAsync("", new StringContent(string.Empty));

                    var subsequentResponse = await client.PostAsync("", new StringContent(string.Empty));

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #9
0
        public async Task ServesCachedContent_IfVaryQueryKeysExplicit_Matches_QueryKeyCaseInsensitive()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Features.Get <IResponseCachingFeature>().VaryByQueryKeys = new[] { "QueryA", "queryb" });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("?querya=valuea&queryb=valueb");

                    var subsequentResponse = await client.GetAsync("?QueryA=valuea&QueryB=valueb");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #10
0
        public async Task ServesFreshContent_IfVaryQueryKeyStar_Mismatch_QueryKeyValueCaseSensitive()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Features.Get <IResponseCachingFeature>().VaryByQueryKeys = new[] { "*" });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("?querya=valuea&queryb=valueb");

                    var subsequentResponse = await client.GetAsync("?querya=ValueA&queryb=ValueB");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #11
0
        public async Task ServesFreshContent_IfNotAvailable(string method)
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));

                    var subsequentResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, "different"));

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #12
0
        public async Task ServesCachedContent_IfVaryQueryKeyStar_Matches_OrderInsensitive()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Features.Get <IResponseCachingFeature>().VaryByQueryKeys = new[] { "*" });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("?QueryA=ValueA&QueryB=ValueB");

                    var subsequentResponse = await client.GetAsync("?QueryB=ValueB&QueryA=ValueA");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #13
0
        public async Task ServesFreshContent_IfSetCookie_IsSpecified()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Response.Headers[HeaderNames.SetCookie] = "cookieName=cookieValue");

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #14
0
        public async Task ServesFreshContent_IfInitialResponseContainsNoStore()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Response.Headers[HeaderNames.CacheControl] = CacheControlHeaderValue.NoStoreString);

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #15
0
        public async Task ServesCachedContent_IfVaryHeader_Matches()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Response.Headers[HeaderNames.Vary] = HeaderNames.From);

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();
                    client.DefaultRequestHeaders.From = "*****@*****.**";
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #16
0
        public async Task ServesCachedContent_IfIfNoneMatch_NotSatisfied()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(contextAction: context => context.Response.GetTypedHeaders().ETag = new EntityTagHeaderValue("\"E1\""));

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.IfNoneMatch.Add(new System.Net.Http.Headers.EntityTagHeaderValue("\"E2\""));
                    var subsequentResponse = await client.GetAsync("");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #17
0
        public async Task ServesFreshContent_If_Authorization_HeaderExists(string method)
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();
                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("abc");
                    var initialResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));

                    var subsequentResponse = await client.SendAsync(ResponseCachingTestUtils.CreateRequest(method, ""));

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #18
0
        public async Task ServesCachedContent_IfIfModifiedSince_NotSatisfied()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.MinValue;
                    var subsequentResponse = await client.GetAsync("");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
        public async Task Serves304_IfIfModifiedSince_Satisfied()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.MaxValue;
                    var subsequentResponse = await client.GetAsync("");

                    initialResponse.EnsureSuccessStatusCode();
                    Assert.Equal(System.Net.HttpStatusCode.NotModified, subsequentResponse.StatusCode);
                }
            }
        }
Example #20
0
        public async Task ServesFreshContent_CaseSensitivePaths_IsNotCacheable()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(options: new ResponseCachingOptions()
            {
                UseCaseSensitivePaths = true
            });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("/path");

                    var subsequentResponse = await client.GetAsync("/Path");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #21
0
        public async Task ServesFreshContent_IfBodySize_IsNotCacheable()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(options: new ResponseCachingOptions()
            {
                MaximumBodySize = 1
            });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("/different");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #22
0
        public async Task ServesFreshContent_IfRequestRequirements_NotMet()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        MaxAge = TimeSpan.FromSeconds(0)
                    };
                    var subsequentResponse = await client.GetAsync("");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #23
0
        public async Task ServesFreshContent_IfInitialRequestContainsNoStore()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client = server.CreateClient();
                    client.DefaultRequestHeaders.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        NoStore = true
                    };
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertFreshResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }
Example #24
0
        public async Task Serves504_IfOnlyIfCachedHeader_IsSpecified()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching();

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    client.DefaultRequestHeaders.CacheControl = new System.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        OnlyIfCached = true
                    };
                    var subsequentResponse = await client.GetAsync("/different");

                    initialResponse.EnsureSuccessStatusCode();
                    Assert.Equal(System.Net.HttpStatusCode.GatewayTimeout, subsequentResponse.StatusCode);
                }
            }
        }
        public async Task ServesCachedContent_IfIHttpSendFileFeature_NotUsed()
        {
            var builders = ResponseCachingTestUtils.CreateBuildersWithResponseCaching(app =>
            {
                app.Use(async(context, next) =>
                {
                    context.Features.Set <IHttpSendFileFeature>(new DummySendFileFeature());
                    await next.Invoke();
                });
            });

            foreach (var builder in builders)
            {
                using (var server = new TestServer(builder))
                {
                    var client          = server.CreateClient();
                    var initialResponse = await client.GetAsync("");

                    var subsequentResponse = await client.GetAsync("");

                    await AssertCachedResponseAsync(initialResponse, subsequentResponse);
                }
            }
        }