public async Task SetDefaultHeaderIfNoSpecificRouteIsSet() { var defaultHeader = new CacheControlHeaderValue { NoCache = true }; var httpContext = new DefaultHttpContext(); var nextCalled = false; var middleware = new CacheHeaderMiddleware(new RequestDelegate(_ => { nextCalled = true; return(Task.CompletedTask); }), new CacheHeaderOptions() { DefaultHeader = defaultHeader }); await middleware.Invoke(httpContext); Assert.Equal(defaultHeader, httpContext.Response.GetTypedHeaders().CacheControl); Assert.True(nextCalled); // Does not override exisiting header nextCalled = false; var newHeader = new CacheControlHeaderValue { NoCache = false, MaxAge = TimeSpan.FromDays(10) }; await middleware.Invoke(httpContext); Assert.Equal(defaultHeader, httpContext.Response.GetTypedHeaders().CacheControl); Assert.True(nextCalled); }
public async Task SetRouteHeaderIfRouteMatches() { var defaultHeader = new CacheControlHeaderValue { NoCache = true }; var routeHeader = new CacheControlHeaderValue { NoCache = false, MaxAge = TimeSpan.FromDays(10) }; var httpContext = new DefaultHttpContext() { Request = { Path = "/SomeOtherRoute" } }; var nextCalled = false; var middleware = new CacheHeaderMiddleware(new RequestDelegate(_ => { nextCalled = true; return(Task.CompletedTask); }), new CacheHeaderOptions() { DefaultHeader = defaultHeader, Routes = { { "/MyFancyRoute", routeHeader } } }); await middleware.Invoke(httpContext); Assert.Equal(defaultHeader, httpContext.Response.GetTypedHeaders().CacheControl); Assert.True(nextCalled); nextCalled = false; httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/MyFancyRoute/xyz/asdf"; await middleware.Invoke(httpContext); Assert.Equal(routeHeader, httpContext.Response.GetTypedHeaders().CacheControl); Assert.True(nextCalled); // Check route matching ignores case (Ordinal ignore case) httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/myfancyroute/xyz/asdf"; await middleware.Invoke(httpContext); Assert.Equal(routeHeader, httpContext.Response.GetTypedHeaders().CacheControl); }