コード例 #1
0
        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"));
        }
コード例 #2
0
        public void AddGlobalVariables_should_add_baseurl_as_variable()
        {
            // Arrange
            var config = new Config();
            config.BaseUrl = "mybaseurl";
            var sessionVariables = new SessionVariables();

            // Act
            sessionVariables.AddGlobalVariables(config);

            // Assert
            Assert.That(sessionVariables.GetVariableValue("baseurl"), Is.EqualTo("mybaseurl"));
        }
コード例 #3
0
        public void AddGlobalVariables_should_add_variables_from_config()
        {
            // Arrange
            var config = new Config();
            config.Variables.Add(new Variable("eggs", "ham"));
            var sessionVariables = new SessionVariables();

            // Act
            sessionVariables.AddGlobalVariables(config);

            // Assert
            Assert.That(sessionVariables.GetVariableValue("eggs"), Is.EqualTo("ham"));
        }
コード例 #4
0
        public void AddOrUpdateVariables_should_set_variable()
        {
            // Arrange
            var sessionVariables = new SessionVariables();

            // Act
            sessionVariables.AddOrUpdateVariables(new Dictionary<string, string>()
            {
                {"nano", "leaf"},
                {"light", "bulb"}
            });

            // Assert
            Assert.That(sessionVariables.GetVariableValue("nano"), Is.EqualTo("leaf"));
            Assert.That(sessionVariables.GetVariableValue("light"), Is.EqualTo("bulb"));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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));
        }
コード例 #7
0
        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)"));
        }
コード例 #8
0
        public void AddOrUpdateVariable_should_set_variable()
        {
            // Arrange
            var sessionVariables = new SessionVariables();

            // Act
            sessionVariables.AddOrUpdateVariable("nano", "leaf");

            // Assert
            Assert.That(sessionVariables.GetVariableValue("nano"), Is.EqualTo("leaf"));
        }
コード例 #9
0
        public void ReplaceVariablesIn_should_replace_all_variables_and_escape_regex_characters_in_values()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            sessionVariables.AddOrUpdateVariable("nano", "$var leaf");
            sessionVariables.AddOrUpdateVariable("two", "(.*?) [a-z] ^perlmagic");

            string template = "{nano} {dummy} {two}";
            string expectedText = @"\$var\ leaf {dummy} \(\.\*\?\)\ \[a-z]\ \^perlmagic";

            // Act
            string actualText = sessionVariables.ReplaceVariablesIn(template);

            // Assert
            Assert.That(actualText, Is.EqualTo(expectedText));
        }
コード例 #10
0
        public void ReplacePlainTextVariablesIn_should_replace_all_variables()
        {
            // Arrange
            var sessionVariables = new SessionVariables();
            sessionVariables.AddOrUpdateVariable("nano", "leaf");
            sessionVariables.AddOrUpdateVariable("two", "ten");

            string template = "{nano} {dummy} {two}";
            string expectedText = "leaf {dummy} ten";

            // Act
            string actualText = sessionVariables.ReplacePlainTextVariablesIn(template);

            // Assert
            Assert.That(actualText, Is.EqualTo(expectedText));
        }
コード例 #11
0
 public VerificationsMatcher(SessionVariables variables)
 {
     _variables = variables;
 }
コード例 #12
0
        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;
        }
コード例 #13
0
        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;
        }