Example #1
0
        public void Match(Assertion item, string httpContent)
        {
            string regex = item.Value;

            if (!string.IsNullOrEmpty(regex))
            {
                regex = _variableProvider.ReplaceVariablesIn(regex);
                item.TransformedValue = regex;

                _assertionLogger.LogValue(item.Value, regex);

                try
                {
                    bool isMatch = Regex.IsMatch(httpContent, regex, RegexOptions.IgnoreCase);
                    item.Success = true;

                    if (item.AssertionType == AssertionType.Positive && isMatch == false)
                    {
                        item.Success = false;
                        _assertionLogger.LogFail(item.AssertionType, regex, AssertionMethod.Regex);
                    }
                    else if (item.AssertionType == AssertionType.Negative && isMatch == true)
                    {
                        item.Success = false;
                        _assertionLogger.LogFail(item.AssertionType, regex, AssertionMethod.Regex);
                    }
                    else
                    {
                        _assertionLogger.LogSuccess(item.AssertionType, regex, AssertionMethod.Regex);
                    }
                }
                catch (ArgumentException e)
                {
                    // Invalid regex - ignore.
                    item.Success = false;
                    _assertionLogger.LogException(AssertionMethod.Regex, e);
                }
            }
            else
            {
                _assertionLogger.LogEmpty();
            }
        }
Example #2
0
        public void Match(Assertion assertion, string httpContent)
        {
            string selector = assertion.Value;

            if (!string.IsNullOrEmpty(selector) && !string.IsNullOrEmpty(httpContent))
            {
                selector = _variableProvider.ReplaceVariablesIn(selector);
                assertion.TransformedValue = selector;

                _assertionLogger.LogValue(assertion.Value, selector);

                try
                {
                    var           parser   = new HtmlParser();
                    IHtmlDocument document = parser.Parse(httpContent);

                    IHtmlCollection <IElement> result = document.QuerySelectorAll(selector);
                    bool isMatch = (result != null && result.Length > 0);

                    switch (assertion.AssertionType)
                    {
                    case AssertionType.Positive:
                        if (isMatch == false)
                        {
                            assertion.Success = false;
                            _assertionLogger.LogFail(assertion.AssertionType, selector, AssertionMethod.CssSelector);
                        }
                        else
                        {
                            _assertionLogger.LogSuccess(assertion.AssertionType, selector, AssertionMethod.CssSelector);
                            assertion.Success = true;
                        }
                        break;

                    case AssertionType.Negative:
                        if (isMatch == true)
                        {
                            assertion.Success = false;
                            _assertionLogger.LogFail(assertion.AssertionType, selector, AssertionMethod.CssSelector);
                        }
                        else
                        {
                            _assertionLogger.LogSuccess(assertion.AssertionType, selector, AssertionMethod.CssSelector);
                            assertion.Success = true;
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception e)
                {
                    // Something happened with Anglesharp, it doesn't document its exceptions
                    assertion.Success = false;
                    _assertionLogger.LogException(AssertionMethod.CssSelector, e);
                }
            }
            else
            {
                _assertionLogger.LogEmpty();
            }
        }