public void Should_throw_CodeEvaluationException_with_test_and_compilation_information_in_exception_message() { // given string code = "this won't compile"; var snippetReader = new SnippetFileReaderMock(code); var evaluator = new TestFileScriptEvaluator(new JsonConfiguration(), snippetReader); var test = new Test() { ScriptSnippets = new ScriptSnippets() { BeforeExecuteFilename = "filepath-isnt-used.snippet" }, Description = "My test" }; // when + then try { evaluator.EvaluateBeforeExecute(test, new RestRequest()); Assert.Fail("Expected a CodeEvaluationException"); } catch (CodeEvaluationException ex) { Assert.That(ex.Message, Contains.Substring("An exception occurred evaluating the before script for test 'My test'")); Assert.That(ex.Message, Contains.Substring("error CS1002: ; expected")); Assert.That(ex.InnerException, Is.Not.Null); Assert.That(ex.InnerException, Is.TypeOf <CompilationErrorException>()); } }
public void EvaluateBeforeExecute_should_add_required_references() { // given var snippetReader = new SnippetFileReaderMock("IRestRequest request = new RestRequest();"); var evaluator = new TestFileScriptEvaluator(new JsonConfiguration(), snippetReader); var test = new Test(); test.ScriptSnippets.BeforeExecuteFilename = "path-doesnt-matter.snippet"; // when + then bool result = evaluator.EvaluateBeforeExecute(test, new RestRequest()); Assert.That(result, Is.True); }
public void EvaluateBeforeExecute_should_set_globals() { // given string code = "Test.Description = \"it worked\";" + "Request.Method = Method.PUT;"; var snippetReader = new SnippetFileReaderMock(code); var evaluator = new TestFileScriptEvaluator(new JsonConfiguration(), snippetReader); var test = new Test(); test.ScriptSnippets.BeforeExecuteFilename = "filename-doesnt-matter.snippet"; // when bool result = evaluator.EvaluateBeforeExecute(test, new RestRequest()); // then Assert.That(result, Is.True); Assert.That(evaluator.RequestGlobals.Test.Description, Is.EqualTo("it worked")); Assert.That(evaluator.RequestGlobals.Request.Method, Is.EqualTo(Method.PUT)); }
public void EvaluateBeforeExecute_should_call_read_with_snippet_type_in_the_path() { // given var snippetReader = new Mock <ISnippetFileReader>(); var configuration = new JsonConfiguration(); configuration.ScriptSnippetDirectory = @"C:\foo"; var evaluator = new TestFileScriptEvaluator(configuration, snippetReader.Object); var test = new Test(); test.ScriptSnippets.BeforeExecuteFilename = "path-doesnt-matter.snippet"; string typeName = ScriptSnippetType.BeforeExecute.ToString().ToLower(); string expectedPath = Path.Combine(configuration.ScriptSnippetDirectory, typeName, test.ScriptSnippets.BeforeExecuteFilename); // when evaluator.EvaluateBeforeExecute(test, new RestRequest()); // then snippetReader.Verify(x => x.ReadFile(expectedPath)); }
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); }
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); }