public void WillCorrectlySetContentType(string path, string contentType, bool contentInStore) { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.ContentType); response.Setup(x => x.Headers).Returns(new NameValueCollection()); response.Setup(x => x.Cache).Returns(new Mock<HttpCachePolicyBase>().Object); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Response).Returns(response.Object); context.Setup(x => x.Request.RawUrl).Returns(path); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/RRContent"); var store = new Mock<IStore>(); store.Setup( x => x.SendContent(It.IsAny<string>(), response.Object)). Returns(contentInStore); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use(new UriBuilder(config.Object)); }); module.HandleRRContent(context.Object); Assert.Equal(contentType, response.Object.ContentType); RRContainer.Current = null; }
public void WillDelegateContentMappedToHandler() { var module = new RequestReduceModule(); var handler = new DefaultHttpHandler(); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/RRContent"); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.Url).Returns(new Uri("http://host/content/someresource.less")); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); context.Setup(x => x.Items).Returns(new Hashtable()); var handlerFactory = new HandlerFactory(config.Object, new UriBuilder(config.Object)); handlerFactory.AddHandlerMap(x => x.AbsolutePath.EndsWith(".less") ? handler : null); handlerFactory.AddHandlerMap(x => x.AbsolutePath.EndsWith(".les") ? new DefaultHttpHandler() : null); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IUriBuilder>().Use<UriBuilder>(); x.For<IHandlerFactory>().Use(handlerFactory); }); module.HandleRRContent(context.Object); Assert.Equal(handler, context.Object.Items["remapped handler"]); RRContainer.Current = null; }
public void WillSetPhysicalPathToMappedVirtualPathWhenHandlingContent() { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/Virtual"); context.Setup(x => x.Server.MapPath("/Virtual")).Returns("physical"); context.Setup(x => x.Request.RawUrl).Returns("/Virtual/blah"); context.Setup(x => x.Response.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); var cache = new Mock<HttpCachePolicyBase>(); context.Setup(x => x.Response.Cache).Returns(cache.Object); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(new Mock<IStore>().Object); x.For<IUriBuilder>().Use(new Mock<IUriBuilder>().Object); }); module.HandleRRContent(context.Object); config.VerifySet(x => x.SpritePhysicalPath = "physical", Times.Once()); RRContainer.Current = null; }
public void WillSetCachabilityIfInRRPathAndStoreSendsContent(string path) { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.RawUrl).Returns("/RRContent/css.css"); context.Setup(x => x.Request.Url).Returns(new Uri("http://localhost/RRContent/css.css")); context.Setup(x => x.Response.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); var cache = new Mock<HttpCachePolicyBase>(); context.Setup(x => x.Response.Cache).Returns(cache.Object); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns(path); var store = new Mock<IStore>(); store.Setup( x => x.SendContent(It.IsAny<string>(), context.Object.Response)). Returns(true); var builder = new Mock<IUriBuilder>(); builder.Setup(x => x.ParseSignature("/RRContent/css.css")).Returns("sig"); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use(builder.Object); }); module.HandleRRContent(context.Object); Assert.Equal(60*24*360, context.Object.Response.Expires); cache.Verify(x => x.SetETag(@"""sig"""), Times.Once()); cache.Verify(x => x.SetCacheability(HttpCacheability.Public), Times.Once()); RRContainer.Current = null; }
public void WillNotSetCachabilityIfNotInRRPatOrStoreDoesNotSendContent(string path, bool contentSent) { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.RawUrl).Returns("/RRContent/css.css"); context.Setup(x => x.Request.Url).Returns(new Uri("http://localhost/RRContent/css.css")); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); var cache = new Mock<HttpCachePolicyBase>(); context.Setup(x => x.Response.Cache).Returns(cache.Object); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns(path); var store = new Mock<IStore>(); store.Setup( x => x.SendContent(It.IsAny<string>(), context.Object.Response)). Returns(contentSent); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use(new UriBuilder(config.Object)); }); module.HandleRRContent(context.Object); cache.Verify(x => x.SetCacheability(HttpCacheability.Public), Times.Never()); RRContainer.Current = null; }
public void WillNotHandleRequestsNotInMyDirectory() { var module = new RequestReduceModule(); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/RRContent"); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.RawUrl).Returns("/RRContents/someresource"); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); var store = new Mock<IStore>(); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use<UriBuilder>(); }); module.HandleRRContent(context.Object); store.Verify(x => x.SendContent(It.IsAny<string>(), It.IsAny<HttpResponseBase>()), Times.Never()); RRContainer.Current = null; }
public void WillHonorEtagsandReturn304WhenTheyMatchIfNoneMatch() { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.RawUrl).Returns("/RRContent/key-match-css.css"); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection() { { "If-None-Match", @"""match""" } }); context.Setup(x => x.Request.Url).Returns(new Uri("http://localhost/RRContent/key-match-css.css")); context.Setup(x => x.Response.Headers).Returns(new NameValueCollection()); context.Setup(x => x.Server).Returns(new Mock<HttpServerUtilityBase>().Object); var cache = new Mock<HttpCachePolicyBase>(); context.Setup(x => x.Response.Cache).Returns(cache.Object); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/RRContent"); var store = new Mock<IStore>(); var builder = new Mock<IUriBuilder>(); builder.Setup(x => x.ParseSignature("/RRContent/key-match-css.css")).Returns("match"); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use(builder.Object); }); module.HandleRRContent(context.Object); Assert.Equal(60 * 24 * 360, context.Object.Response.Expires); cache.Verify(x => x.SetETag(@"""match"""), Times.Once()); cache.Verify(x => x.SetCacheability(HttpCacheability.Public), Times.Once()); Assert.Equal(304, context.Object.Response.StatusCode); store.Verify(x => x.SendContent(It.IsAny<string>(), context.Object.Response), Times.Never()); RRContainer.Current = null; }
public void WillStripQueryStringDromGeneratedUrls() { var module = new RequestReduceModule(); var context = new Mock<HttpContextBase>(); context.Setup(x => x.Request.RawUrl).Returns("/RRContent/key-match-css.css?somequerystring"); context.Setup(x => x.Request.Url).Returns(new Uri("http://localhost/RRContent/key-match-css.css?somequerystring")); context.Setup(x => x.Request.Headers).Returns(new NameValueCollection() { { "If-None-Match", @"""match""" } }); context.Setup(x => x.Response.Headers).Returns(new NameValueCollection()); var cache = new Mock<HttpCachePolicyBase>(); context.Setup(x => x.Response.Cache).Returns(cache.Object); var config = new Mock<IRRConfiguration>(); config.Setup(x => x.SpriteVirtualPath).Returns("/RRContent"); var store = new Mock<IStore>(); var builder = new Mock<IUriBuilder>(); builder.Setup(x => x.ParseSignature("/RRContent/key-match-css.css")).Returns("match"); RRContainer.Current = new Container(x => { x.For<IRRConfiguration>().Use(config.Object); x.For<IHostingEnvironmentWrapper>().Use(new Mock<IHostingEnvironmentWrapper>().Object); x.For<IStore>().Use(store.Object); x.For<IUriBuilder>().Use(builder.Object); }); module.HandleRRContent(context.Object); store.Verify(x => x.SendContent("/RRContent/key-match-css.css", context.Object.Response), Times.Never()); RRContainer.Current = null; }