Skip to content

Commit d43d4f5

Browse files
author
Yehudah Asher[user]
committed
IConfiguration.WithHttpRequesters(HttpMessageHandler)
This method is used to inject HttpMessageHandler. It allows mocking and unit tests for HttpClientListener.
1 parent 17ed67d commit d43d4f5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

AngleSharp.Io/ConfigurationExtensions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AngleSharp.Network.Default;
77
using AngleSharp.Services;
88
using System.Linq;
9+
using System.Net.Http;
910

1011
/// <summary>
1112
/// Additional extensions for improved requesters.
@@ -28,5 +29,23 @@ public static IConfiguration WithRequesters(this IConfiguration configuration)
2829

2930
return configuration;
3031
}
32+
33+
34+
/// <summary>
35+
/// Adds a loader service that comes with all (improved) requesters.
36+
/// </summary>
37+
/// <param name="configuration">The configuration to use.</param>
38+
/// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param>
39+
/// <returns>The new configuration.</returns>
40+
public static IConfiguration WithHttpRequesters(this IConfiguration configuration, HttpMessageHandler httpMessageHandler)
41+
{
42+
if (!configuration.Services.OfType<ILoaderService>().Any())
43+
{
44+
var httpClient = new HttpClient(httpMessageHandler);
45+
var requesters = new IRequester[] { new HttpClientRequester(httpClient), new DataRequester() };
46+
var service = new LoaderService(requesters);
47+
return configuration.With(service);
48+
}
49+
}
3150
}
32-
}
51+
}

0 commit comments

Comments
 (0)