public void SettingCachingRuleShouldWork() { TimeSpan maxAge = TimeSpan.FromDays(7); cmd.CacheByMaxAge(@"somedir\*", maxAge); routeMatch.AddParameter("path", @"somedir\somepath.png"); const string ExpectedFileName = @"d:\somedir\contents\somedir\somepath.png"; fileSystem.Stub(x => x.DoesFileExist(ExpectedFileName)).Return(true); IFileInformation fileInfo = MockRepository.GenerateStub <IFileInformation>(); fileInfo.LastWriteTime = now.AddDays(-3); fileSystem.Stub(x => x.GetFileInformation(ExpectedFileName)).Return(fileInfo); fileSystem.Stub(x => x.ReadFileAsBytes(ExpectedFileName)).Return(new byte[100]); IWebCommandResult result = cmd.Execute(context, routeMatch); FileResult fileResult = (FileResult)result; CachingByMaxAgePolicy maxAgePolicy = (CachingByMaxAgePolicy)fileResult.CachingPolicy; Assert.AreEqual(maxAge, maxAgePolicy.MaxAge); context.RequestHeaders.Add(HttpConsts.HeaderIfModifiedSince, fileInfo.LastWriteTime.ToRfc2822DateTime()); result.Apply(context); Assert.AreEqual((int)HttpStatusCode.NotModified, context.StatusCode); }
public void CacheByMaxAgeWhenNew() { TimeSpan maxAge = TimeSpan.FromHours(1); DateTime lastModifiedTime = new DateTime(2014, 06, 15, 18, 14, 00); CachingByMaxAgePolicy policy = new CachingByMaxAgePolicy(maxAge, x => lastModifiedTime); policy.ProcessRequest(null, WebContext, ReturnResource); AssertCachedByAgeResult(HttpStatusCode.OK, maxAge, lastModifiedTime); Assert.IsTrue(ResourceReturned); }
public IWebCommandResult Execute(IWebContext context, WebRequestRouteMatch routeMatch) { string path = "favicon-" + routeMatch["path"]; // do not allow rooted (absolute) paths if (Path.IsPathRooted(path)) { HttpStatusResult httpStatusResult = new HttpStatusResult(HttpStatusCode.BadRequest); httpStatusResult.LoggingSeverity = LoggingSeverity.Error; return(httpStatusResult); } string fileFullPath = Path.Combine(faviconsDirectory, path); // do not allow access outside of the contents folder (and its children) fileFullPath = Path.GetFullPath(fileFullPath); if (!fileFullPath.StartsWith(faviconsDirectory, StringComparison.Ordinal)) { return(new HttpStatusResult(HttpStatusCode.Forbidden)); } if (fileSystem.DoesFileExist(fileFullPath)) { ICachingPolicy cachingPolicy; if (context.Configuration.WebServerDevelopmentMode) { cachingPolicy = new NoCachingPolicy(); } else { cachingPolicy = new CachingByMaxAgePolicy(TimeSpan.FromDays(30), FileLastModifiedFunc); } FileResult fileResult = new FileResult(fileFullPath, cachingPolicy); fileResult.LoggingSeverity = LoggingSeverity.Verbose; fileResult.FileCache = fileCache; return(fileResult); } if (log.IsDebugEnabled) { log.DebugFormat("Request file {0} does not exist", fileFullPath); } HttpStatusResult notFoundResult = new HttpStatusResult(HttpStatusCode.NotFound); notFoundResult.LoggingSeverity = LoggingSeverity.Verbose; return(notFoundResult); }
public void CacheByMaxAgeShouldCloseResponseEvenWhenFresh() { TimeSpan maxAge = TimeSpan.FromHours(1); DateTime lastModifiedTime = new DateTime(2014, 06, 15, 18, 14, 00); CachingByMaxAgePolicy policy = new CachingByMaxAgePolicy(maxAge, x => lastModifiedTime); DateTime cachedTime = lastModifiedTime; WebContext.RequestHeaders.Add(HttpConsts.HeaderIfModifiedSince, cachedTime.ToRfc2822DateTime()); policy.ProcessRequest(null, WebContext, ReturnResource); Assert.IsTrue(WebContext.IsResponseClosed); }
public void CacheByMaxAgeWhenStale() { TimeSpan maxAge = TimeSpan.FromHours(1); DateTime lastModifiedTime = new DateTime(2014, 06, 15, 20, 14, 00); CachingByMaxAgePolicy policy = new CachingByMaxAgePolicy(maxAge, x => lastModifiedTime); DateTime cachedTime = new DateTime(2014, 06, 15, 18, 14, 00); WebContext.RequestHeaders.Add(HttpConsts.HeaderIfModifiedSince, cachedTime.ToRfc2822DateTime()); policy.ProcessRequest(null, WebContext, ReturnResource); AssertCachedByAgeResult(HttpStatusCode.OK, maxAge, lastModifiedTime); Assert.IsTrue(ResourceReturned); }
public void SubsecondsShouldBeIgnoredWhenComparingTimes() { TimeSpan maxAge = TimeSpan.FromHours(1); DateTime lastModifiedTime = new DateTime(2014, 06, 15, 18, 14, 00, 666); CachingByMaxAgePolicy policy = new CachingByMaxAgePolicy(maxAge, x => lastModifiedTime); DateTime cachedTime = lastModifiedTime; WebContext.RequestHeaders.Add(HttpConsts.HeaderIfModifiedSince, cachedTime.ToRfc2822DateTime()); policy.ProcessRequest(null, WebContext, ReturnResource); AssertCachedByAgeResult(HttpStatusCode.NotModified, maxAge, lastModifiedTime); Assert.IsFalse(ResourceReturned); }