-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRedirect.cs
211 lines (175 loc) · 7.73 KB
/
TestRedirect.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
206
207
208
209
210
211
using ASP_Components.Middleware;
using ASP_Components.Models;
using ASP_Components.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Moq;
using System.Reflection;
using Xunit.Abstractions;
namespace ASP_Components.test
{
public class TestRedirect
{
private readonly ITestOutputHelper _output;
public TestRedirect(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public async Task TestRedirectConcurrency()
{
//Create Mock Service
var mockService = new Mock<IRedirectService>();
var mockData = new List<RedirectData>
{
new RedirectData { RedirectUrl = "/old", TargetUrl = "/new", UseRelative = false }
};
mockService.Setup(service => service.GetRedirectDataAsync())
.ReturnsAsync((IEnumerable<RedirectData>)mockData);
var middleware = new RedirectMiddleware(null, null, mockService.Object, null);
// Simulate 10 reading threads
var readingTasks = Enumerable.Range(0, 10).Select(_ => Task.Run(async () =>
{
for (int i = 0; i < 100; i++)
{
// Access _redirectData here (or any method that indirectly accesses it)
var data = middleware.GetType().GetField("_redirectData", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(middleware);
_output.WriteLine($"Thread {Task.CurrentId} - Reading data: {data}");
await Task.Delay(10); // Simulate some delay
}
})).ToList();
// Simulate updating thread
var updatingTask = Task.Run(async () =>
{
for (int i = 0; i < 10; i++)
{
_output.WriteLine($"Updating data...");
middleware.UpdateFromSourceAsync(); // Update _redirectData
await Task.Delay(50); // Simulate some delay
}
});
await Task.WhenAll(readingTasks.Concat(new[] { updatingTask }));
}
[Fact]
public async Task Ensure_RedirectFunctionality_WorksAsExpected()
{
// Setup RedirectService mock
var mockRedirectService = new Mock<IRedirectService>();
mockRedirectService.Setup(service => service.GetRedirectDataAsync()).ReturnsAsync(new List<RedirectData>
{
new RedirectData {
RedirectUrl = "/campaignA",
TargetUrl = "/campaigns/targetcampaign",
RedirectType= 302,
UseRelative = false },
new RedirectData {
RedirectUrl = "/campaignB",
TargetUrl = "/campaigns/targetcampaign/channelB",
RedirectType= 302,
UseRelative = false },
new RedirectData {
RedirectUrl = "/product-directory",
TargetUrl = "/products",
RedirectType= 301,
UseRelative = true }
});
// Setup ILogger<RedirectMiddleware> mock
var mockLogger = new Mock<ILogger<RedirectMiddleware>>();
// Setup IConfiguration mock
var mockConfig = new Mock<IConfiguration>();
mockConfig.Setup(config => config["RedirectCacheLifespan_Minutes"]).Returns("5");
// Instantiate the RedirectMiddleware
var middleware = new RedirectMiddleware(null, mockLogger.Object, mockRedirectService.Object, mockConfig.Object);
var tasks = new List<Task<bool>>();
tasks.Add(checkMiddleWare(middleware,
"/product-directory/bits/masonary/diamond-tip",
"/products/bits/masonary/diamond-tip",
StatusCodes.Status301MovedPermanently));
tasks.Add(checkMiddleWare(middleware,
"/campaignA",
"/campaigns/targetcampaign",
StatusCodes.Status302Found));
tasks.Add(checkMiddleWare(middleware,
"/campaignB",
"/campaigns/targetcampaign/channelB",
StatusCodes.Status302Found));
Task.WaitAll(tasks.ToArray());
}
private async Task<bool> checkMiddleWare(
RedirectMiddleware middleware,
string requestedPath,
string expectedResult,
int expectedStatusCode
)
{
// Setup mock HttpContext
var context = new DefaultHttpContext();
context.Request.Path = requestedPath;
// Invoke the middleware
await middleware.Invoke(context);
// Assert the redirect
Assert.Equal(expectedStatusCode, context.Response.StatusCode);
Assert.Equal(expectedResult, context.Response.Headers["Location"]);
_output.WriteLine($"Redirected {requestedPath} to {expectedResult} with code {context.Response.StatusCode}");
return true;
}
[Fact]
public async Task Ensure_Updating_Correctly()
{
List<Task> tasks = new List<Task>();
tasks.Add(Update_With_Delay("0.05", 3000));
tasks.Add(Update_With_Delay("0.1", 6000));
tasks.Add(Update_With_Delay("0.2", 12000));
tasks.Add(Update_With_Delay("0.3", 18000));
Task.WaitAll(tasks.ToArray());
}
private async Task<bool> Update_With_Delay(string delayString, int waitSeconds)
{
// Setup RedirectService mock
var mockRedirectService = new Mock<IRedirectService>();
mockRedirectService.Setup(service => service.GetRedirectDataAsync()).ReturnsAsync(new List<RedirectData>
{
new RedirectData {
RedirectUrl = "/campaignA",
TargetUrl = "/campaigns/targetcampaign",
RedirectType= 302,
UseRelative = false },
});
// Setup ILogger<RedirectMiddleware> mock
var mockLogger = new Mock<ILogger<RedirectMiddleware>>();
// Setup IConfiguration mock
var mockConfig = new Mock<IConfiguration>();
mockConfig.Setup(config => config["RedirectCacheLifespan_Minutes"]).Returns(delayString);
// Instantiate the RedirectMiddleware
var middleware = new RedirectMiddleware(null, mockLogger.Object, mockRedirectService.Object, mockConfig.Object);
//Check initial state
await checkMiddleWare(middleware,
"/campaignA",
"/campaigns/targetcampaign",
StatusCodes.Status302Found);
//updates the redirect data
mockRedirectService.Setup(service => service.GetRedirectDataAsync()).ReturnsAsync(new List<RedirectData>
{
new RedirectData {
RedirectUrl = "/campaignA",
TargetUrl = "/campaigns/targetcampaign2",
RedirectType= 302,
UseRelative = false },
});
//Check state again, should not have updated yet
await checkMiddleWare(middleware,
"/campaignA",
"/campaigns/targetcampaign",
StatusCodes.Status302Found);
//Wait for cache to expire
await Task.Delay(waitSeconds);
//Check state again, should have updated
await checkMiddleWare(middleware,
"/campaignA",
"/campaigns/targetcampaign2",
StatusCodes.Status302Found);
return true;
}
}
}