Tynamix / ObjectFiller.NET

The .NET ObjectFiller fills the properties of your .NET objects with random data
http://objectfiller.net
MIT License
76 stars 34 forks source link

Randomizer for type URI is missing #113

Closed Tynamix closed 6 years ago

Tynamix commented 6 years ago

There is no default randomizer for type URI. Here is one prepared and can be included to objectfiller.

    public class RandomUri : IRandomizerPlugin<Uri>
    {
        /// <summary>
        /// Gets random data for type <see cref="Uri" />
        /// </summary>
        /// <returns>Random data for type <see cref="Uri" /></returns>
        public Uri GetValue()
        {
            var http = Randomizer<bool>.Create() ? "http" : "https";
            var www = Randomizer<bool>.Create() ? "www." : string.Empty;
            var host = Randomizer<string>.Create(new MnemonicString(1)).ToLower();
            var domain = Randomizer<string>.Create(new RandomListItem<string>("de", "com", "org", "uk", "gov", "fr", "ru"));
            string relativePath = string.Empty;
            if (Randomizer<bool>.Create())
            {
                var relativeElements =Randomizer<int>.Create(new IntRange(1, 4));
                for (int i = 0; i < relativeElements; i++)
                {
                    relativePath += $"/{Randomizer<string>.Create(new MnemonicString(1)).ToLower()}";
                }
            }

            UriBuilder u = new UriBuilder(http, $"{www}{host}.{domain}{relativePath}");
            return u.Uri;
        }
    }