-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFixture.cs
73 lines (62 loc) · 1.91 KB
/
TestFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.Net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace FreeProxySharp.Test
{
public class TestFixture : IDisposable
{
/// <summary>
/// UNIT test configuration
/// </summary>
public TestOptions Options;
/// <summary>
/// DI
/// </summary>
public IServiceProvider Services { get; private set; }
/// <summary>
/// initialize
/// </summary>
public TestFixture()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true)
.Build();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.Debug()
.CreateLogger();
// initialize configuration
Options = new TestOptions();
configuration.GetSection("Http").Bind(Options);
// DI
var services = new ServiceCollection();
services.AddSingleton(s => Log.Logger);
services.AddSingleton<IHttpProxyConfiguration>(Options);
services.AddSingleton<HttpProxyFactory>();
// common IHttpClientFactory
services.AddHttpClient();
// parse & check proxies ; save it into configuration
Options.CheckAndAssignToConfig(required: 1, throwWhenLessThanRequired: true);
// common client, with all settings from Configuration
services.AddHttpClient("COMMON", Options);
// common client, with retry = 5
services.AddHttpClient("5RETRY", retry: 5);
// common client, with configured for retry when 404 status found (example)
services.AddHttpClient("404TEST", Options, whenRetry: res => res.StatusCode == HttpStatusCode.NotFound);
// proxy client, with all proxies gets by CheckAndAssignToConfig
services.AddHttpClientProxy("PROXY", Options);
Services = services.BuildServiceProvider();
}
/// <summary>
/// clean up
/// </summary>
public void Dispose()
{
}
}
}