Skip to content

Commit ed84b51

Browse files
committed
Improved and extended the tests
1 parent ea4d00d commit ed84b51

File tree

10 files changed

+221
-150
lines changed

10 files changed

+221
-150
lines changed

src/AngleSharp.Io.Tests/AngleSharp.Io.Tests.csproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
<WarningLevel>4</WarningLevel>
3737
</PropertyGroup>
3838
<ItemGroup>
39-
<Reference Include="AngleSharp, Version=0.9.7.21214, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea, processorArchitecture=MSIL">
40-
<HintPath>..\packages\AngleSharp.0.9.7\lib\net45\AngleSharp.dll</HintPath>
39+
<Reference Include="AngleSharp, Version=0.9.8.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea, processorArchitecture=MSIL">
40+
<HintPath>..\packages\AngleSharp.0.9.8\lib\net45\AngleSharp.dll</HintPath>
4141
<Private>True</Private>
4242
</Reference>
43-
<Reference Include="FluentAssertions, Version=4.12.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
44-
<HintPath>..\packages\FluentAssertions.4.12.0\lib\net45\FluentAssertions.dll</HintPath>
43+
<Reference Include="FluentAssertions, Version=4.13.1.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
44+
<HintPath>..\packages\FluentAssertions.4.13.1\lib\net45\FluentAssertions.dll</HintPath>
4545
<Private>True</Private>
4646
</Reference>
47-
<Reference Include="FluentAssertions.Core, Version=4.12.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
48-
<HintPath>..\packages\FluentAssertions.4.12.0\lib\net45\FluentAssertions.Core.dll</HintPath>
47+
<Reference Include="FluentAssertions.Core, Version=4.13.1.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
48+
<HintPath>..\packages\FluentAssertions.4.13.1\lib\net45\FluentAssertions.Core.dll</HintPath>
4949
<Private>True</Private>
5050
</Reference>
5151
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
@@ -72,11 +72,12 @@
7272
</ProjectReference>
7373
</ItemGroup>
7474
<ItemGroup>
75+
<Compile Include="DisposableStream.cs" />
7576
<Compile Include="Helper.cs" />
77+
<Compile Include="Network\CookieHandlingTests.cs" />
7678
<Compile Include="Network\FileRequesterTests.cs" />
7779
<Compile Include="Network\FtpRequesterTests.cs" />
7880
<Compile Include="Network\HttpClientRequesterTests.cs" />
79-
<Compile Include="Network\Mocks\Request.cs" />
8081
<Compile Include="Network\Mocks\HttpMockHandler.cs" />
8182
<Compile Include="Network\Mocks\HttpMockState.cs" />
8283
<Compile Include="Network\ResponseTests.cs" />
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace AngleSharp.Io.Tests
2+
{
3+
using System;
4+
using System.IO;
5+
6+
sealed class DisposableStream : Stream
7+
{
8+
public override Boolean CanRead
9+
{
10+
get { return false; }
11+
}
12+
13+
public override Boolean CanSeek
14+
{
15+
get { return false; }
16+
}
17+
18+
public override Boolean CanWrite
19+
{
20+
get { return false; }
21+
}
22+
23+
public override Int64 Length
24+
{
25+
get { return 0; }
26+
}
27+
28+
public override Int64 Position
29+
{
30+
get;
31+
set;
32+
}
33+
34+
public Boolean Disposed
35+
{
36+
get;
37+
set;
38+
}
39+
40+
public override void Flush()
41+
{
42+
throw new NotImplementedException();
43+
}
44+
45+
public override Int64 Seek(Int64 offset, SeekOrigin origin)
46+
{
47+
throw new NotImplementedException();
48+
}
49+
50+
public override void SetLength(Int64 value)
51+
{
52+
throw new NotImplementedException();
53+
}
54+
55+
public override Int32 Read(Byte[] buffer, Int32 offset, Int32 count)
56+
{
57+
throw new NotImplementedException();
58+
}
59+
60+
public override void Write(Byte[] buffer, Int32 offset, Int32 count)
61+
{
62+
throw new NotImplementedException();
63+
}
64+
65+
protected override void Dispose(Boolean disposing)
66+
{
67+
Disposed = true;
68+
base.Dispose(disposing);
69+
}
70+
}
71+
}

src/AngleSharp.Io.Tests/Helper.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
namespace AngleSharp.Io.Tests
22
{
3+
using NUnit.Framework;
34
using System;
4-
using System.IO;
55
using System.Net.NetworkInformation;
6-
using NUnit.Framework;
76

87
/// <summary>
98
/// Small (but quite useable) code to enable / disable some
109
/// test(s) depending on the current network status.
1110
/// Taken from
1211
/// http://stackoverflow.com/questions/520347/c-sharp-how-do-i-check-for-a-network-connection
1312
/// </summary>
14-
class Helper
13+
static class Helper
1514
{
1615
/// <summary>
1716
/// Indicates whether any network connection is available
@@ -22,7 +21,9 @@ class Helper
2221
public static Boolean IsNetworkAvailable()
2322
{
2423
if (IsNetworkAvailable(0))
24+
{
2525
return true;
26+
}
2627

2728
Assert.Inconclusive("No network has been detected. Test skipped.");
2829
return false;
@@ -36,32 +37,32 @@ public static Boolean IsNetworkAvailable()
3637
/// <returns>True if a network connection is available; otherwise false.</returns>
3738
public static Boolean IsNetworkAvailable(Int64 minimumSpeed)
3839
{
39-
if (!NetworkInterface.GetIsNetworkAvailable())
40-
return false;
41-
42-
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
40+
if (NetworkInterface.GetIsNetworkAvailable())
4341
{
44-
// discard because of standard reasons
45-
if ((ni.OperationalStatus != OperationalStatus.Up) ||
46-
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
47-
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
48-
continue;
42+
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
43+
{
44+
// discard because of standard reasons
45+
if ((ni.OperationalStatus != OperationalStatus.Up) ||
46+
(ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
47+
(ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
48+
continue;
4949

50-
// this allow to filter modems, serial, etc.
51-
// I use 10000000 as a minimum speed for most cases
52-
if (ni.Speed < minimumSpeed)
53-
continue;
50+
// this allow to filter modems, serial, etc.
51+
// I use 10000000 as a minimum speed for most cases
52+
if (ni.Speed < minimumSpeed)
53+
continue;
5454

55-
// discard virtual cards (virtual box, virtual pc, etc.)
56-
if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
57-
(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
58-
continue;
55+
// discard virtual cards (virtual box, virtual pc, etc.)
56+
if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
57+
(ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
58+
continue;
5959

60-
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
61-
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
62-
continue;
60+
// discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
61+
if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
62+
continue;
6363

64-
return true;
64+
return true;
65+
}
6566
}
6667

6768
return false;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace AngleSharp.Io.Tests.Network
2+
{
3+
using NUnit.Framework;
4+
using System;
5+
using System.Threading.Tasks;
6+
7+
[TestFixture]
8+
public class CookieHandlingTests
9+
{
10+
[Test]
11+
public async Task SettingOneCookiesInOneRequestAppearsInDocument()
12+
{
13+
if (Helper.IsNetworkAvailable())
14+
{
15+
var url = "https://httpbin.org/cookies/set?k1=v1";
16+
var config = Configuration.Default.WithCookies().WithRequesters();
17+
var context = BrowsingContext.New(config);
18+
var document = await context.OpenAsync(url);
19+
20+
Assert.AreEqual("k1=v1", document.Cookie);
21+
}
22+
}
23+
24+
[Test]
25+
public async Task SettingTwoCookiesInOneRequestAppearsInDocument()
26+
{
27+
if (Helper.IsNetworkAvailable())
28+
{
29+
var url = "https://httpbin.org/cookies/set?k2=v2&k1=v1";
30+
var config = Configuration.Default.WithCookies().WithRequesters();
31+
var context = BrowsingContext.New(config);
32+
var document = await context.OpenAsync(url);
33+
34+
Assert.AreEqual("k2=v2; k1=v1", document.Cookie);
35+
}
36+
}
37+
38+
[Test]
39+
public async Task SettingThreeCookiesInOneRequestAppearsInDocument()
40+
{
41+
if (Helper.IsNetworkAvailable())
42+
{
43+
var url = "https://httpbin.org/cookies/set?test=baz&k2=v2&k1=v1&foo=bar";
44+
var config = Configuration.Default.WithCookies().WithRequesters();
45+
var context = BrowsingContext.New(config);
46+
var document = await context.OpenAsync(url);
47+
48+
Assert.AreEqual("test=baz; k2=v2; k1=v1; foo=bar", document.Cookie);
49+
}
50+
}
51+
52+
[Test]
53+
public async Task SettingThreeCookiesInOneRequestAreTransportedToNextRequest()
54+
{
55+
if (Helper.IsNetworkAvailable())
56+
{
57+
var baseUrl = "https://httpbin.org/cookies";
58+
var url = baseUrl + "/set?test=baz&k2=v2&k1=v1&foo=bar";
59+
var config = Configuration.Default.WithCookies().WithRequesters();
60+
var context = BrowsingContext.New(config);
61+
await context.OpenAsync(url);
62+
var document = await context.OpenAsync(baseUrl);
63+
64+
Assert.AreEqual(@"{
65+
""cookies"": {
66+
""foo"": ""bar"",
67+
""k1"": ""v1"",
68+
""k2"": ""v2"",
69+
""test"": ""baz""
70+
}
71+
}
72+
".Replace(Environment.NewLine, "\n"), document.Body.TextContent);
73+
}
74+
}
75+
76+
[Test]
77+
public async Task SettingCookieIsPreservedViaRedirect()
78+
{
79+
if (Helper.IsNetworkAvailable())
80+
{
81+
var cookieUrl = "https://httpbin.org/cookies/set?test=baz";
82+
var redirectUrl = "http://httpbin.org/redirect-to?url=http%3A%2F%2Fhttpbin.org%2Fcookies";
83+
var config = Configuration.Default.WithCookies().WithRequesters();
84+
var context = BrowsingContext.New(config);
85+
await context.OpenAsync(cookieUrl);
86+
var document = await context.OpenAsync(redirectUrl);
87+
88+
Assert.AreEqual(@"{
89+
""cookies"": {
90+
""test"": ""baz""
91+
}
92+
}
93+
".Replace(Environment.NewLine, "\n"), document.Body.TextContent);
94+
}
95+
}
96+
}
97+
}

src/AngleSharp.Io.Tests/Network/FileRequesterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[TestFixture]
1515
public class FileRequesterTests
1616
{
17-
static String GetLocalPath()
17+
private static String GetLocalPath()
1818
{
1919
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
2020
var path = Path.Combine(directory, "TestContent.txt");

src/AngleSharp.Io.Tests/Network/Mocks/HttpMockHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7-
class HttpMockHandler : DelegatingHandler
7+
sealed class HttpMockHandler : DelegatingHandler
88
{
9-
readonly HttpMockState _testState;
9+
private readonly HttpMockState _testState;
1010

1111
public HttpMockHandler(HttpMockState testState)
1212
{
@@ -18,7 +18,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
1818
_testState.HttpRequestMessage = request;
1919

2020
if (request.Content != null)
21+
{
2122
_testState.HttpRequestMessageContent = await request.Content.ReadAsByteArrayAsync();
23+
}
2224

2325
_testState.HttpResponseMessage.RequestMessage = request;
2426
return _testState.HttpResponseMessage;

src/AngleSharp.Io.Tests/Network/Mocks/HttpMockState.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace AngleSharp.Io.Tests.Network.Mocks
22
{
33
using AngleSharp.Io.Network;
4+
using AngleSharp.Network.Default;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Net;
89
using System.Net.Http;
910
using System.Text;
1011

11-
class HttpMockState
12+
sealed class HttpMockState
1213
{
1314
public HttpMockState()
1415
{
@@ -22,23 +23,23 @@ public HttpMockState()
2223
Method = AngleSharp.Network.HttpMethod.Post,
2324
Address = new Url("http://example/path?query=value"),
2425
Headers = new Dictionary<String, String>
25-
{
26-
{"User-Agent", "Foo/2.0"},
27-
{"Cookie", "foo=bar"},
28-
{"Content-Type", "application/json"},
29-
{"Content-Length", "9"}
30-
},
26+
{
27+
{"User-Agent", "Foo/2.0"},
28+
{"Cookie", "foo=bar"},
29+
{"Content-Type", "application/json"},
30+
{"Content-Length", "9"}
31+
},
3132
Content = new MemoryStream(Encoding.UTF8.GetBytes("\"request\""))
3233
};
3334
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
3435
{
3536
Content = new StringContent("\"response\"", Encoding.UTF8, "application/json"),
3637
Headers =
37-
{
38-
{"Server", "Fake"},
39-
{"X-Powered-By", "Magic"},
40-
{"X-CSV", new[] {"foo", "bar"}}
41-
}
38+
{
39+
{"Server", "Fake"},
40+
{"X-Powered-By", "Magic"},
41+
{"X-CSV", new[] {"foo", "bar"}}
42+
}
4243
};
4344

4445
// setup

0 commit comments

Comments
 (0)