public void CreateScriptVersionsServiceUseMinificationTest()
        {
            var service = new ScriptVersionsService(_scriptVersionsFile,
                                                    new ScriptVersionsServiceOptions()
            {
                UseMinified = true
            });

            // should always return minified file version and minified file URL when UseMinified set to "true"
            Assert.Equal("3", service.GetVersion("scripts/vendor.js"));
            Assert.Equal("3", service.GetVersion("scripts/vendor.min.js"));
            Assert.Equal("scripts/vendor.min.js?v=3", service.GetVersionedUrl("scripts/vendor.js"));
            Assert.Equal("scripts/vendor.min.js?v=3", service.GetVersionedUrl("scripts/vendor.min.js"));

            // but, a script without a minified version should return the normal version
            Assert.Equal("/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js"));
        }
        public void CreateScriptVersionsServiceWithBaseUrlTest()
        {
            var service = new ScriptVersionsService(_scriptVersionsFile,
                                                    new ScriptVersionsServiceOptions()
            {
                BaseUrl = "//foo.com/"
            });

            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js"));

            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js", "apppath"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js", "apppath")); // should ignore relative path
            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "/apppath"));
            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "/apppath/"));

            service = new ScriptVersionsService(_scriptVersionsFile,
                                                new ScriptVersionsServiceOptions()
            {
                BaseUrl = "//foo.com"     // removed trailing /
            });

            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js"));

            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js", "apppath"));
            Assert.Equal("//foo.com/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js", "apppath")); // should ignore relative path
            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "/apppath"));
            Assert.Equal("//foo.com/apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "/apppath/"));
        }
        public void CreateScriptVersionsServiceCusomParamNameTest()
        {
            var service = new ScriptVersionsService(_scriptVersionsFile,
                                                    new ScriptVersionsServiceOptions()
            {
                VersionParamName = "_version"
            });

            Assert.Equal("scripts/main.js?_version=5", service.GetVersionedUrl("scripts/main.js"));
        }
        public void CreateScriptVersionsServiceWithCustomOptionsTest()
        {
            var service = new ScriptVersionsService(_scriptVersionsFile,
                                                    new ScriptVersionsServiceOptions()
            {
                BaseUrl          = "//foo.com/",
                UseMinified      = true,
                VersionParamName = "ver",
                ScriptFileTypes  = new [] { "js" }
            });

            Assert.Equal("//foo.com/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("scripts/vendor.js"));
            Assert.Equal("//foo.com/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("/scripts/vendor.js"));
            Assert.Equal("//foo.com/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("~/scripts/vendor.min.js"));

            Assert.Equal("//foo.com/apppath/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("scripts/vendor.js", "apppath"));
            Assert.Equal("//foo.com/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("/scripts/vendor.js", "apppath")); // should ignore relative path
            Assert.Equal("//foo.com/apppath/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("~/scripts/vendor.js", "/apppath"));
            Assert.Equal("//foo.com/apppath/scripts/vendor.min.js?ver=3", service.GetVersionedUrl("~/scripts/vendor.min.js", "/apppath/"));
        }
        public void CreateScriptVersionsServiceWithDefaultOptionsTest()
        {
            var service = new ScriptVersionsService(_scriptVersionsFile);

            Assert.Null(service.GetVersion(null));
            Assert.Null(service.GetVersion(string.Empty));
            Assert.Null(service.GetVersion("scripts/doesntexist.js"));
            Assert.Equal("5", service.GetVersion("scripts/main.js"));
            Assert.Equal("2", service.GetVersion("scripts/vendor.js"));
            Assert.Equal("3", service.GetVersion("scripts/vendor.min.js"));

            Assert.Equal("5", service.GetVersion("/scripts/main.js"));
            Assert.Equal("5", service.GetVersion("~/scripts/main.js"));

            Assert.Null(service.GetVersion("https://somewhere.com/scripts/main.js"));
            Assert.Null(service.GetVersion("//somewhere.com/scripts/main.js"));

            Assert.Equal("scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js"));
            Assert.Equal("scripts/vendor.js?v=2", service.GetVersionedUrl("scripts/vendor.js"));
            Assert.Equal("scripts/vendor.min.js?v=3", service.GetVersionedUrl("scripts/vendor.min.js"));

            Assert.Equal("/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js"));
            Assert.Equal("scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js"));

            Assert.Equal("scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", null));
            Assert.Equal("apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "apppath"));
            Assert.Equal("apppath/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js", "apppath"));
            Assert.Equal("apppath/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js", "apppath/"));
            Assert.Equal("/apppath/scripts/main.js?v=5", service.GetVersionedUrl("scripts/main.js", "/apppath/"));
            Assert.Equal("tenant1/apppath/scripts/main.js?v=5", service.GetVersionedUrl("~/scripts/main.js", "tenant1/apppath"));

            // relative path should actually be ignored in this case since path starts with a "/"
            Assert.Equal("/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js", "apppath"));
            Assert.Equal("/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js", null));
            Assert.Equal("/scripts/main.js?v=5", service.GetVersionedUrl("/scripts/main.js", string.Empty));
        }