/// <summary>
        /// Convert the input into a PlaceholderValue and realize any reference to a stored value into
        /// the actual value at the given time.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="storedValues"></param>
        /// <returns></returns>
        public static PlaceholderValue ConvertToPlaceholderValue(string key, string value, Dictionary<string, string> storedValues)
        {
            PlaceholderValue v = new PlaceholderValue
            {
                PlaceholderKey = key,
                DefinedValue = value,
                Location = BasicRequestDefinition.LocationForKey(key),
                Value =
                    BasicRequestDefinition.LocationForKey(value) == PlaceholderLocation.StoredValue
                        ? storedValues[value]
                        : value
            };

            // Allow the random-filename generator to swap the value with a randomly generated filename.
            if (null != value && value.StartsWith(RandomFilenameValuePrefix))
            {
                string fileExtension = value.Substring(RandomFilenameValuePrefix.Length);
                string randomFilename = string.Format("/testfile-{0:D}.{1}", Guid.NewGuid(), fileExtension);
                v.Value = randomFilename;
            }

            Debug.WriteLine("Converting \"{0}: {1}\" into loc={2},value={3}", key, value, v.Location, v.Value);

            return v;
        }
        /// <summary>
        /// Convert the input into a PlaceholderValue and realize any reference to a stored value into
        /// the actual value at the given time.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="storedValues"></param>
        /// <returns></returns>
        public static PlaceholderValue ConvertToPlaceholderValue(string key, string value, Dictionary<string, string> storedValues)
        {
            PlaceholderValue v = new PlaceholderValue
            {
                PlaceholderKey = key,
                DefinedValue = value,
                Location = BasicRequestDefinition.LocationForKey(key),
                Value =
                    BasicRequestDefinition.LocationForKey(value) == PlaceholderLocation.StoredValue
                        ? storedValues[value]
                        : value
            };

            if (BasicRequestDefinition.LocationForKey(v.Value) == PlaceholderLocation.CSharpCode)
            {
                v.Value = CSharpEval.Evaluate(v.Value.Substring(1), storedValues);
            }

            // Allow the random-filename generator to swap the value with a randomly generated filename.
            int index = value == null ? -1 : value.IndexOf(RandomFilenameValuePrefix);
            if (index >= 0)
            {
                int endIndex = value.IndexOf("!", index + 1);
                string placeholder = null;
                if (endIndex > -1)
                {
                    placeholder = value.Substring(index, endIndex - index + 1);
                }
                else
                {
                    // Be backwards comaptible with previous behavior.
                    placeholder = value.Substring(index);
                    if (!placeholder.EndsWith("!"))
                    {
                        if (placeholder != RandomFilenameValuePrefix)
                            value = "/" + value.Replace(placeholder, placeholder + "!");
                        else
                            value = value.Replace(placeholder, placeholder + "!");
                        placeholder = placeholder + "!";
                    }
                }
                string randomFilename = GenerateRandomFilename(placeholder);

                v.Value = value.Replace(placeholder, randomFilename);
            }

            Debug.WriteLine("Converting \"{0}: {1}\" into loc={2},value={3}", key, value, v.Location, v.Value);

            return v;
        }
Пример #3
0
        /// <summary>
        /// Convert the input into a PlaceholderValue and realize any reference to a stored value into
        /// the actual value at the given time.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="storedValues"></param>
        /// <returns></returns>
        public static PlaceholderValue ConvertToPlaceholderValue(string key, string value, Dictionary <string, string> storedValues)
        {
            string replacementValue = value;

            switch (BasicRequestDefinition.LocationForKey(value))
            {
            case PlaceholderLocation.StoredValue:
                if (!storedValues.TryGetValue(value, out replacementValue))
                {
                    throw new PlaceholderValueNotFoundException($"Unable to locate the placeholder value {value} in available values: {storedValues.Keys.ComponentsJoinedByString(",")}");
                }
                break;

            case PlaceholderLocation.CSharpCode:
                replacementValue = CSharpEval.Evaluate(value.Substring(1), storedValues);
                break;
            }

            PlaceholderValue v = new PlaceholderValue
            {
                PlaceholderKey = key,
                DefinedValue   = value,
                Location       = BasicRequestDefinition.LocationForKey(key),
                Value          = replacementValue
            };

            // Allow the random-filename generator to swap the value with a randomly generated filename.
            int index = value == null ? -1 : value.IndexOf(RandomFilenameValuePrefix);

            if (index >= 0)
            {
                int    endIndex    = value.IndexOf("!", index + 1);
                string placeholder = null;
                if (endIndex > -1)
                {
                    placeholder = value.Substring(index, endIndex - index + 1);
                }
                else
                {
                    // Be backwards comaptible with previous behavior.
                    placeholder = value.Substring(index);
                    if (!placeholder.EndsWith("!"))
                    {
                        if (placeholder != RandomFilenameValuePrefix)
                        {
                            value = "/" + value.Replace(placeholder, placeholder + "!");
                        }
                        else
                        {
                            value = value.Replace(placeholder, placeholder + "!");
                        }
                        placeholder = placeholder + "!";
                    }
                }
                string randomFilename = GenerateRandomFilename(placeholder);
                v.Value = value.Replace(placeholder, randomFilename);
            }

            Debug.WriteLine("Converting \"{0}: {1}\" into loc={2},value={3}", key, value, v.Location, v.Value);

            return(v);
        }