Skip to content

Commit 3846824

Browse files
committed
Added a mock requester to test async capabilities
1 parent adc490f commit 3846824

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

AngleSharp.Scripting.JavaScript.Tests/AngleSharp.Scripting.JavaScript.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="GeneratorTests.cs" />
8383
<Compile Include="InteractionTests.cs" />
8484
<Compile Include="JqueryTests.cs" />
85+
<Compile Include="Mocks\DelayedRequester.cs" />
8586
<Compile Include="Properties\AssemblyInfo.cs" />
8687
<Compile Include="ScriptEvalTests.cs" />
8788
<Compile Include="ScriptingTests.cs" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace AngleSharp.Scripting.JavaScript.Tests.Mocks
2+
{
3+
using AngleSharp.Network;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Net;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
sealed class DelayedRequester : IRequester
13+
{
14+
readonly Int32 _delay;
15+
readonly String _message;
16+
17+
public DelayedRequester(Int32 delay, String message)
18+
{
19+
_delay = delay;
20+
_message = message;
21+
}
22+
23+
public async Task<IResponse> RequestAsync(IRequest request, CancellationToken cancel)
24+
{
25+
await Task.Delay(_delay, cancel);
26+
return new Response(_message, request.Address);
27+
}
28+
29+
public Boolean SupportsProtocol(String protocol)
30+
{
31+
return true;
32+
}
33+
34+
class Response : IResponse
35+
{
36+
readonly MemoryStream _content;
37+
readonly Dictionary<String, String> _headers;
38+
readonly Url _address;
39+
40+
public Response(String message, Url address)
41+
{
42+
var buffer = Encoding.UTF8.GetBytes(message);
43+
_content = new MemoryStream(buffer);
44+
_headers = new Dictionary<String, String>();
45+
_address = address;
46+
}
47+
48+
public Url Address
49+
{
50+
get { return _address; }
51+
}
52+
53+
public Stream Content
54+
{
55+
get { return _content; }
56+
}
57+
58+
public IDictionary<String, String> Headers
59+
{
60+
get { return _headers; }
61+
}
62+
63+
public HttpStatusCode StatusCode
64+
{
65+
get { return HttpStatusCode.OK; }
66+
}
67+
68+
public void Dispose()
69+
{
70+
_content.Dispose();
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)