public void MatchNegative_should_not_match_text_that_is_not_in_content()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            var matcher = new VerificationsMatcher(sessionVariables);

            var verifications = new List<VerificationItem>();
            verifications.Add(new VerificationItem("desc1", "this isnt in the text", VerifyType.Negative));
            verifications.Add(new VerificationItem("desc2", "content here", VerifyType.Negative));

            string content = "<p>Some content here</p>";

            // Act
            List<VerificationItem> results = matcher.MatchNegative(verifications, content);

            // Assert
            Assert.That(results.Count, Is.EqualTo(2));
            Assert.That(results[0].Success, Is.True);
            Assert.That(results[0].Description, Is.EqualTo("desc1"));
            Assert.That(results[0].Regex, Is.EqualTo("this isnt in the text"));

            Assert.That(results[1].Success, Is.False);
            Assert.That(results[1].Description, Is.EqualTo("desc2"));
            Assert.That(results[1].Regex, Is.EqualTo("content here"));
        }
        public void invalid_regex_should_set_success_to_false()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            var matcher = new VerificationsMatcher(sessionVariables);

            var verifications = new List<VerificationItem>();
            verifications.Add(new VerificationItem("dodgy regex", "((*)", VerifyType.Positive));

            string content = "<p>Some content here</p>";

            // Act
            List<VerificationItem> results = matcher.MatchPositive(verifications, content);

            // Assert
            Assert.That(results.Count, Is.EqualTo(1));
            Assert.That(results[0].Success, Is.False);
        }
        public void MatchNegative_should_return_veriftype_positives_in_list()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            var matcher = new VerificationsMatcher(sessionVariables);

            var verifications = new List<VerificationItem>();
            verifications.Add(new VerificationItem("p1", "a regex", VerifyType.Positive));
            verifications.Add(new VerificationItem("p2", "another regex", VerifyType.Positive));
            verifications.Add(new VerificationItem("n1", "one more regex", VerifyType.Negative));

            string content = "<p>whatever</p>";

            // Act
            List<VerificationItem> results = matcher.MatchNegative(verifications, content);

            // Assert
            Assert.That(results.Count, Is.EqualTo(3));
        }
        public void should_replace_variables_in_regex()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            sessionVariables.AddOrUpdateVariable("password", "tedx123");

            var matcher = new VerificationsMatcher(sessionVariables);

            var verifications = new List<VerificationItem>();
            verifications.Add(new VerificationItem("desc1", "({password})", VerifyType.Positive));

            string content = "<p>The password is tedx123</p>";

            // Act
            List<VerificationItem> results = matcher.MatchPositive(verifications, content);

            // Assert
            Assert.That(results.Count, Is.EqualTo(1));
            Assert.That(results[0].Success, Is.True);
            Assert.That(results[0].Description, Is.EqualTo("desc1"));
            Assert.That(results[0].Regex, Is.EqualTo("({password})"));
            Assert.That(results[0].TransformedRegex, Is.EqualTo("(tedx123)"));
        }
        internal TestCaseResult RunCase(Case testCase, SessionVariables variables, VerificationsMatcher verificationMatcher)
        {
            var testResult = new TestCaseResult();
            testResult.TestCase = testCase;

            try
            {
                string resolvedUrl = variables.ReplacePlainTextVariablesIn(testCase.Url);
                testResult.ActualUrl = resolvedUrl;

                HttpResponse response = _httpClient.ExecuteRequest(testCase.Method, resolvedUrl, testCase.PostType, testCase.PostBody, testCase.Headers);
                testResult.ResponseTime = response.ResponseTime;
                testResult.HttpResponse = response;

                if (response.StatusCode == testCase.VerifyResponseCode)
                {
                    testResult.ResponseCodeSuccess = true;
                    string content = response.ToString();

                    // Put the parseresponse regex values in the current variable set
                    Dictionary<string, string> parsedVariables = ParseResponseMatcher.MatchParseResponses(testCase.ParseResponses, content);
                    variables.AddOrUpdateVariables(parsedVariables);

                    // Verify positives
                    testResult.VerifyPositiveResults = verificationMatcher.MatchPositive(testCase.VerifyPositives, content);

                    // Verify Negatives
                    testResult.VerifyNegativeResults = verificationMatcher.MatchNegative(testCase.VerifyNegatives, content);
                }
                else
                {
                    testResult.ResponseCodeSuccess = false;
                }

                if (testResult.Success == false)
                {
                    testResult.Message = testCase.ErrorMessage;
                }

                if (ShouldLogRequest(testResult, testCase))
                {
                    _httpClient.LogLastRequest();
                }

                if (ShouldLogResponse(testResult, testCase))
                {
                    _httpClient.LogLastResponse();
                }

                _resultWriter.Write(testResult);

                if (testCase.Sleep > 0)
                    Thread.Sleep(testCase.Sleep * 1000);
            }
            catch (Exception ex)
            {
                testResult.ResponseCodeSuccess = false;
                testResult.ExceptionMessage = ex.Message;
            }

            return testResult;
        }
        public TestCaseSession Run(CaseCollection testCollection)
        {
            _isStopPending = false;
            _currentResults = new List<TestCaseResult>();

            var session = new TestCaseSession();
            session.StartTime = DateTime.UtcNow;

            // Add all config variables and ones in this <testcase>
            var variables = new SessionVariables();
            variables.AddGlobalVariables(_config);
            variables.AddOrUpdateVariables(testCollection.Variables);

            var verificationsMatcher = new VerificationsMatcher(variables);

            // Ensure we loop atleast once:
            int repeatTotal = (testCollection.Repeat > 0) ? testCollection.Repeat : 1;
            List<Case> testCases = testCollection.TestCases.ToList();

            TimeSpan minResponseTime = TimeSpan.MaxValue;
            TimeSpan maxResponseTime = TimeSpan.MinValue;
            int totalCasesRun = 0;

            CasesRun = 0;
            TotalCases = testCases.Count;
            RepeatCount = 0;

            for (int i = 0; i < repeatTotal; i++)
            {
                foreach (Case testCase in testCases)
                {
                    if (_isStopPending)
                        break;

                    try
                    {
                        TestCaseResult result = RunCase(testCase, variables, verificationsMatcher);
                        session.TestCaseResults.Add(result);
                        _currentResults.Add(result);

                        if (result.ResponseTime < minResponseTime)
                            minResponseTime = result.ResponseTime;

                        if (result.ResponseTime > maxResponseTime)
                            maxResponseTime = result.ResponseTime;
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "An exception occurred running case {0}", testCase.Id);
                    }
                    finally
                    {
                        totalCasesRun++;

                        CasesRun++;
                        RepeatCount = i;
                    }
                }

                if (_isStopPending)
                    break;
            }

            session.EndTime = DateTime.UtcNow;
            session.TotalRunTime = session.EndTime - session.StartTime;
            session.TotalCasesRun = totalCasesRun;
            session.MinResponseTime = minResponseTime;
            session.MaxResponseTime = maxResponseTime;

            return session;
        }