/// <inheritdoc /> public string Parse(string input, IEnumerable <Match> matches) { var enumerable = matches as Match[] ?? matches.ToArray(); if (!enumerable.Any()) { return(input); } ValueTuple <string, StringValues>[] formValues; try { // We don't care about any exceptions here. formValues = _httpContextService.GetFormValues(); } catch { formValues = Array.Empty <(string, StringValues)>(); } // TODO there can be multiple form values, so this should be fixed in the future. var formDict = formValues.ToDictionary(f => f.Item1, f => f.Item2.First()); foreach (var match in enumerable) { var formValueName = match.Groups[2].Value; formDict.TryGetValue(formValueName, out var replaceValue); input = input.Replace(match.Value, replaceValue); } return(input); }
/// <inheritdoc /> public ConditionCheckResultModel Validate(StubModel stub) { var result = new ConditionCheckResultModel(); var formConditions = stub.Conditions?.Form?.ToArray() ?? Array.Empty <StubFormModel>(); if (!formConditions.Any()) { return(result); } try { var form = _httpContextService.GetFormValues(); var validConditions = 0; foreach (var condition in formConditions) { var(formKey, formValues) = form.FirstOrDefault(f => f.Item1 == condition.Key); if (formKey == null) { result.ConditionValidation = ConditionValidationType.Invalid; result.Log = $"No form value with key '{condition.Key}' found."; break; } validConditions += formValues .Count(value => StringHelper.IsRegexMatchOrSubstring(HttpUtility.UrlDecode(value), condition.Value)); } // If the number of succeeded conditions is equal to the actual number of conditions, // the form condition is passed and the stub ID is passed to the result. if (validConditions == formConditions.Length) { result.ConditionValidation = ConditionValidationType.Valid; } else { result.Log = $"Number of configured form conditions: '{formConditions.Length}'; number of passed form conditions: '{validConditions}'"; result.ConditionValidation = ConditionValidationType.Invalid; } } catch (InvalidOperationException ex) { result.Log = ex.Message; result.ConditionValidation = ConditionValidationType.Invalid; } return(result); }