예제 #1
0
        public async Task Run_should_populate_StartTime_and_EndTime_and_TotalRunTime()
        {
            // given
            var beforeStart = DateTime.UtcNow;

            var response = new HttpResponse();

            response.ResponseTime = TimeSpan.FromSeconds(5);

            HttpClientMock httpClient = new HttpClientMock(response);
            var            runner     = new TestFileRunner(httpClient, GetRepositoryFactory(), new JsonConfiguration(), _capturedVariableProviderFactory.Object, GetTestFileRunnerLoggerFactory());

            var testFile = CreateTestFile(new[]
            {
                new Test()
                {
                    Url = "foo1"
                },
            });

            // when
            TestFileResult session = await runner.RunAsync(testFile, "development", "bob");

            // then
            Assert.That(session.StartTime, Is.GreaterThanOrEqualTo(beforeStart));
            Assert.That(session.EndTime, Is.GreaterThanOrEqualTo(session.StartTime));
            Assert.That(session.TotalRunTime, Is.EqualTo(session.EndTime - session.StartTime));
        }
예제 #2
0
        public void ToString_should_append_headers_and_response_body_and_empty_line()
        {
            // Arrange
            var response = new HttpResponse();
            response.Headers = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("Server", "Apache"),
                new KeyValuePair<string, string>("Cache-Control", "private, s-maxage=0, max-age=0, must-revalidate"),
                new KeyValuePair<string, string>("Date", "Sun, 12 Apr 2015 19:18:21 GMT"),
                new KeyValuePair<string, string>("Content-Type", "text/html; charset=UTF-8")
            };

            response.Content = "<html><body></body></html>";
            response.StatusCode = HttpStatusCode.OK;

            // Act
            string content = response.ToString();

            // Assert
            string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            Assert.AreEqual("HTTP/1.1 200 OK", lines[0]);
            Assert.AreEqual("Server: Apache", lines[1]);
            Assert.AreEqual("Cache-Control: private, s-maxage=0, max-age=0, must-revalidate", lines[2]);
            Assert.AreEqual("Date: Sun, 12 Apr 2015 19:18:21 GMT", lines[3]);
            Assert.AreEqual("Content-Type: text/html; charset=UTF-8", lines[4]);
            Assert.AreEqual("", lines[5]);
            Assert.AreEqual("<html><body></body></html>", lines[6]);
        }
예제 #3
0
        public async Task should_fill_response_properties()
        {
            // given
            var httpLogWriter    = GetHttpLogWriter();
            var restResponseStub = new RestResponseStub();

            restResponseStub.Content    = "HTTP/1.1 200 OK\nServer: Apache\n\n<html>some text </html>";
            restResponseStub.StatusCode = HttpStatusCode.Accepted;
            restResponseStub.Headers    = new Parameter[] { new Parameter(), new Parameter() };

            HttpClient httpClient = CreateClient(restResponseStub);

            string method      = "get";
            string url         = "http://www.example.com";
            string postBody    = "";
            var    headers     = new List <HeaderItem>();
            var    restRequest = httpClient.CreateRestRequest(method, url, postBody, headers);

            // when
            HttpResponse response = await httpClient.ExecuteRequestAsync(restRequest, httpLogWriter);

            // then
            Assert.AreEqual(restResponseStub.StatusCode, response.StatusCode);
            Assert.AreEqual(restResponseStub.Content, response.Content);
            Assert.AreEqual(restResponseStub.Headers.Count, response.Headers.Count);
        }
예제 #4
0
        public void Run_should_populate_StartTime_and_EndTime_and_TotalRunTime()
        {
            // Arrange
            var beforeStart = DateTime.UtcNow;
            var config = new Config();
            var testCaseReader = new TestCaseReaderMock();
            var stringReader = new StringReader("");

            var response = new HttpResponse();
            response.ResponseTime = TimeSpan.FromSeconds(5);

            HttpClientMock httpClient = new HttpClientMock(response);
            IResultWriter resultWriter = new ResultWriterStub();
            var runner = new TestSessionRunner(config, httpClient, resultWriter);

            var caseCollection = CreateCaseCollection(new[]
            {
                new Case() { Url = "foo1" },
            });

            // Act
            TestCaseSession session = runner.Run(caseCollection);

            // Assert
            Assert.That(session.StartTime, Is.GreaterThanOrEqualTo(beforeStart));
            Assert.That(session.EndTime, Is.GreaterThanOrEqualTo(session.StartTime));
            Assert.That(session.TotalRunTime, Is.EqualTo(session.EndTime - session.StartTime));
        }
예제 #5
0
        private TestFileRunner CreateRunner()
        {
            _httpResponse   = new HttpResponse();
            _httpClientMock = new HttpClientMock(_httpResponse);

            return(new TestFileRunner(_httpClientMock, GetRepositoryFactory(), new JsonConfiguration(),
                                      _capturedVariableProviderFactory.Object, GetTestFileRunnerLoggerFactory()));
        }
예제 #6
0
        public void should_create_headers_in_ctor()
        {
            // Arrange
            var response = new HttpResponse();

            // Act + Assert
            Assert.NotNull(response.Headers);
            Assert.NotNull(response.ResponseTime);
        }
예제 #7
0
        public async Task Run_should_set_MinResponseTime_and_MaxResponseTime_from_http_response_times()
        {
            // given
            var response = new HttpResponse();

            response.ResponseTime = TimeSpan.FromSeconds(5);

            HttpClientMock httpClient = new HttpClientMock(response);

            httpClient.ResponseTimes = new List <TimeSpan>()
            {
                // Deliberately mixed up order
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(88),
                TimeSpan.FromSeconds(3),
                TimeSpan.FromSeconds(10)
            };
            httpClient.Response = response;

            var runner = new TestFileRunner(httpClient, GetRepositoryFactory(), new JsonConfiguration(), _capturedVariableProviderFactory.Object, GetTestFileRunnerLoggerFactory());

            var testFile = CreateTestFile(new[]
            {
                new Test()
                {
                    Url = "foo1"
                },
                new Test()
                {
                    Url = "foo2"
                },
                new Test()
                {
                    Url = "foo3"
                },
                new Test()
                {
                    Url = "foo4"
                },
            });

            // when
            TestFileResult session = await runner.RunAsync(testFile, "development", "bob");

            // then
            Assert.That(session.MinResponseTime, Is.EqualTo(TimeSpan.FromSeconds(3)));
            Assert.That(session.MaxResponseTime, Is.EqualTo(TimeSpan.FromSeconds(88)));
        }
예제 #8
0
        public async Task should_ignore_null_headers()
        {
            // given
            var        httpLogWriter = GetHttpLogWriter();
            HttpClient httpClient    = CreateClient(new RestResponse());

            string method      = "get";
            string url         = "http://www.example.com";
            string postBody    = "";
            var    headers     = new List <HeaderItem>();
            var    restRequest = httpClient.CreateRestRequest(method, url, postBody, headers);

            // when
            HttpResponse response = await httpClient.ExecuteRequestAsync(restRequest, httpLogWriter);

            // then
            Assert.IsNotNull(response);
        }
예제 #9
0
        public async Task should_record_response_times()
        {
            // given
            var        httpLogWriter = GetHttpLogWriter();
            HttpClient httpClient    = CreateClient(new RestResponse());

            _restClientMock.ResponseTime = TimeSpan.FromSeconds(1);

            string method      = "get";
            string url         = "http://www.example.com";
            string postBody    = "";
            var    headers     = new List <HeaderItem>();
            var    restRequest = httpClient.CreateRestRequest(method, url, postBody, headers);

            // when
            HttpResponse response = await httpClient.ExecuteRequestAsync(restRequest, httpLogWriter);

            // then
            Assert.That(response.ResponseTime, Is.GreaterThanOrEqualTo(TimeSpan.FromSeconds(1)));
        }
예제 #10
0
        private static void ProcessResponse(Test test, ICapturedVariableProvider variables, AssertionsMatcher assertionMatcher,
                                            TestResult testResult, HttpResponse response, HttpLogWriter httpLogWriter, ITestFileRunnerLogger logger)
        {
            testResult.ResponseTime = response.ResponseTime;
            testResult.HttpResponse = response;
            testResult.HttpLog      = httpLogWriter.StringBuilder.ToString();
            testResult.HttpContent  = response.Content;

            if (response.StatusCode == test.ExpectedHttpStatusCode)
            {
                testResult.ResponseCodeSuccess = true;
                string content = response.ToString();

                // Put the captured variables regex values in the current variable set
                foreach (var capturedVariable in test.CapturedVariables)
                {
                    capturedVariable.Regex = variables.ReplacePlainTextVariablesIn(capturedVariable.Regex);
                }

                List <Variable> parsedVariables = CapturedVariableProvider.MatchVariables(test.CapturedVariables, content, logger);
                variables.AddOrUpdateVariables(parsedVariables);
                logger.WriteLine("{0} captured variable(s) parsed.", parsedVariables.Count);

                // Verify assertions
                testResult.AssertionResults = assertionMatcher.MatchVerifications(test.Assertions, content);
                logger.WriteLine("Verifying {0} assertion(s)", testResult.AssertionResults.Count);
                foreach (Assertion item in testResult.AssertionResults)
                {
                    logger.AppendTextLine(item.Log);
                }

                // Store the log
                testResult.Log = logger.GetLog();
            }
            else
            {
                testResult.ResponseCodeSuccess = false;
                testResult.Log = $"No verifications run - the response code {response.StatusCode} did not match the expected response code {test.ExpectedHttpStatusCode}.";
            }
        }
예제 #11
0
        public async Task should_return_expected_html_content()
        {
            // given
            var httpLogWriter = GetHttpLogWriter();
            var restResponse  = new RestResponseStub()
            {
                Content = "<html>some text </html>"
            };
            HttpClient httpClient = CreateClient(restResponse);

            string method      = "get";
            string url         = "http://www.example.com";
            string postBody    = "";
            var    headers     = new List <HeaderItem>();
            var    restRequest = httpClient.CreateRestRequest(method, url, postBody, headers);

            // when
            HttpResponse response = await httpClient.ExecuteRequestAsync(restRequest, httpLogWriter);

            // then
            Assert.NotNull(response);
            Assert.AreEqual(restResponse.Content, response.Content);
        }
예제 #12
0
        internal async Task <TestResult> RunTestAsync(Test test, int position, ICapturedVariableProvider variables, AssertionsMatcher assertionMatcher, string environment)
        {
            var testResult = new TestResult
            {
                Position    = position,
                SessionId   = SessionId,
                Test        = test,
                ResultState = TestResultState.Failed
            };

            if (test.TestConditions.RequiredEnvironments.Any())
            {
                bool inEnvironment = test.TestConditions.RequiredEnvironments
                                     .Where(x => !string.IsNullOrEmpty(x))
                                     .Any(x => x.Equals(environment, StringComparison.InvariantCultureIgnoreCase));
                if (!inEnvironment)
                {
                    testResult.ResultState = TestResultState.Skipped;
                    return(testResult);
                }
            }

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

                string postBody = variables.ReplacePlainTextVariablesIn(test.PostBody);
                foreach (HeaderItem header in test.Headers)
                {
                    header.Value = variables.ReplacePlainTextVariablesIn(header.Value);
                }

                var          logger  = _loggerFactory.CreateLogger();
                IRestRequest request = _httpClient.CreateRestRequest(test.Method, resolvedUrl, postBody, test.Headers);

                // Scripting part
                if (!string.IsNullOrEmpty(test.ScriptSnippets?.BeforeExecuteFilename))
                {
                    logger.WriteLine("Evaluating C# script");

                    try
                    {
                        var  snippetReader = new SnippetFileReader(_configuration);
                        var  evaluator     = new TestFileScriptEvaluator(_configuration, snippetReader);
                        bool success       = evaluator.EvaluateBeforeExecute(test, request);

                        if (success)
                        {
                            request = evaluator.RequestGlobals.Request;
                            test    = evaluator.RequestGlobals.Test;
                            logger.WriteLine("Compilation successful.");
                        }
                    }
                    catch (Exception ex)
                    {
                        testResult.ScriptCompilationSuccess = false;
                        testResult.ExceptionMessage         = "The script failed to compile - see the log file for a stack trace.";
                        logger.WriteLine("Compilation failed: {0}", ex);
                    }
                }

                var          httpLogWriter = new HttpLogWriter();
                HttpResponse response      = await _httpClient.ExecuteRequestAsync(request, httpLogWriter);

                ProcessResponse(test, variables, assertionMatcher, testResult, response, httpLogWriter, logger);
            }
            catch (Exception ex)
            {
                testResult.Log = "An exception occured: " + ex;
                testResult.ResponseCodeSuccess = false;
                testResult.ExceptionMessage    = ex.Message;
            }

            if (testResult.ResponseCodeSuccess && testResult.AssertionsSuccess && testResult.ScriptCompilationSuccess)
            {
                testResult.ResultState = TestResultState.Success;
            }

            return(testResult);
        }
예제 #13
0
        private TestSessionRunner CreateRunner(Config config)
        {
            _httpResponse = new HttpResponse();
            _httpClientMock = new HttpClientMock(_httpResponse);
            _resultWriterStub = new ResultWriterStub();

            return new TestSessionRunner(config, _httpClientMock, _resultWriterStub);
        }
예제 #14
0
        public void Run_should_set_MinResponseTime_and_MaxResponseTime_from_http_response_times()
        {
            // Arrange
            var config = new Config();
            var stringReader = new StringReader("");
            var testCaseReader = new TestCaseReaderMock();

            var response = new HttpResponse();
            response.ResponseTime = TimeSpan.FromSeconds(5);

            HttpClientMock httpClient = new HttpClientMock(response);
            httpClient.ResponseTimes = new List<TimeSpan>()
            {
                // Deliberately mixed up order
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(88),
                TimeSpan.FromSeconds(3),
                TimeSpan.FromSeconds(10)
            };
            httpClient.Response = response;

            IResultWriter resultWriter = new ResultWriterStub();
            var runner = new TestSessionRunner(config, httpClient, resultWriter);

            var caseCollection = CreateCaseCollection(new[]
            {
                new Case() { Url = "foo1" },
                new Case() { Url = "foo2" },
                new Case() { Url = "foo3" },
                new Case() { Url = "foo4" },
            });

            // Act
            TestCaseSession session = runner.Run(caseCollection);

            // Assert
            Assert.That(session.MinResponseTime, Is.EqualTo(TimeSpan.FromSeconds(3)));
            Assert.That(session.MaxResponseTime, Is.EqualTo(TimeSpan.FromSeconds(88)));
        }
예제 #15
0
        internal async Task <TestResult> RunTestAsync(Test test, int position, ICapturedVariableProvider variables, AssertionsMatcher assertionMatcher)
        {
            var testResult = new TestResult
            {
                Position  = position,
                SessionId = SessionId,
                Test      = test
            };

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

                string postBody = variables.ReplacePlainTextVariablesIn(test.PostBody);
                foreach (HeaderItem header in test.Headers)
                {
                    header.Value = variables.ReplacePlainTextVariablesIn(header.Value);
                }

                IRestRequest request = _httpClient.CreateRestRequest(test.Method, resolvedUrl, postBody, test.Headers);
                var          logger  = _loggerFactory.CreateLogger();

                // Scripting part
                if (test.ScriptSnippets != null && !string.IsNullOrEmpty(test.ScriptSnippets.BeforeExecuteFilename))
                {
                    logger.WriteLine("Evaluating C# script");

                    try
                    {
                        var  snippetReader = new SnippetFileReader(_configuration);
                        var  evaluator     = new TestFileScriptEvaluator(_configuration, snippetReader);
                        bool success       = evaluator.EvaluateBeforeExecute(test, request);

                        if (success)
                        {
                            request = evaluator.RequestGlobals.Request;
                            test    = evaluator.RequestGlobals.Test;
                            logger.WriteLine("Compilation successful.");
                        }
                    }
                    catch (Exception ex)
                    {
                        testResult.ScriptCompilationSuccess = false;
                        testResult.ExceptionMessage         = "The script failed to compile - see the log file for a stack trace.";
                        logger.WriteLine("Compilation failed: {0}", ex);
                    }
                }

                var          httpLogWriter = new HttpLogWriter();
                HttpResponse response      = await _httpClient.ExecuteRequestAsync(request, httpLogWriter);

                testResult.ResponseTime = response.ResponseTime;
                testResult.HttpResponse = response;
                testResult.HttpLog      = httpLogWriter.StringBuilder.ToString();
                testResult.HttpContent  = response.Content;

                if (response.StatusCode == test.ExpectedHttpStatusCode)
                {
                    testResult.ResponseCodeSuccess = true;
                    string content = response.ToString();

                    // Put the captured variables regex values in the current variable set
                    foreach (var capturedVariable in test.CapturedVariables)
                    {
                        capturedVariable.Regex = variables.ReplacePlainTextVariablesIn(capturedVariable.Regex);
                    }

                    List <Variable> parsedVariables = CapturedVariableProvider.MatchVariables(test.CapturedVariables, content, logger);
                    variables.AddOrUpdateVariables(parsedVariables);
                    logger.WriteLine("{0} captured variable(s) parsed.", parsedVariables.Count);

                    // Verify assertions
                    testResult.AssertionResults = assertionMatcher.MatchVerifications(test.Assertions, content);
                    logger.WriteLine("Verifying {0} assertion(s)", testResult.AssertionResults.Count);
                    foreach (Assertion item in testResult.AssertionResults)
                    {
                        logger.AppendTextLine(item.Log);
                    }

                    // Store the log
                    testResult.Log = logger.GetLog();
                }
                else
                {
                    testResult.ResponseCodeSuccess = false;
                    testResult.Log = $"No verifications run - the response code {response.StatusCode} did not match the expected response code {test.ExpectedHttpStatusCode}.";
                }
            }
            catch (Exception ex)
            {
                testResult.Log = "An exception occured: " + ex;
                testResult.ResponseCodeSuccess = false;
                testResult.ExceptionMessage    = ex.Message;
            }

            return(testResult);
        }
예제 #16
0
 public HttpClientMock(HttpResponse response)
 {
     Response = response;
 }
예제 #17
0
 public HttpClientMock(HttpResponse response)
 {
     Response = response;
 }