public void WhiteSpacePath_Throws()
        {
            var mockHttp = Mock.Of<HttpContextBase>();
            var file = new BasicFile(ClientDependencyType.Javascript) { FilePath = "  ", };

            Assert.Throws<ArgumentException>(() => file.ResolveFilePath(mockHttp));
        }
        public void AbsolutePath_IsUnaltered()
        {
            var mockHttp = Mock.Of<HttpContextBase>();

            var file = new BasicFile(ClientDependencyType.Javascript) { FilePath = "/file.js", };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/file.js", resolvedPath);
        }
        public void RelativePath_HasCurrentExecutionPathPrefixed()
        {
            var mockHttp = Mock.Of<HttpContextBase>();
            Mock.Get(mockHttp).DefaultValue = DefaultValue.Mock;
            Mock.Get(mockHttp.Request).Setup(r => r.AppRelativeCurrentExecutionFilePath).Returns("/the/path/page.aspx");

            var file = new BasicFile(ClientDependencyType.Javascript) { FilePath = "file.js", };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/the/path/file.js", resolvedPath);
        }
        public void TildePath_IsResolved()
        {
            var mockHttp = Mock.Of<HttpContextBase>();
            Mock.Get(mockHttp).DefaultValue = DefaultValue.Mock;
            Mock.Get(mockHttp.Request).Setup(r => r.ApplicationPath).Returns("/");

            var file = new BasicFile(ClientDependencyType.Javascript) { FilePath = "~/file.js", };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/file.js", resolvedPath);
        }
        public void NonCanonicalAbsolutePath_IsCanonicalized()
        {
            var mockHttp = Mock.Of<HttpContextBase>();
            var file = new BasicFile(ClientDependencyType.Javascript) { FilePath = "/website/folder/../js.js", };

            var resolvedPath = file.ResolveFilePath(mockHttp);

            Assert.AreEqual("/website/js.js", resolvedPath);
        }
Esempio n. 6
0
        /// <summary>
        /// Replaces the content with the new js/css paths
        /// </summary>
        /// <param name="html"></param>
        /// <param name="namedGroup"></param>
        /// <param name="extensions"></param>
        /// <param name="type"></param>
        /// <param name="regex"></param>
        /// <param name="http"></param>
        /// <returns></returns>
        /// <remarks>
        /// For some reason ampersands that aren't html escaped are not compliant to HTML standards when they exist in 'link' or 'script' tags in URLs,
        /// we need to replace the ampersands with &amp; . This is only required for this one w3c compliancy, the URL itself is a valid URL.
        /// </remarks>
        private static string ReplaceContent(string html, string namedGroup, string[] extensions,
            ClientDependencyType type, string regex, HttpContextBase http)
        {
            html = Regex.Replace(html, regex,
                (m) =>
                {
                    var grp = m.Groups[namedGroup];

                    //if there is no namedGroup group name or it doesn't end with a js/css extension or it's already using the composite handler,
                    //the return the existing string.
                    if (grp == null
                        || string.IsNullOrEmpty(grp.ToString())
                        || !grp.ToString().EndsWithOneOf(extensions)
                        || grp.ToString().StartsWith(ClientDependencySettings.Instance.CompositeFileHandlerPath))
                        return m.ToString();

                    
                    //make sure that it's an internal request, though we can deal with external 
                    //requests, we'll leave that up to the developer to register an external request
                    //explicitly if they want to include in the composite scripts.
                    try
                    {
                        var url = new Uri(grp.ToString(), UriKind.RelativeOrAbsolute);
                        if (!url.IsLocalUri(http))
                            return m.ToString(); //not a local uri        
                        else
                        {
                           
                            var dependency = new BasicFile(type) { FilePath = grp.ToString() };

                            var file = new[] { new BasicFile(type) { FilePath = dependency.ResolveFilePath(http) } };

                            var resolved = ClientDependencySettings.Instance.DefaultCompositeFileProcessingProvider.ProcessCompositeList(
                                file,
                                type,
								http).Single();

                            return m.ToString().Replace(grp.ToString(), resolved.Replace("&", "&amp;"));
                        }
                    }
                    catch (UriFormatException)
                    {
                        //malformed url, let's exit
                        return m.ToString();
                    }

                },
                RegexOptions.Compiled);

            return html;
        }