Beispiel #1
0
        public void should_correctly_parse_single_custom_response_rule()
        {
            // given
            const string path = "some-path";

            Stream stream = Resources.ResourceReader.StreamEmbeddedFile(Resources.ResourceReader.CustomResponseRuleWithoutConditions);

            _fileStreamer
            .Setup(x => x.ReadFile(path))
            .Returns(stream);

            // when
            var          parser = new RewriteRulesParser(_fileStreamer.Object);
            InboundRules rules  = parser.ParseInboundRules(path);

            // then
            Assert.That(rules.Rules.Length, Is.EqualTo(1));

            var rule = rules.Rules[0];

            Assert.That(rule.Name, Is.EqualTo("customy"));
            Assert.That(rule.Match.Url, Is.EqualTo("me-likey"));
            Assert.That(rule.Conditions, Is.Null);

            Assert.That(rule.Action.Type, Is.EqualTo(ActionType.CustomResponse));
            Assert.That(rule.Action.StatusCode, Is.EqualTo(403));
            Assert.That(rule.Action.StatusReason, Is.EqualTo("Unauthorised"));
            Assert.That(rule.Action.StatusDescription, Is.EqualTo("Unauthorised"));
            Assert.That(rule.Action.LogRewrittenUrl, Is.True);
        }
        public void should_return_expected_rule_given_name()
        {
            // given
            var rules = new InboundRules
            {
                Rules = new[]
                {
                    new Rule {
                        Name = "not-me"
                    },
                    new Rule {
                        Name = "not-me2"
                    },
                    new Rule {
                        Name = "not-me3"
                    },
                    new Rule {
                        Name = "me"
                    },
                }
            };

            // when
            Rule rule = rules["me"];

            // then
            Assert.That(rule, Is.EqualTo(rules.Rules[3]));
        }
        public static Rule FirstRuleToMatchUrl(this InboundRules inboundRules, string url)
        {
            foreach (Rule rule in inboundRules.Rules)
            {
                if (rule.Match.MatchesUrl(url))
                {
                    return(rule);
                }
            }

            return(null);
        }
Beispiel #4
0
        public void should_correctly_parse_single_rewrite_rule()
        {
            // given
            const string path = "i am a path";

            Stream stream = Resources.ResourceReader.StreamEmbeddedFile(Resources.ResourceReader.SingleRewriteRuleWithConditions);

            _fileStreamer
            .Setup(x => x.ReadFile(path))
            .Returns(stream);

            // when
            var          parser = new RewriteRulesParser(_fileStreamer.Object);
            InboundRules rules  = parser.ParseInboundRules(path);

            // then
            Assert.That(stream.CanRead, Is.False);
            Assert.That(rules.Rules.Length, Is.EqualTo(1));

            var rule = rules.Rules[0];

            Assert.That(rule.Name, Is.EqualTo("name-data"));
            Assert.That(rule.StopProcessing, Is.True);

            Assert.That(rule.Match.Url, Is.EqualTo("url-data"));
            Assert.That(rule.Match.IgnoreCase, Is.True);

            Assert.That(rule.Conditions.ConditionList.Length, Is.EqualTo(2));
            Assert.That(rule.Conditions.LogicalGrouping, Is.EqualTo(LogicalGroupingType.MatchAll));
            Assert.That(rule.Conditions.TrackAllCaptures, Is.False);

            Assert.That(rule.Conditions.ConditionList[0].Input, Is.EqualTo("input-data"));
            Assert.That(rule.Conditions.ConditionList[0].Pattern, Is.EqualTo("pattern-data"));
            Assert.That(rule.Conditions.ConditionList[0].Negate, Is.True);
            Assert.That(rule.Conditions.ConditionList[0].IgnoreCase, Is.True);

            Assert.That(rule.Conditions.ConditionList[1].Input, Is.EqualTo("more-input-data"));
            Assert.That(rule.Conditions.ConditionList[1].Pattern, Is.EqualTo("more-pattern-data"));
            Assert.That(rule.Conditions.ConditionList[1].Negate, Is.False);
            Assert.That(rule.Conditions.ConditionList[1].IgnoreCase, Is.False);

            Assert.That(rule.Action.Type, Is.EqualTo(ActionType.Rewrite));
            Assert.That(rule.Action.Url, Is.EqualTo("url-data"));
            Assert.That(rule.Action.AppendQueryString, Is.True);
        }
Beispiel #5
0
        public void should_correctly_parse_multiple_rules()
        {
            // given
            const string path = "some-path";

            Stream stream = Resources.ResourceReader.StreamEmbeddedFile(Resources.ResourceReader.MultipleRules);

            _fileStreamer
            .Setup(x => x.ReadFile(path))
            .Returns(stream);

            // when
            var          parser = new RewriteRulesParser(_fileStreamer.Object);
            InboundRules rules  = parser.ParseInboundRules(path);

            // then
            Assert.That(rules.Rules.Length, Is.EqualTo(3));
        }
        public void should_return_expected_rule_given_index()
        {
            // given
            var rules = new InboundRules
            {
                Rules = new[]
                {
                    new Rule(),
                    new Rule(),
                    new Rule(),
                    new Rule()
                }
            };

            // when
            Rule rule = rules[3];

            // then
            Assert.That(rule, Is.EqualTo(rules.Rules[3]));
        }
Beispiel #7
0
        public void should_correctly_parse_single_redirect_rule()
        {
            // given
            const string path = "a different path";

            Stream stream = Resources.ResourceReader.StreamEmbeddedFile(Resources.ResourceReader.SingleRedirectRuleWithoutConditions);

            _fileStreamer
            .Setup(x => x.ReadFile(path))
            .Returns(stream);

            // when
            var          parser = new RewriteRulesParser(_fileStreamer.Object);
            InboundRules rules  = parser.ParseInboundRules(path);

            // then
            Assert.That(rules.Rules.Length, Is.EqualTo(1));

            var rule = rules.Rules[0];

            Assert.That(rule.Name, Is.EqualTo("redirecty"));
            Assert.That(rule.StopProcessing, Is.False);

            Assert.That(rule.Match.Url, Is.EqualTo("url-stuffz"));
            Assert.That(rule.Match.IgnoreCase, Is.False);

            Assert.That(rule.Conditions.ConditionList, Is.Null);
            Assert.That(rule.Conditions.LogicalGrouping, Is.EqualTo(LogicalGroupingType.MatchAny));
            Assert.That(rule.Conditions.TrackAllCaptures, Is.True);

            Assert.That(rule.ServerVariables.ServerVariablesList.Length, Is.EqualTo(1));
            Assert.That(rule.ServerVariables.ServerVariablesList[0].Name, Is.EqualTo("namey"));
            Assert.That(rule.ServerVariables.ServerVariablesList[0].Value, Is.EqualTo("valuey"));

            Assert.That(rule.Action.Type, Is.EqualTo(ActionType.Redirect));
            Assert.That(rule.Action.Url, Is.EqualTo("some-url"));
            Assert.That(rule.Action.AppendQueryString, Is.False);
            Assert.That(rule.Action.RedirectType, Is.EqualTo(RedirectType.Temporary));
        }
        public void should_return_null_if_nothing_is_matched()
        {
            // given
            var rules = new InboundRules
            {
                Rules = new[]
                {
                    new Rule {
                        Match = new Match {
                            Url = "^wrong-url$", IgnoreCase = true
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^another-wrong-url$", IgnoreCase = true
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^correct-URL$", IgnoreCase = false
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^correct-url$", IgnoreCase = true
                        }
                    },
                }
            };

            // when
            Rule rule = rules.FirstRuleToMatchUrl("NOPE");

            // then
            Assert.That(rule, Is.Null);
        }
        public void should_return_expected_rule_when_url_matches()
        {
            // given
            var rules = new InboundRules
            {
                Rules = new[]
                {
                    new Rule {
                        Match = new Match {
                            Url = "^wrong-url$", IgnoreCase = true
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^another-wrong-url$", IgnoreCase = true
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^correct-URL$", IgnoreCase = false
                        }
                    },
                    new Rule {
                        Match = new Match {
                            Url = "^correct-url$", IgnoreCase = true
                        }
                    },
                }
            };

            // when
            Rule rule = rules.FirstRuleToMatchUrl("correct-url");

            // then
            Assert.That(rule, Is.EqualTo(rules.Rules[3]));
        }