Skip to content

Commit 33fab74

Browse files
committed
Add randomizer for Uri #113
1 parent a9e4f24 commit 33fab74

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

Tynamix.ObjectFiller.Test/Tynamix.ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Compile Include="TestPoco\SimpleList.cs" />
102102
<Compile Include="TestPoco\TestEnum.cs" />
103103
<Compile Include="Properties\AssemblyInfo.cs" />
104+
<Compile Include="UriTest.cs" />
104105
</ItemGroup>
105106
<ItemGroup>
106107
<None Include="packages.config" />
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Tynamix.ObjectFiller;
4+
5+
namespace ObjectFiller.Test
6+
{
7+
[TestClass]
8+
public class UriTest
9+
{
10+
[TestMethod]
11+
public void RandomUriIsReturned()
12+
{
13+
var sut = new RandomUri();
14+
var value = sut.GetValue();
15+
Assert.IsNotNull(value);
16+
17+
Assert.IsFalse(string.IsNullOrEmpty(value.Host));
18+
Assert.IsFalse(string.IsNullOrEmpty(value.Scheme));
19+
Assert.IsFalse(string.IsNullOrEmpty(value.PathAndQuery));
20+
}
21+
22+
[TestMethod]
23+
public void Returns_Http_When_Set()
24+
{
25+
var sut = new RandomUri(RandomUri.SchemeType.Http);
26+
var value = sut.GetValue();
27+
Assert.IsNotNull(value);
28+
29+
Assert.AreEqual("http", value.Scheme);
30+
}
31+
32+
[TestMethod]
33+
public void Returns_Https_When_Set()
34+
{
35+
var sut = new RandomUri(RandomUri.SchemeType.Https);
36+
var value = sut.GetValue();
37+
Assert.IsNotNull(value);
38+
39+
Assert.AreEqual("https", value.Scheme);
40+
}
41+
42+
[TestMethod]
43+
public void Returns_Configured_Domain_When_Set()
44+
{
45+
var sut = new RandomUri(RandomUri.SchemeType.Https, new[] {"net"});
46+
var value = sut.GetValue();
47+
Assert.IsNotNull(value);
48+
49+
Assert.AreEqual("https", value.Scheme);
50+
Assert.IsTrue(value.Host.EndsWith(".net"));
51+
}
52+
53+
internal class UriTestClass
54+
{
55+
public Uri RemoteUri { get; set; }
56+
}
57+
58+
[TestMethod]
59+
public void TestIpAddressGenerator()
60+
{
61+
Filler<UriTestClass> filler = new Filler<UriTestClass>();
62+
63+
filler.Setup()
64+
.OnProperty(x => x.RemoteUri).Use<RandomUri>();
65+
66+
var result = filler.Create();
67+
68+
var url = result.RemoteUri.ToString();
69+
70+
Assert.IsFalse(string.IsNullOrEmpty(url));
71+
}
72+
}
73+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tynamix.ObjectFiller
7+
{
8+
/// <summary>
9+
/// Creates a random Uri
10+
/// </summary>
11+
public class RandomUri : IRandomizerPlugin<Uri>
12+
{
13+
/// <summary>
14+
/// Type which uri scheme should be used
15+
/// </summary>
16+
public enum SchemeType
17+
{
18+
/// <summary>
19+
/// The HTTP scheme
20+
/// </summary>
21+
Http,
22+
/// <summary>
23+
/// The HTTPS scheme
24+
/// </summary>
25+
Https,
26+
/// <summary>
27+
/// The HTTP and HTTPS schemes (randomized)
28+
/// </summary>
29+
HttpAndHttps
30+
}
31+
32+
private readonly SchemeType _schemeType;
33+
private readonly string[] _domainNames = { "de", "com", "org", "uk", "gov", "fr", "ru" };
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="RandomUri"/> class.
37+
/// </summary>
38+
public RandomUri()
39+
: this(SchemeType.HttpAndHttps)
40+
{
41+
42+
}
43+
44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="RandomUri"/> class.
46+
/// </summary>
47+
/// <param name="schemeType">Type of the scheme.</param>
48+
public RandomUri(SchemeType schemeType)
49+
{
50+
_schemeType = schemeType;
51+
}
52+
53+
/// <summary>
54+
/// Initializes a new instance of the <see cref="RandomUri" /> class.
55+
/// </summary>
56+
/// <param name="schemeType">Type of the scheme.</param>
57+
/// <param name="domainNames">The possible domain names.</param>
58+
public RandomUri(SchemeType schemeType, IEnumerable<string> domainNames)
59+
:this(schemeType)
60+
{
61+
_domainNames = domainNames.ToArray();
62+
}
63+
64+
/// <summary>
65+
/// Gets random data for type <see cref="Uri" />
66+
/// </summary>
67+
/// <returns>Random data for type <see cref="Uri" /></returns>
68+
public Uri GetValue()
69+
{
70+
var scheme = CreateScheme();
71+
var www = Randomizer<bool>.Create() ? "www." : string.Empty;
72+
var host = Randomizer<string>.Create(new MnemonicString(1)).ToLower();
73+
var domain = Randomizer<string>.Create(new RandomListItem<string>(_domainNames));
74+
StringBuilder relativePathBuilder = new StringBuilder();
75+
if (Randomizer<bool>.Create())
76+
{
77+
var relativeElements = Randomizer<int>.Create(new IntRange(1, 4));
78+
for (int i = 0; i < relativeElements; i++)
79+
{
80+
relativePathBuilder.Append("/");
81+
relativePathBuilder.Append(Randomizer<string>.Create(new MnemonicString(1)).ToLower());
82+
}
83+
}
84+
85+
var uriBuilder = new UriBuilder(scheme, $"{www}{host}.{domain}{relativePathBuilder}");
86+
return uriBuilder.Uri;
87+
}
88+
89+
private string CreateScheme()
90+
{
91+
switch (_schemeType)
92+
{
93+
case SchemeType.Http:
94+
return "http";
95+
case SchemeType.Https:
96+
return "https";
97+
default:
98+
return Randomizer<bool>.Create() ? "http" : "https";
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)