-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathServiceHandler.cs
More file actions
275 lines (250 loc) · 9.8 KB
/
Copy pathServiceHandler.cs
File metadata and controls
275 lines (250 loc) · 9.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Net;
namespace Ocelot.Testing;
// TODO 1. Refactor in future to make this class a base class of acceptance steps
// TODO 2. Develop async versions for each sync method
public class ServiceHandler : IDisposable
{
#if NET10_0_OR_GREATER
private readonly ConcurrentDictionary<string, IHost> _hosts = new();
#else
private readonly ConcurrentDictionary<string, IWebHost> _hosts = new();
#endif
public void Dispose()
{
foreach (var kv in _hosts)
{
kv.Value?.Dispose();
}
_hosts.Clear();
GC.SuppressFinalize(this);
}
protected Task AddOrStopAsync(
string key,
#if NET10_0_OR_GREATER
IHost host)
#else
IWebHost host)
#endif
{
if (_hosts.TryAdd(key, host))
return Task.CompletedTask;
var h = _hosts.GetOrAdd(key, host);
_hosts[key] = host;
// Shutdown old host
return h.StopAsync().ContinueWith(t => h.Dispose(), TaskContinuationOptions.ExecuteSynchronously);
}
public Task ReleasePortAsync(params int[] ports)
{
List<Task> tasks = new(ports.Length);
foreach (int port in ports)
{
var kv = _hosts.SingleOrDefault(x => new Uri(x.Key).Port == port);
if (kv.Key is null || kv.Value is null)
continue;
var host = kv.Value;
var task = host.StopAsync()
.ContinueWith(t =>
{
host.Dispose();
_hosts.TryRemove(kv);
});
tasks.Add(task);
}
return Task.WhenAll(tasks);
}
#if NET10_0_OR_GREATER
private static IHost CreateHost(Action<IWebHostBuilder> configureWeb)
#else
private static IWebHost CreateHost(Action<IWebHostBuilder> configureWeb)
#endif
{
#if NET10_0_OR_GREATER
var host = TestHostBuilder.CreateHost()
.ConfigureWebHost(configureWeb)
.Build();
#else
var builder = TestHostBuilder.Create();
configureWeb(builder);
var host = builder.Build();
#endif
return host;
}
#if NET10_0_OR_GREATER
public IHost
#else
public IWebHost
#endif
GivenThereIsAServiceRunningOn(string baseUrl, RequestDelegate handler)
{
void ConfigureWeb(IWebHostBuilder builder) => builder
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app => app.Run(handler));
var host = CreateHost(ConfigureWeb);
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
return host;
}
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate handler)
{
void ConfigureWeb(IWebHostBuilder builder) => builder
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app => app.UsePathBase(basePath).Run(handler));
var host = CreateHost(ConfigureWeb);
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
}
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, Action<IServiceCollection> configureServices, RequestDelegate handler)
{
void ConfigureWeb(IWebHostBuilder builder) => builder
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureServices(configureServices)
.Configure(app => app.UsePathBase(basePath).Run(handler));
var host = CreateHost(ConfigureWeb);
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
}
public void GivenThereIsAServiceRunningOnWithKestrelOptions(string baseUrl, string basePath, Action<KestrelServerOptions> options, RequestDelegate handler)
{
void ConfigureWeb(IWebHostBuilder builder) => builder
.UseUrls(baseUrl)
.UseKestrel()
.ConfigureKestrel(options ?? WithDefaultKestrelServerOptions) // !
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app => app.UsePathBase(basePath).Run(handler));
var host = CreateHost(ConfigureWeb);
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
}
internal void WithDefaultKestrelServerOptions(KestrelServerOptions options)
{ }
public void GivenThereIsAHttpsServiceRunningOn(string baseUrl, string basePath, string fileName, string password, int port, RequestDelegate handler)
{
void WithKestrelOptions(KestrelServerOptions options)
=> options.Listen(IPAddress.Loopback, port, o => o.UseHttps(fileName, password));
void ConfigureWeb(IWebHostBuilder builder) => builder
.UseUrls(baseUrl)
.UseKestrel(WithKestrelOptions)
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app => app.UsePathBase(basePath).Run(handler));
var host = CreateHost(ConfigureWeb);
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
}
#region Advanced helpers
public static string Localhost(int port) => $"{Uri.UriSchemeHttp}://localhost:{port}";
public void GivenThereIsAServiceRunningOn(int port, RequestDelegate handler)
=> GivenThereIsAServiceRunningOn(Localhost(port), handler);
public void GivenThereIsAServiceRunningOn(int port, string path, RequestDelegate handler)
=> GivenThereIsAServiceRunningOn(Localhost(port), path, handler);
#endregion
#if NET10_0_OR_GREATER
public IHost
#else
public IWebHost
#endif
GivenThereIsAServiceRunningOn(int port,
Action<WebHostBuilderContext, IConfigurationBuilder>? configureDelegate,
Action<WebHostBuilderContext, ILoggingBuilder>? configureLogging,
Action<IServiceCollection>? configureServices,
Action<IApplicationBuilder>? configureApp,
Action<IWebHostBuilder>? configureWebHost)
=> GivenThereIsAServiceRunningOn(Localhost(port), configureDelegate, configureLogging, configureServices, configureApp, configureWebHost);
#if NET10_0_OR_GREATER
public IHost
#else
public IWebHost
#endif
GivenThereIsAServiceRunningOn(string baseUrl,
Action<WebHostBuilderContext, IConfigurationBuilder>? configureDelegate,
Action<WebHostBuilderContext, ILoggingBuilder>? configureLogging,
Action<IServiceCollection>? configureServices,
Action<IApplicationBuilder>? configureApp,
Action<IWebHostBuilder>? configureWebHost)
{
void ConfigureWeb(IWebHostBuilder builder)
{
builder.UseUrls(baseUrl).UseKestrel();
if (configureDelegate != null) builder.ConfigureAppConfiguration(configureDelegate);
if (configureLogging != null) builder.ConfigureLogging(configureLogging);
if (configureServices != null) builder.ConfigureServices(configureServices);
if (configureApp != null) builder.Configure(configureApp);
configureWebHost?.Invoke(builder);
}
var host = CreateBuilder(ConfigureWeb).Build();
AddOrStopAsync(baseUrl, host).GetAwaiter().GetResult();
host.Start();
return host;
}
#if NET10_0_OR_GREATER
public Task<IHost>
#else
public Task<IWebHost>
#endif
GivenThereIsAServiceRunningOnAsync(int port,
Action<WebHostBuilderContext, IConfigurationBuilder>? configureDelegate,
Action<WebHostBuilderContext, ILoggingBuilder>? configureLogging,
Action<IServiceCollection>? configureServices,
Action<IApplicationBuilder>? configureApp,
Action<IWebHostBuilder>? configureWebHost)
=> GivenThereIsAServiceRunningOnAsync(Localhost(port), configureDelegate, configureLogging, configureServices, configureApp, configureWebHost);
#if NET10_0_OR_GREATER
public Task<IHost>
#else
public Task<IWebHost>
#endif
GivenThereIsAServiceRunningOnAsync(string baseUrl,
Action<WebHostBuilderContext, IConfigurationBuilder>? configureDelegate,
Action<WebHostBuilderContext, ILoggingBuilder>? configureLogging,
Action<IServiceCollection>? configureServices,
Action<IApplicationBuilder>? configureApp,
Action<IWebHostBuilder>? configureWebHost)
{
void ConfigureWeb(IWebHostBuilder builder)
{
builder.UseUrls(baseUrl).UseKestrel();
if (configureDelegate != null) builder.ConfigureAppConfiguration(configureDelegate);
if (configureLogging != null) builder.ConfigureLogging(configureLogging);
if (configureServices != null) builder.ConfigureServices(configureServices);
if (configureApp != null) builder.Configure(configureApp);
configureWebHost?.Invoke(builder);
}
var host = CreateBuilder(ConfigureWeb).Build();
return AddOrStopAsync(baseUrl, host)
.ContinueWith(t => host.StartAsync())
.ContinueWith(t => host, TaskContinuationOptions.ExecuteSynchronously);
}
#if NET10_0_OR_GREATER
private static IHostBuilder
#else
private static IWebHostBuilder
#endif
CreateBuilder(Action<IWebHostBuilder> configureWeb)
{
#if NET10_0_OR_GREATER
return TestHostBuilder.CreateHost()
.ConfigureWebHost(configureWeb);
#else
var builder = TestHostBuilder.Create();
configureWeb(builder);
return builder;
#endif
}
}