/// <summary>
        /// Finds text in the content, returning them as variables, e.g. {capturedvariable1} = value
        /// </summary>
        public static List <Variable> MatchVariables(List <CapturedVariable> capturedVariables, string content, ITestFileRunnerLogger logger)
        {
            var variables             = new List <Variable>();
            var variablePostProcessor = new VariablePostProcessor();

            foreach (CapturedVariable regexItem in capturedVariables)
            {
                logger.WriteLine("Parsing captured variable '{{{0}}}'", regexItem.Name);
                logger.WriteLine("- Regex: {0}", regexItem.Regex);

                string capturedValue = "";
                try
                {
                    var regex = new Regex(regexItem.Regex, RegexOptions.IgnoreCase);
                    if (regex.IsMatch(content))
                    {
                        MatchCollection matches    = regex.Matches(content);
                        int             matchCount = 0;
                        foreach (Match match in matches)
                        {
                            if (match.Groups.Count > 1)
                            {
                                string detectedValue = match.Groups[1].Value;
                                logger.WriteLine($"- Detected value: {detectedValue}");

                                string transformedValue = variablePostProcessor.Process(detectedValue, regexItem.PostProcessorType);
                                logger.WriteLine($"- Transformed value: {detectedValue}");

                                capturedValue = transformedValue;
                                logger.WriteLine($"{++matchCount}. '{regexItem.Regex}' matched, updated variable to '{capturedValue}'");
                                break;
                            }

                            logger.WriteLine("- {0}. '{1}' matched, but the regex has no capture groups so the variable value wasn't updated.", ++matchCount, regexItem.Regex);
                        }
                    }
                    else
                    {
                        logger.WriteLine("- No match");
                    }
                }
                catch (ArgumentException e)
                {
                    logger.WriteLine("- Invalid regex: {0}", e.Message);
                }

                variables.Add(new Variable(regexItem.Name, capturedValue, ""));
            }

            return(variables);
        }
Example #2
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}.";
            }
        }
Example #3
0
 public void LogItem(AssertionType assertionType, Assertion item)
 {
     _logger.WriteLine("Verifying {0} item \"{1}\"", assertionType, item.Description ?? "(no description)");
 }