-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUnitTestUrlResolver.cs
More file actions
147 lines (114 loc) · 4.19 KB
/
UnitTestUrlResolver.cs
File metadata and controls
147 lines (114 loc) · 4.19 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
using InertiaCore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
namespace InertiaCoreTests;
public partial class Tests
{
[Test]
[Description("Test if custom URL resolver is used when provided.")]
public async Task TestCustomUrlResolver()
{
// Set up a custom URL resolver
_factory.ResolveUrlUsing(context => "/custom/url");
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var context = PrepareContext();
response.SetContext(context);
await response.ProcessResponse();
var page = response.GetJson().Value as Page;
Assert.That(page?.Url, Is.EqualTo("/custom/url"));
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));
}
[Test]
[Description("Test if default URL resolver is used when no custom resolver is provided.")]
public async Task TestDefaultUrlResolver()
{
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var context = PrepareContext();
response.SetContext(context);
await response.ProcessResponse();
var page = response.GetJson().Value as Page;
// Should use the default RequestedUri() method
Assert.That(page?.Url, Is.Not.Null);
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));
}
[Test]
[Description("Test if custom URL resolver receives correct ActionContext.")]
public async Task TestUrlResolverReceivesContext()
{
ActionContext? receivedContext = null;
// Set up a custom URL resolver that captures the context
_factory.ResolveUrlUsing(context =>
{
receivedContext = context;
return "/captured/context/url";
});
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var context = PrepareContext();
response.SetContext(context);
await response.ProcessResponse();
var page = response.GetJson().Value as Page;
Assert.That(page?.Url, Is.EqualTo("/captured/context/url"));
Assert.That(receivedContext, Is.Not.Null);
Assert.That(receivedContext, Is.EqualTo(context));
}
[Test]
[Description("Test if custom URL resolver can access request information.")]
public async Task TestUrlResolverAccessesRequest()
{
// Set up a custom URL resolver that uses request path
_factory.ResolveUrlUsing(context =>
{
var path = context.HttpContext.Request.Path;
return $"/custom{path}";
});
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var context = PrepareContext();
response.SetContext(context);
await response.ProcessResponse();
var page = response.GetJson().Value as Page;
Assert.That(page?.Url, Is.Not.Null);
Assert.That(page?.Url, Does.StartWith("/custom"));
}
[Test]
[Description("Test if URL resolver can be changed between requests.")]
public async Task TestUrlResolverCanBeChanged()
{
// First resolver
_factory.ResolveUrlUsing(context => "/first/url");
var response1 = _factory.Render("Test/Page", new { Test = "Test1" });
var context1 = PrepareContext();
response1.SetContext(context1);
await response1.ProcessResponse();
var page1 = response1.GetJson().Value as Page;
Assert.That(page1?.Url, Is.EqualTo("/first/url"));
// Change resolver
_factory.ResolveUrlUsing(context => "/second/url");
var response2 = _factory.Render("Test/Page", new { Test = "Test2" });
var context2 = PrepareContext();
response2.SetContext(context2);
await response2.ProcessResponse();
var page2 = response2.GetJson().Value as Page;
Assert.That(page2?.Url, Is.EqualTo("/second/url"));
}
}