Ejemplo n.º 1
0
        public void CanAppendFileHashToAbsoluteUri()
        {
            var hashQueryStringKeyName = "v";
            var hashValue        = "hashValue";
            var hasher           = new StubHasher(hashValue);
            var uri              = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath      = Path.Combine(Path.GetDirectoryName(uri), @"output.css");
            var url              = "/" + Path.GetFileName(uri);
            var pathToResolveTo  = Assembly.GetExecutingAssembly().Location;
            var fileResolver     = StubResolver.ForFile(pathToResolveTo);
            var assetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "?" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
Ejemplo n.º 2
0
        public void CanAppendFileHashToAbsoluteUriWithAnExistingQueryString()
        {
            var hashQueryStringKeyName = "v";
            var hashValue       = "hashValue";
            var hasher          = new StubHasher(hashValue);
            var uri             = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath     = Path.GetDirectoryName(uri) + @"\output.css";
            var url             = "/" + Path.GetFileName(uri) + "?test=value";
            var pathToResolveTo = Assembly.GetExecutingAssembly().Location;
            var fileResolver    = StubResolver.ForFile(pathToResolveTo);
            var pathTranslator  = new PathTranslator();

            var assetsFileHasher = new CSSAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher, pathTranslator);

            var expectedUrl = url + "&" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
Ejemplo n.º 3
0
        public void CanBundleJavascriptInRelease(Type preprocessorType)
        {
            var preprocessor = Activator.CreateInstance(preprocessorType) as IPreprocessor;

            const string template         = "<h1>{{message}}</h1>";
            var          templateFileName = "test.hogan.html";
            var          resolver         = StubResolver.ForFile(TestUtilities.PrepareRelativePath(templateFileName));

            var readerFactory = new StubFileReaderFactory();

            readerFactory.SetContentsForFile(TestUtilities.PrepareRelativePath(templateFileName), template);

            var writerFactory = new StubFileWriterFactory();

            string tag;

            using (new ResolverFactoryScope(typeof(FileSystemResolver).FullName, resolver))
            {
                tag = javaScriptBundleFactory
                      .WithFileReaderFactory(readerFactory)
                      .WithFileWriterFactory(writerFactory)
                      .WithDebuggingEnabled(false)
                      .Create()
                      .WithPreprocessor(preprocessor)
                      .Add("~/" + templateFileName)
                      .Render("~/template.js");
            }

            //are minifier's optimizations here OK?
            var compiled =
                @"var JST=JST||{};JST.test=new Hogan.Template(function(n,t,i){var r=this;return r.b(i=i||""""),r.b(""<h1>""),r.b(r.v(r.f(""message"",n,t,0))),r.b(""<\/h1>""),r.fl()},""" + template.Replace("/", @"\/") + "\",Hogan,{});";

            Assert.AreEqual(1, writerFactory.Files.Count);
            var expectedTag = "<script type=\"text/javascript\" src=\"template.js?r=hash\"></script>";

            Assert.AreEqual(expectedTag, TestUtilities.NormalizeLineEndings(tag));

            var actual = writerFactory.Files[TestUtilities.PrepareRelativePath("template.js")];

            Assert.AreEqual(compiled + "\n", actual);
        }
Ejemplo n.º 4
0
        public void CanBundleJavascriptInDebug(Type preprocessorType)
        {
            var preprocessor = Activator.CreateInstance(preprocessorType) as IPreprocessor;

            const string template         = "<h1>{{message}}</h1>";
            var          templateFileName = "test.hogan.html";
            var          resolver         = StubResolver.ForFile(TestUtilities.PrepareRelativePath(templateFileName));

            var readerFactory = new StubFileReaderFactory();

            readerFactory.SetContentsForFile(TestUtilities.PrepareRelativePath(templateFileName), template);

            var writerFactory = new StubFileWriterFactory();

            string tag;

            using (new ResolverFactoryScope(typeof(FileSystemResolver).FullName, resolver))
            {
                tag = javaScriptBundleFactory
                      .WithFileReaderFactory(readerFactory)
                      .WithFileWriterFactory(writerFactory)
                      .WithDebuggingEnabled(true)
                      .Create()
                      .WithPreprocessor(preprocessor)
                      .Add("~/" + templateFileName)
                      .Render("~/template.js");
            }

            var sb = new StringBuilder();

            sb.AppendLine(@"var JST = JST || {};");
            sb.AppendLine(@"JST['test'] = new Hogan.Template(function(c,p,i){var _=this;_.b(i=i||"""");_.b(""<h1>"");_.b(_.v(_.f(""message"",c,p,0)));_.b(""</h1>"");return _.fl();;},""" + template + "\",Hogan,{});");
            var compiled = sb.ToString();

            Assert.AreEqual(1, writerFactory.Files.Count);
            var expectedTag = "<script type=\"text/javascript\" src=\"test.hogan.html.squishit.debug.js\"></script>\n";

            Assert.AreEqual(expectedTag, TestUtilities.NormalizeLineEndings(tag));

            Assert.AreEqual(compiled, writerFactory.Files[TestUtilities.PrepareRelativePath("test.hogan.html.squishit.debug.js")]);
        }
Ejemplo n.º 5
0
        public void CanBundleCssWithRemote()
        {
            //this is rendering tag correctly but incorrectly(?) merging both files
            using (new ResolverFactoryScope(typeof(Framework.Resolvers.HttpResolver).FullName, StubResolver.ForFile("http://www.someurl.com/css/first.css")))
            {
                CSSBundle cssBundle = cssBundleFactory
                                      .WithHasher(hasher)
                                      .WithDebuggingEnabled(false)
                                      .WithContents(css)
                                      .Create();

                string tag = cssBundle
                             .AddRemote("/css/first.css", "http://www.someurl.com/css/first.css")
                             .Add("/css/second.css")
                             .Render("/css/output_remote.css");
                Assert.AreEqual("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.someurl.com/css/first.css\" /><link rel=\"stylesheet\" type=\"text/css\" href=\"/css/output_remote.css?r=67F81278D746D60E6F711B5A29747388\" />", tag);
                Assert.AreEqual(1, cssBundleFactory.FileWriterFactory.Files.Count);
                Assert.AreEqual("li{margin-bottom:.1em;margin-left:0;margin-top:.1em}th{font-weight:normal;vertical-align:bottom}.FloatRight{float:right}.FloatLeft{float:left}", cssBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"css\output_remote.css")]);
            }
        }
Ejemplo n.º 6
0
        public void RenderRelease_OmitsRenderedTag_IfOnlyRemoteAssets()
        {
            //this is rendering tag correctly but incorrectly(?) merging both files
            using (new ResolverFactoryScope(typeof(Framework.Resolvers.HttpResolver).FullName, StubResolver.ForFile("http://www.someurl.com/css/first.css")))
            {
                string tag = javaScriptBundle
                             .ForceRelease()
                             .AddRemote("/css/first.js", "http://www.someurl.com/js/first.js")
                             .Render("/css/output_remote.js");

                Assert.AreEqual("<script type=\"text/javascript\" src=\"http://www.someurl.com/js/first.js\"></script>", tag);
                Assert.AreEqual(0, fileWriterFactory.Files.Count);
            }
        }