public void Write_should_contain_test_and_result_information()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var stringWriter = new StringWriter(stringBuilder);
            var resultWriter = new TextResultWriter(stringWriter);

            var result = new TestCaseResult()
            {
                ActualUrl = "http://www.actualurl.com",
                Message = "my message",
                ResponseTime = TimeSpan.FromSeconds(2),
                ResponseCodeSuccess = true,
                TestCase = new Case() {  Url = "http://www.originalurl.com", Id = 99, ShortDescription = "shortdescription" }
            };

            // Act
            resultWriter.Write(result);

            // Assert
            string output = stringBuilder.ToString();

            Assert.That(output, Is.StringContaining(new String('-', 80)));
            Assert.That(output, Is.StringContaining("Case 99 (shortdescription)"));
            Assert.That(output, Is.StringContaining(""));

            Assert.That(output, Is.StringContaining(" - Original url: " + result.TestCase.Url));
            Assert.That(output, Is.StringContaining(" - Actual url: " + result.ActualUrl));
            Assert.That(output, Is.StringContaining(" - Response code success: Passed"));
            Assert.That(output, Is.StringContaining(" - Time taken: " + result.ResponseTime));
            Assert.That(output, Is.StringContaining(" - Success: Passed"));
            Assert.That(output, Is.StringContaining(" - Message: " + result.Message));
        }
        public void VerifyPositivesSuccess_and_VerifyNegativeSuccess_should_return_true_when_positiveresults_is_null()
        {
            // Arrange
            var testCaseResults = new TestCaseResult();

            // Act + Assert
            Assert.That(testCaseResults.VerifyPositivesSuccess, Is.True);
            Assert.That(testCaseResults.VerifyNegativeSuccess, Is.True);
        }
        public void VerifyPositivesSuccess_should_return_false_when_all_positive_results_are_false()
        {
            // Arrange
            var testCaseResults = new TestCaseResult();
            testCaseResults.VerifyPositiveResults.Add(new VerificationItem("desc", "regex", VerifyType.Positive) { Success = false });

            // Act
            bool actualResult = testCaseResults.VerifyPositivesSuccess;

            // Assert
            Assert.That(actualResult, Is.False);
        }
        public void Success_should_return_result_based_on_success_codes(bool expectedResult, bool responseCodeSuccess, bool positiveSuccess, bool negativeSuccess)
        {
            // Arrange
            var testCaseResults = new TestCaseResult();
            testCaseResults.ResponseCodeSuccess = responseCodeSuccess;
            testCaseResults.VerifyPositiveResults.Add(new VerificationItem("desc", "regex", VerifyType.Positive) { Success = positiveSuccess });
            testCaseResults.VerifyNegativeResults.Add(new VerificationItem("desc", "regex", VerifyType.Negative) { Success = negativeSuccess });

            // Act
            bool actualResult = testCaseResults.Success;

            // Assert
            Assert.That(actualResult, Is.EqualTo(expectedResult));
        }
        public void Write_should_contain_handle_empty_verify_results()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var stringWriter = new StringWriter(stringBuilder);
            var resultWriter = new TextResultWriter(stringWriter);

            var result = new TestCaseResult()
            {
                TestCase = new Case() { Url = "", Id = 99, ShortDescription = "" }
            };

            // Act
            resultWriter.Write(result);

            // Assert
            string output = stringBuilder.ToString();

            Assert.That(output, Is.StringContaining("Verify negatives"));
            Assert.That(output, Is.StringContaining("- (none found)"));
        }
        public void ShouldLogResponse_should_return_true_when_testcase_logresponse_is_true()
        {
            // Arrange
            var config = new Config();
            config.GlobalHttpLog = LogType.None;
            TestSessionRunner runner = CreateRunner(config);

            var testResult = new TestCaseResult();
            var testCase = new Case() { LogResponse = true };

            // Act
            bool shouldLog = runner.ShouldLogResponse(testResult, testCase);

            // Assert
            Assert.That(shouldLog, Is.True);
        }
        public void ShouldLogResponse_should_return_true_when_case_fails_and_onfail()
        {
            // Arrange
            var config = new Config();
            config.GlobalHttpLog = LogType.OnFail;
            TestSessionRunner runner = CreateRunner(config);

            var testResult = new TestCaseResult() { ResponseCodeSuccess = false };
            var testCase = new Case();

            // Act
            bool shouldLog = runner.ShouldLogResponse(testResult, testCase);

            // Assert
            Assert.That(shouldLog, Is.True);
        }
        public void ShouldLogRequest_should_return_true_when_logtype_is_all()
        {
            // Arrange
            var config = new Config();
            config.GlobalHttpLog = LogType.All;
            TestSessionRunner runner = CreateRunner(config);

            var testResult = new TestCaseResult() {ResponseCodeSuccess = true };
            var testCase = new Case();

            // Act
            bool shouldLog = runner.ShouldLogRequest(testResult, testCase);

            // Assert
            Assert.That(shouldLog, Is.True);
        }
 public TestCaseResultsBuilder New()
 {
     _currentTestCaseResult = new TestCaseResult();
     return this;
 }
        public void Write_should_contain_verify_negative_results()
        {
            // Arrange
            var stringBuilder = new StringBuilder();
            var stringWriter = new StringWriter(stringBuilder);
            var resultWriter = new TextResultWriter(stringWriter);

            var result = new TestCaseResult()
            {
                TestCase = new Case() { Url = "", Id = 99, ShortDescription = "" }
            };
            result.VerifyPositiveResults.Add(new VerificationItem("x1", "myregex1", VerifyType.Positive) { Success = true });
            result.VerifyPositiveResults.Add(new VerificationItem("x2", "myregex2", VerifyType.Positive) { Success = true });

            // Act
            resultWriter.Write(result);

            // Assert
            string output = stringBuilder.ToString();

            Assert.That(output, Is.StringContaining("Verify negatives"));
            Assert.That(output, Is.StringContaining("- Success: Passed"));
            Assert.That(output, Is.StringContaining("- x1 - True"));
            Assert.That(output, Is.StringContaining("- x2 - True"));
        }
 internal bool ShouldLogResponse(TestCaseResult testResult, Case testCase)
 {
     return (testResult.ResponseCodeSuccess == false && _config.GlobalHttpLog == LogType.OnFail)
            || _config.GlobalHttpLog == LogType.All
            || testCase.LogResponse;
 }
        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;
        }