-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathModuleTests.cs
205 lines (173 loc) · 6.39 KB
/
ModuleTests.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModulesLibrary;
using Xunit;
namespace Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests;
public class ModuleTests
{
private static readonly string[] Initial = new[]
{
nameof(HttpApplication.BeginRequest),
nameof(HttpApplication.AuthenticateRequest),
nameof(HttpApplication.PostAuthenticateRequest),
nameof(HttpApplication.AuthorizeRequest),
nameof(HttpApplication.PostAuthorizeRequest),
nameof(HttpApplication.ResolveRequestCache),
nameof(HttpApplication.PostResolveRequestCache),
nameof(HttpApplication.MapRequestHandler),
nameof(HttpApplication.PostMapRequestHandler),
nameof(HttpApplication.AcquireRequestState),
nameof(HttpApplication.PostAcquireRequestState),
nameof(HttpApplication.PreRequestHandlerExecute),
nameof(HttpApplication.PostRequestHandlerExecute),
nameof(HttpApplication.ReleaseRequestState),
nameof(HttpApplication.PostReleaseRequestState),
nameof(HttpApplication.UpdateRequestCache),
nameof(HttpApplication.PostUpdateRequestCache),
};
private static readonly string[] Always = new[]
{
nameof(HttpApplication.LogRequest),
nameof(HttpApplication.PostLogRequest),
nameof(HttpApplication.EndRequest),
nameof(HttpApplication.PreSendRequestHeaders),
};
public static IEnumerable<object[]> GetAllEvents()
=> Initial.Concat(Always).Select(o => new[] { o });
[MemberData(nameof(GetAllEvents))]
[Theory]
public async Task EndModuleEarly(string notification)
{
var expected = GetNotificationsUpTo(notification);
var result = await RunAsync(ModuleTestModule.End, notification);
Assert.Equal(expected, result);
}
[MemberData(nameof(GetAllEvents))]
[Theory]
public async Task CompleteModuleEarly(string notification)
{
var expected = GetNotificationsUpTo(notification);
var result = await RunAsync(ModuleTestModule.Complete, notification);
Assert.Equal(expected, result);
}
[MemberData(nameof(GetAllEvents))]
[Theory]
public async Task ModulesThrow(string notification)
{
var expected = GetExpected(notification).ToList();
var result = await RunAsync(ModuleTestModule.Throw, notification);
Assert.Equal(expected, result);
static IEnumerable<string> GetExpected(string notification)
{
foreach (var item in GetNotificationsUpTo(notification))
{
yield return item;
if (string.Equals(item, notification, StringComparison.Ordinal))
{
yield return nameof(HttpApplication.Error);
}
}
}
}
private static IEnumerable<string> GetNotificationsUpTo(string notification)
{
foreach (var n in Initial)
{
yield return n;
if (string.Equals(n, notification, StringComparison.Ordinal))
{
break;
}
}
foreach (var n in Always)
{
yield return n;
}
}
private static async Task<List<string>> RunAsync(string action, string eventName)
{
var notifier = new NotificationCollection();
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer(options =>
{
options.AllowSynchronousIO = true;
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartupFilter>(new ModuleTestStartup(notifier, action));
services.AddRouting();
services.AddSystemWebAdapters()
.AddHttpApplication(options =>
{
options.RegisterModule<ModuleTestModule>();
});
})
.Configure(app =>
{
app.UseRouting();
app.UseAuthenticationEvents();
app.UseAuthorizationEvents();
app.UseSystemWebAdapters();
app.Run(ctx => Task.CompletedTask);
});
})
.StartAsync();
var url = $"/?action={action}¬ification={eventName}";
using var _ = await host.GetTestClient().GetAsync(new Uri(url, UriKind.Relative));
return notifier;
}
private sealed class NotificationCollection : List<string>
{
}
private sealed class ModuleTestStartup : IStartupFilter
{
private readonly NotificationCollection _collection;
private readonly string _action;
public ModuleTestStartup(NotificationCollection collection, string action)
{
_collection = collection;
_action = action;
}
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.Use(async (ctx, next) =>
{
ctx.Features.Set(_collection);
try
{
await next(ctx);
}
catch (InvalidOperationException) when (_action == ModuleTestModule.Throw)
{
}
});
next(builder);
};
}
private sealed class ModuleTestModule : EventsModule
{
protected override void InvokeEvent(HttpContext context, string name)
{
Add(context, name);
base.InvokeEvent(context, name);
}
private static void Add(HttpContextCore context, string name)
{
context.Features.GetRequired<NotificationCollection>().Add(name);
}
}
}