Repository navigation
Expand file tree
/
Copy pathQuick.Core.Mvc.Tests.pas
More file actions
230 lines (202 loc) · 5.32 KB
/
Copy pathQuick.Core.Mvc.Tests.pas
File metadata and controls
230 lines (202 loc) · 5.32 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
unit Quick.Core.Mvc.Tests;
{ Tests: T-MVC-01..09
T-MVC-01 TStatusResult carries StatusCode and StatusText from constructor
T-MVC-02 TContentResult carries Content and ContentType (two-arg overload)
T-MVC-03 TContentResult (one-arg) sets Content; ContentType defaults to empty
T-MVC-04 TRedirectResult carries StatusCode and URL from constructor
T-MVC-05 TProblemDetailsResult carries Status, Title, Detail, Type, Instance
T-MVC-06 TJsonResult serializes a published-property object to non-empty JSON
T-MVC-07 ActionName attribute carries its value
T-MVC-08 HTTP verb attributes (HttpGet/HttpPost/HttpPut/HttpDelete) carry Route
T-MVC-09 THttpRoute URL/ActionName/ControllerName properties are writable
}
interface
uses
DUnitX.TestFramework,
Quick.Core.Mvc.ActionResult,
Quick.Core.Mvc.Routing,
Quick.Core.Mvc.Controller;
type
{ Simple DTO used by T-MVC-06 }
{$M+}
TMvcTestDto = class
private
fId: Integer;
fName: string;
published
property Id: Integer read fId write fId;
property Name: string read fName write fName;
end;
{$M-}
[TestFixture]
TMvcTests = class
public
[Test]
procedure T_MVC_01_StatusResult_Properties;
[Test]
procedure T_MVC_02_ContentResult_TwoArg;
[Test]
procedure T_MVC_03_ContentResult_OneArg_EmptyContentType;
[Test]
procedure T_MVC_04_RedirectResult_Properties;
[Test]
procedure T_MVC_05_ProblemDetailsResult_Properties;
[Test]
procedure T_MVC_06_JsonResult_SerializesObject;
[Test]
procedure T_MVC_07_ActionNameAttribute_CarriesValue;
[Test]
procedure T_MVC_08_VerbAttributes_CarryRoute;
[Test]
procedure T_MVC_09_HttpRoute_PropertyRoundTrip;
end;
implementation
uses
System.SysUtils,
Quick.HttpServer.Types;
{ T-MVC-01 }
procedure TMvcTests.T_MVC_01_StatusResult_Properties;
var
r: TStatusResult;
begin
r := TStatusResult.Create(404, 'Not Found');
try
Assert.AreEqual(404, r.StatusCode);
Assert.AreEqual('Not Found', r.StatusText);
finally
r.Free;
end;
end;
{ T-MVC-02 }
procedure TMvcTests.T_MVC_02_ContentResult_TwoArg;
var
r: TContentResult;
begin
r := TContentResult.Create('hello world', 'text/plain');
try
Assert.AreEqual('hello world', r.Content);
Assert.AreEqual('text/plain', r.ContentType);
finally
r.Free;
end;
end;
{ T-MVC-03 }
procedure TMvcTests.T_MVC_03_ContentResult_OneArg_EmptyContentType;
var
r: TContentResult;
begin
r := TContentResult.Create('body text');
try
Assert.AreEqual('body text', r.Content);
Assert.AreEqual('', r.ContentType);
finally
r.Free;
end;
end;
{ T-MVC-04 }
procedure TMvcTests.T_MVC_04_RedirectResult_Properties;
var
r: TRedirectResult;
begin
r := TRedirectResult.Create(302, 'https://example.com/new');
try
Assert.AreEqual(302, r.StatusCode);
Assert.AreEqual('https://example.com/new', r.URL);
finally
r.Free;
end;
end;
{ T-MVC-05 }
procedure TMvcTests.T_MVC_05_ProblemDetailsResult_Properties;
var
r: TProblemDetailsResult;
begin
r := TProblemDetailsResult.Create(422, 'Unprocessable Entity',
'Validation failed', 'https://tools.ietf.org/html/rfc4918#section-11.2', '/orders/1');
try
Assert.AreEqual(422, r.Status);
Assert.AreEqual('Unprocessable Entity', r.Title);
Assert.AreEqual('Validation failed', r.Detail);
Assert.AreEqual('https://tools.ietf.org/html/rfc4918#section-11.2', r.&Type);
Assert.AreEqual('/orders/1', r.Instance);
finally
r.Free;
end;
end;
{ T-MVC-06 }
procedure TMvcTests.T_MVC_06_JsonResult_SerializesObject;
var
dto: TMvcTestDto;
r: TJsonResult;
begin
dto := TMvcTestDto.Create;
dto.Id := 42;
dto.Name := 'widget';
r := TJsonResult.Create(dto, True);
try
Assert.IsTrue(r.JsonText <> '', 'JSON output must not be empty');
Assert.IsTrue(r.JsonText.Contains('42'), 'JSON must contain Id value');
Assert.IsTrue(r.JsonText.Contains('widget'), 'JSON must contain Name value');
finally
r.Free;
dto.Free;
end;
end;
{ T-MVC-07 }
procedure TMvcTests.T_MVC_07_ActionNameAttribute_CarriesValue;
var
attr: ActionName;
begin
attr := ActionName.Create('MyAction');
try
Assert.AreEqual('MyAction', attr.Name);
finally
attr.Free;
end;
end;
{ T-MVC-08 }
procedure TMvcTests.T_MVC_08_VerbAttributes_CarryRoute;
var
g: HttpGet;
p: HttpPost;
u: HttpPut;
d: HttpDelete;
begin
g := HttpGet.Create('/api/items');
p := HttpPost.Create('/api/items');
u := HttpPut.Create('/api/items/{id}');
d := HttpDelete.Create('/api/items/{id}');
try
Assert.AreEqual('/api/items', g.Route);
Assert.AreEqual('/api/items', p.Route);
Assert.AreEqual('/api/items/{id}', u.Route);
Assert.AreEqual('/api/items/{id}', d.Route);
finally
g.Free;
p.Free;
u.Free;
d.Free;
end;
end;
{ T-MVC-09 }
procedure TMvcTests.T_MVC_09_HttpRoute_PropertyRoundTrip;
var
route: THttpRoute;
begin
route := THttpRoute.Create;
try
route.URL := '/api/products';
route.ActionName := 'List';
route.ControllerName := 'Products';
route.Authorize := True;
Assert.AreEqual('/api/products', route.URL);
Assert.AreEqual('List', route.ActionName);
Assert.AreEqual('Products', route.ControllerName);
Assert.IsTrue(route.Authorize);
finally
route.Free;
end;
end;
initialization
TDUnitX.RegisterTestFixture(TMvcTests);
end.