Skip to content

Commit 52e4645

Browse files
authored
Merge pull request #114 from twenzel/feature/UriRandomizer
Feature/uri randomizer
2 parents a9e4f24 + 1fdfa63 commit 52e4645

File tree

7 files changed

+322
-0
lines changed

7 files changed

+322
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ UpgradeLog*.htm
174174
# Microsoft Fakes
175175
FakesAssemblies/
176176
ObjectFillerNET.v2.ncrunchsolution
177+
ObjectFillerNET.v3.ncrunchsolution
177178
ObjectFiller/ObjectFiller.v2.ncrunchproject
178179
ObjectFiller.Test/ObjectFiller.Test.v2.ncrunchproject
179180
.vs/config/applicationhost.config

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
<Compile Include="TestPoco\SimpleList.cs" />
102102
<Compile Include="TestPoco\TestEnum.cs" />
103103
<Compile Include="Properties\AssemblyInfo.cs" />
104+
<Compile Include="UriTest.cs" />
105+
<Compile Include="UrlTests.cs" />
104106
</ItemGroup>
105107
<ItemGroup>
106108
<None Include="packages.config" />

Tynamix.ObjectFiller.Test/UriTest.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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, "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 Set_Value_With_Filler_With_Setup()
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+
74+
[TestMethod]
75+
public void Set_Value_With_Filler_Without_Setup()
76+
{
77+
Filler<UriTestClass> filler = new Filler<UriTestClass>();
78+
79+
var result = filler.Create();
80+
81+
var url = result.RemoteUri.ToString();
82+
83+
Assert.IsFalse(string.IsNullOrEmpty(url));
84+
}
85+
}
86+
}

Tynamix.ObjectFiller.Test/UrlTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Tynamix.ObjectFiller;
4+
5+
namespace ObjectFiller.Test
6+
{
7+
[TestClass]
8+
public class UrlTest
9+
{
10+
[TestMethod]
11+
public void RandomUrlIsReturned()
12+
{
13+
var sut = new RandomUrl();
14+
var value = sut.GetValue();
15+
Assert.IsNotNull(value);
16+
17+
var uri = new Uri(value);
18+
19+
Assert.IsFalse(string.IsNullOrEmpty(uri.Host));
20+
Assert.IsFalse(string.IsNullOrEmpty(uri.Scheme));
21+
Assert.IsFalse(string.IsNullOrEmpty(uri.PathAndQuery));
22+
}
23+
24+
[TestMethod]
25+
public void Returns_Http_When_Set()
26+
{
27+
var sut = new RandomUrl(RandomUri.SchemeType.Http);
28+
var value = sut.GetValue();
29+
Assert.IsNotNull(value);
30+
31+
var uri = new Uri(value);
32+
Assert.AreEqual("http", uri.Scheme);
33+
}
34+
35+
[TestMethod]
36+
public void Returns_Https_When_Set()
37+
{
38+
var sut = new RandomUrl(RandomUri.SchemeType.Https);
39+
var value = sut.GetValue();
40+
Assert.IsNotNull(value);
41+
42+
var uri = new Uri(value);
43+
Assert.AreEqual("https", uri.Scheme);
44+
}
45+
46+
[TestMethod]
47+
public void Returns_Configured_Domain_When_Set()
48+
{
49+
var sut = new RandomUrl(RandomUri.SchemeType.Https, "net");
50+
var value = sut.GetValue();
51+
Assert.IsNotNull(value);
52+
53+
var uri = new Uri(value);
54+
Assert.AreEqual("https", uri.Scheme);
55+
Assert.IsTrue(uri.Host.EndsWith(".net"));
56+
}
57+
58+
internal class UriTestClass
59+
{
60+
public string RemoteUrl { get; set; }
61+
}
62+
63+
[TestMethod]
64+
public void Set_Value_With_Filler_With_Setup()
65+
{
66+
Filler<UriTestClass> filler = new Filler<UriTestClass>();
67+
68+
filler.Setup()
69+
.OnProperty(x => x.RemoteUrl).Use<RandomUrl>();
70+
71+
var result = filler.Create();
72+
73+
var url = result.RemoteUrl;
74+
75+
Assert.IsFalse(string.IsNullOrEmpty(url));
76+
}
77+
}
78+
}
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, params string[] domainNames)
59+
:this(schemeType)
60+
{
61+
_domainNames = domainNames;
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using static Tynamix.ObjectFiller.RandomUri;
5+
6+
namespace Tynamix.ObjectFiller
7+
{
8+
/// <summary>
9+
/// Creates a random Url
10+
/// </summary>
11+
public class RandomUrl : IRandomizerPlugin<string>
12+
{
13+
private readonly RandomUri _uri;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RandomUrl"/> class.
17+
/// </summary>
18+
public RandomUrl()
19+
{
20+
_uri = new RandomUri();
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="RandomUrl"/> class.
25+
/// </summary>
26+
/// <param name="schemeType">Type of the scheme.</param>
27+
public RandomUrl(SchemeType schemeType)
28+
{
29+
_uri = new RandomUri(schemeType);
30+
}
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="RandomUrl" /> class.
34+
/// </summary>
35+
/// <param name="schemeType">Type of the scheme.</param>
36+
/// <param name="domainNames">The possible domain names.</param>
37+
public RandomUrl(SchemeType schemeType, params string[] domainNames)
38+
{
39+
_uri = new RandomUri(schemeType, domainNames);
40+
}
41+
42+
/// <summary>
43+
/// Gets random data for type <see cref="string" />
44+
/// </summary>
45+
/// <returns>Random data for type <see cref="string" /></returns>
46+
public string GetValue()
47+
{
48+
return _uri.GetValue().ToString();
49+
}
50+
}
51+
}

Tynamix.ObjectFiller/Setup/FillerSetupItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private void SetDefaultRandomizer()
121121
var mnemonic = new MnemonicString(1);
122122
var doublePlugin = new DoubleRange();
123123
var dateTimeRandomizer = new DateTimeRange(new DateTime(1970, 1, 1));
124+
var uriRandomizer = new RandomUri();
124125
this.TypeToRandomFunc[typeof(string)] = mnemonic.GetValue;
125126
this.TypeToRandomFunc[typeof(bool)] = () => Random.Next(0, 2) == 1;
126127
this.TypeToRandomFunc[typeof(bool?)] = () => new RandomListItem<bool?>(true, false, null).GetValue();
@@ -154,6 +155,7 @@ private void SetDefaultRandomizer()
154155
this.TypeToRandomFunc[typeof(IntPtr?)] = () => default(IntPtr);
155156
this.TypeToRandomFunc[typeof(TimeSpan)] = () => new TimeSpan(Random.Next());
156157
this.TypeToRandomFunc[typeof(TimeSpan?)] = () => new TimeSpan(Random.Next());
158+
this.TypeToRandomFunc[typeof(Uri)] = () => uriRandomizer.GetValue();
157159
#if !NETSTANDARD1_0
158160
this.TypeToRandomFunc[typeof(ArrayList)] = () => ((IRandomizerPlugin<ArrayList>)new Collectionizer<string, MnemonicString>()).GetValue();
159161
#endif

0 commit comments

Comments
 (0)