Skip to content

Commit 048cc5e

Browse files
committed
Refactored tests
1 parent 8898f03 commit 048cc5e

File tree

6 files changed

+177
-142
lines changed

6 files changed

+177
-142
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@
9494
<ItemGroup>
9595
<Compile Include="Helper.cs" />
9696
<Compile Include="Network\HttpClientRequesterTests.cs" />
97+
<Compile Include="Network\Mocks\Request.cs" />
98+
<Compile Include="Network\Mocks\HttpMockHandler.cs" />
99+
<Compile Include="Network\Mocks\HttpMockState.cs" />
97100
<Compile Include="Network\ResponseTests.cs" />
101+
<Compile Include="Network\WebSocketTests.cs" />
98102
<Compile Include="Properties\AssemblyInfo.cs" />
99103
</ItemGroup>
100104
<ItemGroup>
Lines changed: 13 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
namespace AngleSharp.Io.Tests.Network
22
{
3+
using AngleSharp.Io.Network;
4+
using AngleSharp.Io.Tests.Network.Mocks;
5+
using AngleSharp.Network;
6+
using AngleSharp.Services.Default;
7+
using FluentAssertions;
8+
using NUnit.Framework;
39
using System;
4-
using System.Collections.Generic;
510
using System.IO;
611
using System.Linq;
7-
using System.Net;
812
using System.Net.Http;
913
using System.Text;
1014
using System.Threading;
1115
using System.Threading.Tasks;
12-
using AngleSharp.Io.Network;
13-
using AngleSharp.Network;
14-
using AngleSharp.Services.Default;
15-
using FluentAssertions;
16-
using NUnit.Framework;
17-
using NetHttpMethod = System.Net.Http.HttpMethod;
1816
using AngleSharpHttpMethod = AngleSharp.Network.HttpMethod;
17+
using NetHttpMethod = System.Net.Http.HttpMethod;
1918

2019

2120
[TestFixture]
@@ -25,7 +24,7 @@ public class HttpClientRequesterTests
2524
public async Task RequestWithContent()
2625
{
2726
// ARRANGE
28-
var ts = new TestState();
27+
var ts = new HttpMockState();
2928

3029
// ACT
3130
await ts.Target.RequestAsync(ts.Request, CancellationToken.None);
@@ -48,7 +47,7 @@ public async Task RequestWithContent()
4847
public async Task RequestWithoutContent()
4948
{
5049
// ARRANGE
51-
var ts = new TestState {Request = {Content = null, Method = AngleSharpHttpMethod.Get}};
50+
var ts = new HttpMockState { Request = { Content = null, Method = AngleSharpHttpMethod.Get } };
5251

5352
// ACT
5453
await ts.Target.RequestAsync(ts.Request, CancellationToken.None);
@@ -68,7 +67,7 @@ public async Task RequestWithoutContent()
6867
public async Task ResponseWithContent()
6968
{
7069
// ARRANGE
71-
var ts = new TestState();
70+
var ts = new HttpMockState();
7271

7372
// ACT
7473
var response = await ts.Target.RequestAsync(ts.Request, CancellationToken.None);
@@ -89,7 +88,7 @@ public async Task ResponseWithContent()
8988
public async Task ResponseWithoutContent()
9089
{
9190
// ARRANGE
92-
var ts = new TestState {HttpResponseMessage = {Content = null}};
91+
var ts = new HttpMockState { HttpResponseMessage = { Content = null } };
9392

9493
// ACT
9594
var response = await ts.Target.RequestAsync(ts.Request, CancellationToken.None);
@@ -108,14 +107,14 @@ public async Task ResponseWithoutContent()
108107
public void SupportsHttp()
109108
{
110109
// ARRANGE, ACT, ASSERT
111-
new TestState().Target.SupportsProtocol("HTTP").Should().BeTrue();
110+
new HttpMockState().Target.SupportsProtocol("HTTP").Should().BeTrue();
112111
}
113112

114113
[Test]
115114
public void SupportsHttps()
116115
{
117116
// ARRANGE, ACT, ASSERT
118-
new TestState().Target.SupportsProtocol("HTTPS").Should().BeTrue();
117+
new HttpMockState().Target.SupportsProtocol("HTTPS").Should().BeTrue();
119118
}
120119

121120
[Test]
@@ -138,133 +137,5 @@ public async Task EndToEnd()
138137
document.QuerySelector("h1").ToHtml().Should().Be("<h1>Herman Melville - Moby-Dick</h1>");
139138
}
140139
}
141-
142-
class TestState
143-
{
144-
public TestState()
145-
{
146-
// dependencies
147-
148-
TestHandler = new TestHandler(this);
149-
HttpClient = new HttpClient(TestHandler);
150-
151-
// data
152-
Request = new Request
153-
{
154-
Method = AngleSharpHttpMethod.Post,
155-
Address = new Url("http://example/path?query=value"),
156-
Headers = new Dictionary<String, String>
157-
{
158-
{"User-Agent", "Foo/2.0"},
159-
{"Cookie", "foo=bar"},
160-
{"Content-Type", "application/json"},
161-
{"Content-Length", "9"}
162-
},
163-
Content = new MemoryStream(Encoding.UTF8.GetBytes("\"request\""))
164-
};
165-
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
166-
{
167-
Content = new StringContent("\"response\"", Encoding.UTF8, "application/json"),
168-
Headers =
169-
{
170-
{"Server", "Fake"},
171-
{"X-Powered-By", "Magic"},
172-
{"X-CSV", new[] {"foo", "bar"}}
173-
}
174-
};
175-
176-
// setup
177-
Target = new HttpClientRequester(HttpClient);
178-
}
179-
180-
public Request Request
181-
{
182-
get;
183-
private set;
184-
}
185-
186-
public HttpClientRequester Target
187-
{
188-
get;
189-
private set;
190-
}
191-
192-
public HttpClient HttpClient
193-
{
194-
get;
195-
private set;
196-
}
197-
198-
public TestHandler TestHandler
199-
{
200-
get;
201-
private set;
202-
}
203-
204-
public HttpResponseMessage HttpResponseMessage
205-
{
206-
get;
207-
private set;
208-
}
209-
210-
public HttpRequestMessage HttpRequestMessage
211-
{
212-
get;
213-
set;
214-
}
215-
216-
public Byte[] HttpRequestMessageContent
217-
{
218-
get;
219-
set;
220-
}
221-
}
222-
223-
class TestHandler : DelegatingHandler
224-
{
225-
readonly TestState _testState;
226-
227-
public TestHandler(TestState testState)
228-
{
229-
_testState = testState;
230-
}
231-
232-
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
233-
{
234-
_testState.HttpRequestMessage = request;
235-
if (request.Content != null)
236-
_testState.HttpRequestMessageContent = await request.Content.ReadAsByteArrayAsync();
237-
238-
_testState.HttpResponseMessage.RequestMessage = request;
239-
return _testState.HttpResponseMessage;
240-
}
241-
}
242-
243-
class Request : IRequest
244-
{
245-
public AngleSharpHttpMethod Method
246-
{
247-
get;
248-
set;
249-
}
250-
251-
public Url Address
252-
{
253-
get;
254-
set;
255-
}
256-
257-
public Dictionary<String, String> Headers
258-
{
259-
get;
260-
set;
261-
}
262-
263-
public Stream Content
264-
{
265-
get;
266-
set;
267-
}
268-
}
269140
}
270141
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace AngleSharp.Io.Tests.Network.Mocks
2+
{
3+
using System.Net.Http;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
class HttpMockHandler : DelegatingHandler
8+
{
9+
readonly HttpMockState _testState;
10+
11+
public HttpMockHandler(HttpMockState testState)
12+
{
13+
_testState = testState;
14+
}
15+
16+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
17+
{
18+
_testState.HttpRequestMessage = request;
19+
20+
if (request.Content != null)
21+
_testState.HttpRequestMessageContent = await request.Content.ReadAsByteArrayAsync();
22+
23+
_testState.HttpResponseMessage.RequestMessage = request;
24+
return _testState.HttpResponseMessage;
25+
}
26+
}
27+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace AngleSharp.Io.Tests.Network.Mocks
2+
{
3+
using AngleSharp.Io.Network;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Text;
10+
11+
class HttpMockState
12+
{
13+
public HttpMockState()
14+
{
15+
// dependencies
16+
TestHandler = new HttpMockHandler(this);
17+
HttpClient = new HttpClient(TestHandler);
18+
19+
// data
20+
Request = new Request
21+
{
22+
Method = AngleSharp.Network.HttpMethod.Post,
23+
Address = new Url("http://example/path?query=value"),
24+
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+
},
31+
Content = new MemoryStream(Encoding.UTF8.GetBytes("\"request\""))
32+
};
33+
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
34+
{
35+
Content = new StringContent("\"response\"", Encoding.UTF8, "application/json"),
36+
Headers =
37+
{
38+
{"Server", "Fake"},
39+
{"X-Powered-By", "Magic"},
40+
{"X-CSV", new[] {"foo", "bar"}}
41+
}
42+
};
43+
44+
// setup
45+
Target = new HttpClientRequester(HttpClient);
46+
}
47+
48+
public Request Request
49+
{
50+
get;
51+
private set;
52+
}
53+
54+
public HttpClientRequester Target
55+
{
56+
get;
57+
private set;
58+
}
59+
60+
public HttpClient HttpClient
61+
{
62+
get;
63+
private set;
64+
}
65+
66+
public HttpMockHandler TestHandler
67+
{
68+
get;
69+
private set;
70+
}
71+
72+
public HttpResponseMessage HttpResponseMessage
73+
{
74+
get;
75+
private set;
76+
}
77+
78+
public HttpRequestMessage HttpRequestMessage
79+
{
80+
get;
81+
set;
82+
}
83+
84+
public Byte[] HttpRequestMessageContent
85+
{
86+
get;
87+
set;
88+
}
89+
}
90+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace AngleSharp.Io.Tests.Network.Mocks
2+
{
3+
using AngleSharp.Network;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
8+
class Request : IRequest
9+
{
10+
public HttpMethod Method
11+
{
12+
get;
13+
set;
14+
}
15+
16+
public Url Address
17+
{
18+
get;
19+
set;
20+
}
21+
22+
public Dictionary<String, String> Headers
23+
{
24+
get;
25+
set;
26+
}
27+
28+
public Stream Content
29+
{
30+
get;
31+
set;
32+
}
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AngleSharp.Io.Tests.Network
2+
{
3+
using NUnit.Framework;
4+
5+
[TestFixture]
6+
public class WebSocketTests
7+
{
8+
}
9+
}

0 commit comments

Comments
 (0)