Beispiel #1
0
        public void ShouldReplaceServerVariables()
        {
            bool stopProcessing;
            bool endRequest;

            _replaceAction.Initialize(Scope.ServerVariable, "CHANNEL_PATH", _valueGetter);
            _replaceAction.PerformAction(_request1, _ruleResult, out stopProcessing, out endRequest);

            Assert.AreEqual("NewValue", _request1.GetServerVariable("CHANNEL_PATH"));
        }
Beispiel #2
0
        public override void PerformAction(
            IRequestInfo requestInfo,
            IRuleResult ruleResult,
            out bool stopProcessing,
            out bool endRequest)
        {
            var value = _valueGetter.GetString(requestInfo, ruleResult);

            switch (_scope)
            {
            case Scope.Url:
                requestInfo.NewUrlString = requestInfo.NewUrlString + value;
                break;

            case Scope.Path:
                requestInfo.NewPathString = requestInfo.NewPathString
                                            + (requestInfo.NewPathString.EndsWith("/") ? "" : "/") + value;
                break;

            case Scope.QueryString:
                if (!string.IsNullOrWhiteSpace(value))
                {
                    requestInfo.NewParametersString = requestInfo.NewParametersString
                                                      + (requestInfo.NewParametersString.Length > 0 ? "&" : "") + value;
                }
                break;

            case Scope.Header:
                requestInfo.SetHeader(_scopeIndex, requestInfo.GetHeader(_scopeIndex) + value);
                break;

            case Scope.Parameter:
                requestInfo.NewParameters[_scopeIndex].Add(value);
                requestInfo.ParametersChanged();
                break;

            case Scope.PathElement:
            {
                var count = requestInfo.NewPath.Count;
                if (string.IsNullOrEmpty(requestInfo.NewPath[count - 1]))
                {
                    count--;
                }
                if (_scopeIndexValue > 0)
                {
                    if (_scopeIndexValue < count)
                    {
                        requestInfo.NewPath[_scopeIndexValue] = requestInfo.NewPath[_scopeIndexValue] + value;
                        requestInfo.PathChanged();
                    }
                }
                else
                {
                    var index = count + _scopeIndexValue;
                    if (index > 0)
                    {
                        requestInfo.NewPath[index] = requestInfo.NewPath[index] + value;
                        requestInfo.PathChanged();
                    }
                }
                requestInfo.PathChanged();
                break;
            }

            case Scope.ServerVariable:
                requestInfo.SetServerVariable(_scopeIndex, requestInfo.GetServerVariable(_scopeIndex) + value);
                break;
            }

            stopProcessing = _stopProcessing;
            endRequest     = _endRequest;
        }
        public void ShouldGetVariousScopesAsStrings()
        {
            _valueGetter.Initialize(Scope.OriginalUrl);
            Assert.AreEqual(_request1.OriginalUrlString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.OriginalUrlString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.OriginalUrlString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.Url);
            Assert.AreEqual(_request1.NewUrlString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.NewUrlString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.NewUrlString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalPath);
            Assert.AreEqual(_request1.OriginalPathString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.OriginalPathString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.OriginalPathString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.Path);
            Assert.AreEqual(_request1.NewPathString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.NewPathString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.NewPathString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalQueryString);
            Assert.AreEqual(_request1.OriginalParametersString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.OriginalParametersString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.OriginalParametersString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.QueryString);
            Assert.AreEqual(_request1.NewParametersString, _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.NewParametersString, _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.NewParametersString, _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalPathElement, 1);
            Assert.AreEqual(_request1.OriginalPath[1], _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.OriginalPath[1], _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.OriginalPath[1], _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalPathElement, -1);
            Assert.AreEqual("path2", _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual("path2", _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual("path2", _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.PathElement, 1);
            Assert.AreEqual(_request1.NewPath[1], _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.NewPath[1], _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.NewPath[1], _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.PathElement, -1);
            Assert.AreEqual("path2", _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual("changed2", _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual("changed2", _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalParameter, "param");
            Assert.AreEqual("value", _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual("", _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual("", _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.Parameter, "param");
            Assert.AreEqual("changed", _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual("", _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual("added", _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.Header, "HOST");
            Assert.AreEqual(_request1.GetHeader("HOST"), _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.GetHeader("HOST"), _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.GetHeader("HOST"), _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalHeader, "USER_AGENT");
            Assert.AreEqual(_request1.GetOriginalHeader("USER_AGENT"), _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.GetOriginalHeader("USER_AGENT"), _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.GetOriginalHeader("USER_AGENT"), _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.ServerVariable, "URL");
            Assert.AreEqual(_request1.GetServerVariable("URL"), _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.GetServerVariable("URL"), _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.GetServerVariable("URL"), _valueGetter.GetString(_request3, _ruleResult));

            _valueGetter.Initialize(Scope.OriginalServerVariable, "SERVER_PORT");
            Assert.AreEqual(_request1.GetOriginalServerVariable("SERVER_PORT"), _valueGetter.GetString(_request1, _ruleResult));
            Assert.AreEqual(_request2.GetOriginalServerVariable("SERVER_PORT"), _valueGetter.GetString(_request2, _ruleResult));
            Assert.AreEqual(_request3.GetOriginalServerVariable("SERVER_PORT"), _valueGetter.GetString(_request3, _ruleResult));

            _ruleResult.Properties.Set <IList <string> >(new List <string> {
                "match0", "match1", "match2"
            }, "R");
            _ruleResult.Properties.Set <IList <string> >(new List <string> {
                "condition0", "condition1"
            }, "C");

            _valueGetter.Initialize(Scope.MatchGroup, "0");
            Assert.AreEqual("match0", _valueGetter.GetString(_request1, _ruleResult));

            _valueGetter.Initialize(Scope.MatchGroup, "1");
            Assert.AreEqual("match1", _valueGetter.GetString(_request1, _ruleResult));

            _valueGetter.Initialize(Scope.MatchGroup, "2");
            Assert.AreEqual("match2", _valueGetter.GetString(_request1, _ruleResult));

            _valueGetter.Initialize(Scope.ConditionGroup, "0");
            Assert.AreEqual("condition0", _valueGetter.GetString(_request1, _ruleResult));

            _valueGetter.Initialize(Scope.ConditionGroup, "1");
            Assert.AreEqual("condition1", _valueGetter.GetString(_request1, _ruleResult));

            _valueGetter.Initialize(Scope.ConditionGroup, "2");
            Assert.AreEqual("", _valueGetter.GetString(_request1, _ruleResult));
        }