Esempio n. 1
0
        public void ForceHostToUseWww()
        {
            var target = CreateRuleSet(@"
RewriteCond %{HTTP_HOST} !^(www).*$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301]");

            var url     = new Uri("http://somesite.com/pass");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "HTTP_HOST", url.GetComponents(UriComponents.Host, UriFormat.SafeUnescaped) }
            });

            string expected  = "http://www.somesite.com/pass";
            Uri    resultUrl = target.RunRules(context, url);
            string result    = context.Response.RedirectLocation;

            Assert.IsNull(resultUrl);
            Assert.AreEqual(expected, result);

            url     = new Uri("http://www.somesite.com/fail");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "HTTP_HOST", url.GetComponents(UriComponents.Host, UriFormat.SafeUnescaped) }
            });

            resultUrl = target.RunRules(context, url);
            result    = context.Response.RedirectLocation;

            Assert.IsNull(resultUrl);
            Assert.IsNull(result);
        }
Esempio n. 2
0
        public void WordPress()
        {
            var target = CreateRuleSet(@"
RewriteRule . /index.php [L]");

            var url     = new Uri("http://somesite.com/");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_FILENAME", @"D:\Hosting\4905925\html" }
            });

            Uri expected = new Uri("http://somesite.com/index.php");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);

            url     = new Uri("http://somesite.com/2007/10/start-of-something-big/");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_FILENAME", @"D:\Hosting\4905925\html\2007\10\start-of-something-big\" }
            });

            expected = new Uri("http://somesite.com/index.php");
            result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
        public void SimpleRule()
        {
            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);
            var target  = CreateRuleSet(@"RewriteRule ^/([a-z]+)\.aspx  /$1 []");

            Uri expected = new Uri("http://www.somesite.com/test");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
        public void RewriteModule_IRuleAction()
        {
            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);
            var target  = CreateRuleSet(@"
RewriteModule PostQueryString  ManagedFusion.Rewriter.Tests.TestRuleAction,  ManagedFusion.Rewriter.Tests
RewriteRule(,PostQueryString) ^(.*)$  $1");

            Uri expected = new Uri("http://www.somesite.com/pass");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
        public void SimpleRule_WithNoSubstituion()
        {
            var target = CreateRuleSet(@"
RewriteRule ^.* - [F,L]");

            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            Uri expected = null;             // no change
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
        public void RewriteCondWithHttpHeader()
        {
            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetHeaders(new Dictionary <string, string> {
                { "Accept", "text/plain" }
            });
            var target = CreateRuleSet(@"
RewriteCond %{HTTP:Accept} ^text.*$ [NC]
RewriteRule ^/([a-z]+)\.aspx  /$1.txt []");

            Uri expected = new Uri("http://www.somesite.com/test.txt");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
        public void RewriteCondWithServerVariable()
        {
            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });
            var target = CreateRuleSet(@"
RewriteCond %{REQUEST_URI} ^/test\.aspx.*$ [NC]
RewriteRule ^/([a-z]+)\.aspx  /$1 []");

            Uri expected = new Uri("http://www.somesite.com/test");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
Esempio n. 8
0
        public void AddQueryStringParameterOnToUrlThatContainsQueryString()
        {
            var target = CreateRuleSet(@"
RewriteRule ^/(.*)$  /$1?test=added [QSA]");

            var url     = new Uri("http://somesite.com/pass?test=1&test=2&test=3");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "QUERY_STRING", "test=1&test=2&test=3" },
                { "HTTP_HOST", "somesite.com" }
            });

            Uri expected = new Uri("http://somesite.com/pass?test=added&test=1&test=2&test=3");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
Esempio n. 9
0
        public void VariableDoubleProcessingNotAllowed()
        {
            var target = CreateRuleSet(@"
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^/test.aspx$ /pass/%2/ [L]");

            var url     = new Uri("http://somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "QUERY_STRING", "id=%{HTTP_HOST}" },
                { "HTTP_HOST", "somesite.com" }
            });

            Uri expected = new Uri("http://somesite.com/pass/%{HTTP_HOST}/");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);
        }
Esempio n. 10
0
        public void Replace_ServerVariable()
        {
            var url         = new Uri("http://www.somesite.com/test.aspx");
            var httpContext = HttpHelpers.MockHttpContext(url);

            httpContext.Request.SetServerVariables(new Dictionary <string, string> {
                { "SERVER_PORT", "1234" }
            });
            var          context     = CreateRuleContext(url, httpContext);
            string       pattern     = "^(.*)$";
            string       input       = "/test.aspx";
            string       replacement = "/pass?port=%{SERVER_PORT}";
            RegexOptions options     = Manager.RuleOptions;
            Pattern      target      = new Pattern(pattern, options);

            string result   = Pattern.Replace(replacement, context);
            string expected = "/pass?port=1234";

            Assert.AreEqual(expected, result);
        }
Esempio n. 11
0
        public void AddTrailingSlash()
        {
            var target = CreateRuleSet(@"
RewriteRule ^([^.?]+[^.?/])$  $1/ [R=301,L]");

            var url     = new Uri("http://somesite.com/pass");
            var context = HttpHelpers.MockHttpContext(url);

            string expected  = "http://somesite.com/pass/";
            Uri    resultUrl = target.RunRules(context, url);
            string result    = context.Response.RedirectLocation;

            Assert.IsNull(resultUrl);
            Assert.AreEqual(expected, result);

            url     = new Uri("http://somesite.com/fail/");
            context = HttpHelpers.MockHttpContext(url);

            resultUrl = target.RunRules(context, url);
            result    = context.Response.RedirectLocation;

            Assert.IsNull(resultUrl);
            Assert.IsNull(result);
        }
Esempio n. 12
0
        /// <summary>
        /// Creates the rule execution context.
        /// </summary>
        /// <param name="requestedUrl"></param>
        /// <returns></returns>
        public RuleContext CreateRuleContext(Uri requestedUrl)
        {
            var httpContext = HttpHelpers.MockHttpContext(requestedUrl);

            return(CreateRuleContext(requestedUrl, httpContext));
        }
Esempio n. 13
0
        /// <summary>
        /// Creates the rule execution context.
        /// </summary>
        /// <param name="requestedUrl"></param>
        /// <returns></returns>
        public RuleContext CreateOutputRuleContext(Uri requestedUrl, byte[] responseContent)
        {
            var httpContext = HttpHelpers.MockHttpContext(requestedUrl);

            return(CreateOutputRuleContext(responseContent, httpContext));
        }
        public void SimpleRule_WithComplexCondition()
        {
            var target = CreateRuleSet(@"
RewriteCond %{REQUEST_URI} ^/[a-z]+\.aspx.*$ [NC]
RewriteCond %{REQUEST_URI} ^/test\.aspx.*$ [NC, OR]
RewriteCond %{REQUEST_URI} ^/pass\.aspx.*$ [NC, OR]
RewriteCond %{REQUEST_URI} ^/[a-z]{4,4}\.aspx.*$ [NC]
RewriteCond %{REQUEST_URI} !^/fail\.aspx.*$ [NC]
RewriteRule ^/([a-z]+)\.aspx  /$1 []");

            var url     = new Uri("http://www.somesite.com/test.aspx");
            var context = HttpHelpers.MockHttpContext(url);

            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            Uri expected = new Uri("http://www.somesite.com/test");
            Uri result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);

            url     = new Uri("http://www.somesite.com/pass.aspx");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            expected = new Uri("http://www.somesite.com/pass");
            result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);

            url     = new Uri("http://www.somesite.com/nick.aspx");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            expected = new Uri("http://www.somesite.com/nick");
            result   = target.RunRules(context, url);

            Assert.AreEqual(expected, result);

            url     = new Uri("http://www.somesite.com/fail.aspx");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            expected = new Uri("http://www.somesite.com/fail");
            result   = target.RunRules(context, url);

            Assert.AreNotEqual(expected, result);

            url     = new Uri("http://www.somesite.com/1234.aspx");
            context = HttpHelpers.MockHttpContext(url);
            context.Request.SetServerVariables(new Dictionary <string, string> {
                { "REQUEST_URI", url.GetComponents(UriComponents.PathAndQuery, UriFormat.SafeUnescaped) }
            });

            expected = new Uri("http://www.somesite.com/1234");
            result   = target.RunRules(context, url);

            Assert.AreNotEqual(expected, result);
        }