//TODO all these tests could be moved into this...
        public static void BuildAndTestConditions(IEnumerable<string> lines, string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl, int? expectedRuleCount = null)
        {
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));
            var ruleSet = RewriteRuleSet.BuildRuleSets(redirects);
            var redirectData = new RedirectData(originalUrl);

            TestConditions(matchesCond, matchesRule, null, expectedUrl, ruleSet, redirectData);
        }
        public void ltl_freight_full_file(string originalUrl, string expectedUrl)
        {
            //Arrange
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            var tester = new RewriteTester(factory);

            var configFile = @"...\httpd.conf";

            tester.LoadConfig(configFile);

            //Act
            var output = tester.TestUrl(originalUrl);

            //Assert
            output.ProcessedUrl.Should().Be(expectedUrl);
            output.RuleSetMatched.Rules.First().ReplacePattern.Should().Contain("vortal");
        }
        public void Https_test_cases(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl)
        {
            //Arrange
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            string[] lines =
            {
                @"# Force SSL for pages that request personally-identifiable information",
                @"RewriteCond %{HTTPS} ^(?!on).*$",
                @"RewriteCond %{SERVER_PORT} ^80$",
                @"RewriteCond %{HTTP:Host} (.*)",
                @"RewriteRule ^(/ltl-freight)(.*)$ https\://%1$1 [NC,R=301]",
            };
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));
            var ruleSet = RewriteRuleSet.BuildRuleSets(redirects);
            var redirectData = new RedirectData(originalUrl);

            TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData);
        }
        public void Pricing_index_page_urls(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl)
        {
            //Arrange
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            string[] lines =
                {
                    @"RewriteCond %{HTTP:Host} ^directory[2]?\.uship\.com$",
                    @"RewriteRule ^/tips/showtip.aspx(.*)$ http://www.uship.com/tips/showtip.aspx$1 [NC,R=301]",
                    @"",
                    @"# ...",
                    @"",
                    @"# Listing Index",
                    @"RewriteRule ^/pricing/((?:[a-z]|[-])+)/((?:[a-z]|[-])+)(?:/page/([0-9]+))? /listingindex/PricingCommodity.aspx?c=$1&c2=$2&page=$3 [NC,L]",
                    @"RewriteRule ^/pricing/((?:[a-z]|[-])+)(?:/page/([0-9]+))? /listingindex/PricingCommodity.aspx?c=$1&page=$2 [NC,L]",
                    @"RewriteRule ^/pricing/?$ /listingindex/?c=4&c2=79 [NC,L]"
                };
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));
            var ruleSet = RewriteRuleSet.BuildRuleSets(redirects);
            var redirectData = new RedirectData(originalUrl);

            TestConditions(matchesCond, matchesRule, 3, expectedUrl, ruleSet, redirectData);
        }
        public void Single_ruleset_should_manually_work_as_expected(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl)
        {
            //Arrange
            //TODO use structure map for getting the instance???:
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            string[] lines =
                {
                    @"RewriteCond %{HTTP:Host} ^(movers|household-goods|vehicles|boats|motorcycles|special-care|freight|pets-livestock|food-agriculture|junk|craigslist)[2]?\.uship\.com$",
                    @"RewriteRule ^(?!.+\.axd|.+\.ashx|public/images|sticky/images)/([^?]*\u.*)/?(?:[^?]*\u.*)?$ /$1 [CL,R=301]"
                };
            var redirectData = new RedirectData(originalUrl);

            //Act
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));

            //Assert
            var redirectLines = redirects as IList<IRedirectLine> ?? redirects.ToList();
            redirectLines.Count().Should().Be(2);

            var cond = (RewriteCondition) redirectLines.Single(x => x.LineType == RedirectLineType.Condition);
            cond.Variable.Should().Be("HTTP:Host");
            cond.MatchesCondition(ref redirectData).Should().Be(matchesCond);
            if (matchesCond)
            {
                var rule = (RewriteRule) redirectLines.Single(x => x.LineType == RedirectLineType.Rule);
                redirectData = rule.ProcessRule(redirectData);
                if (matchesRule)
                {
                    redirectData.Status.Should().NotBe(RedirectStatus.NotProcessed);
                    redirectData.ProcessedUrl.Should().Be(expectedUrl);
                }
                else
                {
                    redirectData.Status.Should().Be(RedirectStatus.NotProcessed);
                }
            }
        }
        public void Vortal_rewrites_alone_should_go_to_correct_domain(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl)
        {
            //Arrange
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            string[] lines =
                {
                    @"RewriteCond %{HTTP:Host} ^(movers|vehicles|boats|motorcycles|freight)\.uship\.com$",
                    @"RewriteRule ^/(.+)$ http://www.uship.com/%1/$1 [CL,NC,R=301]",
                    @"",
                    @"RewriteCond %{HTTP:Host} ^pets-livestock\.uship\.com$",
                    @"RewriteRule ^/(.+)$ http://www.uship.com/pet-shipping/$1 [CL,NC,R=301]",
                    @"",
                    @"RewriteCond %{HTTP:Host} ^household-goods\.uship\.com$",
                    @"RewriteRule ^/(.+)$ http://www.uship.com/furniture/$1 [CL,NC,R=301]",
                    @"",
                    @"# 301 these lortals to their vortals, these lortals will not exist",
                    @"RewriteCond %{HTTP:Host} ^(craigslist|special-care|junk|food)(?:-agriculture)?\.uship\.com$",
                    @"RewriteRule ^/(.+)$ http://www.uship.com/%1/ [CL,NC,R=301]"
                };
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));
            var ruleSet = RewriteRuleSet.BuildRuleSets(redirects);
            var redirectData = new RedirectData(originalUrl);

            TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData);
        }
        public void Single_ruleset_should_object_work_as_expected(string originalUrl, bool matchesCond, bool matchesRule, string expectedUrl)
        {
            //Arrange
            var samples = new RandomSampleValues();
            var factory = new RewriteFactory(samples);
            string[] lines =
                {
                    @"RewriteCond %{HTTP:Host} ^(movers|household-goods|vehicles|boats|motorcycles|special-care|freight|pets-livestock|food-agriculture|junk|craigslist)[2]?\.uship\.com$",
                    @"RewriteRule ^(?!.+\.axd|.+\.ashx|public/images|sticky/images)/([^?]*\u.*)/?(?:[^?]*\u.*)?$ /$1 [CL,R=301]"
                };
            var lineNum = 1;
            var redirects = lines.Select(l => factory.Build(lineNum++, l));
            var ruleSet = RewriteRuleSet.BuildRuleSets(redirects);
            var redirectData = new RedirectData(originalUrl);

            TestConditions(matchesCond, matchesRule, 1, expectedUrl, ruleSet, redirectData);
        }