-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
165 lines (133 loc) · 5.05 KB
/
Copy pathProgram.cs
File metadata and controls
165 lines (133 loc) · 5.05 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using fetcher_tester;
using System.Security.Cryptography.X509Certificates;
using Xunit;
var builder = WebApplication.CreateBuilder(args);
Assert.NotNull(builder);
Console.WriteLine($"Current directory: {Directory.GetCurrentDirectory()}");
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory()))
{
Console.WriteLine($"File: {file}");
}
var httpsEnabled = false;
if (File.Exists("certificate.pfx"))
{
httpsEnabled = true;
var cert = X509CertificateLoader.LoadPkcs12FromFile("certificate.pfx", null, X509KeyStorageFlags.DefaultKeySet);
Console.WriteLine($"Loaded certificate: {cert.Subject}");
}
var listenUrls = new List<string>();
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenAnyIP(80, listenOptions =>
{
Console.WriteLine($"Configured HTTP endpoint at {listenOptions.EndPoint}");
});
listenUrls.Add("http://*:80");
serverOptions.ListenAnyIP(8080, listenOptions =>
{
Console.WriteLine($"Configured HTTP endpoint at {listenOptions.EndPoint}");
});
listenUrls.Add("http://*:8080");
if (httpsEnabled)
{
serverOptions.ListenAnyIP(443, listenOptions =>
{
listenOptions.UseHttps("certificate.pfx", null, httpsOptions =>
{
httpsOptions.HandshakeTimeout = TimeSpan.FromSeconds(5);
httpsOptions.SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12;
});
Console.WriteLine($"Configured HTTPS endpoint at {listenOptions.EndPoint}");
});
listenUrls.Add("https://*:443");
}
});
builder.WebHost.UseUrls([.. listenUrls]);
DotNetEnv.Env.Load();
builder.Configuration
.AddEnvironmentVariables(prefix: "fetcher_tester_")
.AddCommandLine(args);
builder.Logging.SetMinimumLevel(LogLevel.Warning);
var app = builder.Build();
Assert.NotNull(app);
AppConfig.SetConfiguration(builder.Configuration);
string? specific_test = builder.Configuration["test_to_run"];
string? relayContext = builder.Configuration["relay_context"];
Dictionary<string, (RequestDelegate?, RequestDelegate)>? testLookup = null;
MapEndpoints(app);
Assert.NotNull(testLookup);
CancellationTokenSource endingToken = new CancellationTokenSource();
if (!string.IsNullOrWhiteSpace(relayContext))
{
Console.WriteLine($"Creating new relay consumer for context '{relayContext}'.");
var consumer = new RelayConsumer();
if (consumer != null)
{
Console.WriteLine($"Consumer created successfully- running.");
await Task.Run(consumer.Run, endingToken.Token);
}
else
Console.WriteLine($"Error: Relay consumer creation for context '{relayContext}' has failed.");
}
Console.WriteLine($"Running webhost and hosting application.");
app.Run();
endingToken.Cancel();
void MapEndpoints(WebApplication app)
{
app.Map("/run-tests", RunAllTests);
var actionTests = new ActionTests();
actionTests.ValidationConfiguration();
var playbackTests = new PlayTests();
playbackTests.ValidationConfiguration();
testLookup = new Dictionary<string, (RequestDelegate?, RequestDelegate)>() { { "basic", new(null, BasicEndpoint) } };
testLookup = testLookup.Concat(actionTests.GenerateTestLookup()).ToDictionary();
testLookup = testLookup.Concat(playbackTests.GenerateTestLookup()).ToDictionary();
foreach (var kvp in testLookup)
{
if (kvp.Value.Item1 != null)
{
Console.WriteLine($"Adding test endpoint for: {kvp.Key.TestNameFromLabel()}");
app.Map($"/run-test/{kvp.Key.TestNameFromLabel()}", kvp.Value.Item1);
}
}
foreach (var kvp in testLookup)
{
Console.WriteLine($"Adding endpoint for: {kvp.Key.TestEndpointFromLabel()}");
app.Map($"/{kvp.Key.TestEndpointFromLabel()}", kvp.Value.Item2);
}
}
async Task RunAllTests(HttpContext context)
{
Assert.NotNull(context);
context.RequestContextLog();
if (testLookup == null)
{
Console.WriteLine("Error: Could not locate any tests to run.");
return;
}
if (!string.IsNullOrEmpty(specific_test))
{
if (!testLookup.TryGetValue(specific_test, out var specific_kvp) || specific_kvp.Item1 == null)
{
Console.WriteLine($"Error: Could not locate specific test {specific_test} to run.");
return;
}
await specific_kvp.Item1.Invoke(context);
Console.WriteLine($"Test {specific_test} has completed successfully.");
}
foreach (var kvp in testLookup)
{
if (kvp.Value.Item1 == null)
continue;
await kvp.Value.Item1.Invoke(context);
if (!context.ValidateTestResults(kvp.Value.Item1.Method.Name))
return;
}
}
async Task BasicEndpoint(HttpContext context)
{
Assert.NotNull(context);
await Task.CompletedTask;
context.RequestContextLog();
context.Response.StatusCode = 200;
}