|
| 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