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]); }
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}."; } }
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); }