-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUnitTestHistory.cs
More file actions
210 lines (161 loc) · 7.8 KB
/
UnitTestHistory.cs
File metadata and controls
210 lines (161 loc) · 7.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
using InertiaCore;
using InertiaCore.Models;
using InertiaCore.Ssr;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Moq;
namespace InertiaCoreTests;
public partial class Tests
{
[Test]
[Description("Test if history encryption is sent correctly.")]
public async Task TestHistoryEncryptionResult()
{
_factory.EncryptHistory();
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var headers = new HeaderDictionary
{
{ "X-Inertia", "true" }
};
var context = PrepareContext(headers);
response.SetContext(context);
await response.ProcessResponse();
var result = response.GetResult();
Assert.Multiple(() =>
{
Assert.That(result, Is.InstanceOf<JsonResult>());
var json = (result as JsonResult)?.Value;
Assert.That(json, Is.InstanceOf<Page>());
Assert.That((json as Page)?.ClearHistory, Is.EqualTo(false));
Assert.That((json as Page)?.EncryptHistory, Is.EqualTo(true));
Assert.That((json as Page)?.Component, Is.EqualTo("Test/Page"));
Assert.That((json as Page)?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));
});
}
[Test]
[Description("Test if clear history is sent correctly.")]
public async Task TestClearHistoryResult()
{
// Set up session mock
var sessionData = new Dictionary<string, byte[]>();
var sessionMock = new Mock<ISession>();
sessionMock.Setup(s => s.Set(It.IsAny<string>(), It.IsAny<byte[]>()))
.Callback<string, byte[]>((key, value) => sessionData[key] = value);
sessionMock.Setup(s => s.TryGetValue(It.IsAny<string>(), out It.Ref<byte[]?>.IsAny))
.Returns((string key, out byte[]? value) => sessionData.TryGetValue(key, out value));
sessionMock.Setup(s => s.Remove(It.IsAny<string>()))
.Callback<string>(key => sessionData.Remove(key));
// Set up HttpContext with session support
var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupGet(c => c.Session).Returns(sessionMock.Object);
var contextAccessorMock = new Mock<IHttpContextAccessor>();
contextAccessorMock.SetupGet(a => a.HttpContext).Returns(httpContextMock.Object);
// Create factory with session support
var gateway = new Mock<IGateway>();
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions());
var factoryWithSession = new ResponseFactory(contextAccessorMock.Object, gateway.Object, options.Object);
factoryWithSession.ClearHistory();
var response = factoryWithSession.Render("Test/Page", new
{
Test = "Test"
});
var headers = new HeaderDictionary
{
{ "X-Inertia", "true" }
};
var context = PrepareContextWithSession(headers, sessionMock.Object);
response.SetContext(context);
await response.ProcessResponse();
var result = response.GetResult();
Assert.Multiple(() =>
{
Assert.That(result, Is.InstanceOf<JsonResult>());
var json = (result as JsonResult)?.Value;
Assert.That(json, Is.InstanceOf<Page>());
Assert.That((json as Page)?.ClearHistory, Is.EqualTo(true));
Assert.That((json as Page)?.EncryptHistory, Is.EqualTo(false));
Assert.That((json as Page)?.Component, Is.EqualTo("Test/Page"));
Assert.That((json as Page)?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));
});
// Verify session value was removed after being read (one-time use behavior)
sessionMock.Verify(s => s.Remove("inertia.clear_history"), Times.Once);
}
[Test]
[Description("Test if clear history persists when redirecting.")]
public async Task TestClearHistoryWithRedirect()
{
// Arrange: Set up session mock to simulate session storage behavior
var sessionData = new Dictionary<string, byte[]>();
var sessionMock = new Mock<ISession>();
sessionMock.Setup(s => s.Set(It.IsAny<string>(), It.IsAny<byte[]>()))
.Callback<string, byte[]>((key, value) => sessionData[key] = value);
sessionMock.Setup(s => s.TryGetValue(It.IsAny<string>(), out It.Ref<byte[]?>.IsAny))
.Returns((string key, out byte[]? value) => sessionData.TryGetValue(key, out value));
sessionMock.Setup(s => s.Remove(It.IsAny<string>()))
.Callback<string>(key => sessionData.Remove(key));
// Set up HttpContext with session support
var httpContextMock = new Mock<HttpContext>();
httpContextMock.SetupGet(c => c.Session).Returns(sessionMock.Object);
var contextAccessorMock = new Mock<IHttpContextAccessor>();
contextAccessorMock.SetupGet(a => a.HttpContext).Returns(httpContextMock.Object);
// Create factory with session support
var gateway = new Mock<IGateway>();
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions());
var factoryWithSession = new ResponseFactory(contextAccessorMock.Object, gateway.Object, options.Object);
// Simulate first request: set clearHistory and redirect
factoryWithSession.ClearHistory();
// Simulate second request after redirect: create new response
var response = factoryWithSession.Render("User/Edit", new { });
var headers = new HeaderDictionary
{
{ "X-Inertia", "true" }
};
var context = PrepareContextWithSession(headers, sessionMock.Object);
response.SetContext(context);
await response.ProcessResponse();
var result = response.GetResult();
// Assert: clearHistory should persist through redirect
Assert.Multiple(() =>
{
Assert.That(result, Is.InstanceOf<JsonResult>());
var json = (result as JsonResult)?.Value;
Assert.That(json, Is.InstanceOf<Page>());
Assert.That((json as Page)?.ClearHistory, Is.EqualTo(true));
Assert.That((json as Page)?.EncryptHistory, Is.EqualTo(false));
Assert.That((json as Page)?.Component, Is.EqualTo("User/Edit"));
});
// Verify session value was removed after being read (one-time use behavior)
sessionMock.Verify(s => s.Remove("inertia.clear_history"), Times.Once);
}
/// <summary>
/// Prepares ActionContext with session support for testing redirect scenarios.
/// </summary>
private static ActionContext PrepareContextWithSession(HeaderDictionary? headers, ISession session)
{
var request = new Mock<HttpRequest>();
request.SetupGet(r => r.Headers).Returns(headers ?? new HeaderDictionary());
var response = new Mock<HttpResponse>();
response.SetupGet(r => r.Headers).Returns(new HeaderDictionary());
var features = new Microsoft.AspNetCore.Http.Features.FeatureCollection();
var httpContext = new Mock<HttpContext>();
httpContext.SetupGet(c => c.Request).Returns(request.Object);
httpContext.SetupGet(c => c.Response).Returns(response.Object);
httpContext.SetupGet(c => c.Features).Returns(features);
httpContext.SetupGet(c => c.Session).Returns(session);
return new ActionContext(httpContext.Object, new Microsoft.AspNetCore.Routing.RouteData(), new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor());
}
}