forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteInferenceTests.cs
More file actions
199 lines (156 loc) · 6.17 KB
/
Copy pathRouteInferenceTests.cs
File metadata and controls
199 lines (156 loc) · 6.17 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
using System.Linq;
using NUnit.Framework;
using ServiceStack.DataAnnotations;
using ServiceStack.Host;
using ServiceStack.Testing;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Tests.Support.Services;
using ServiceStack.WebHost.Endpoints.Tests.Support.Types;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[TestFixture]
public class RouteInferenceTests
{
ServiceStackHost appHost;
[TestFixtureSetUp]
public void InferRoutes()
{
appHost = new BasicAppHost().Init();
RouteNamingConvention.PropertyNamesToMatch.Add("Key");
RouteNamingConvention.AttributeNamesToMatch.Add(typeof(KeyAttribute).Name);
appHost.Routes.AddFromAssembly(typeof(RouteInferenceTests).Assembly);
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void Should_infer_route_from_RequestDTO_type()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestNoMembers)
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 1);
Assert.That(restPath.AllowedVerbs.Contains("GET"));
Assert.That(restPath.AllowedVerbs.Contains("POST"));
Assert.That(restPath.AllowedVerbs.Contains("PUT"));
Assert.That(restPath.AllowedVerbs.Contains("DELETE"));
Assert.That(restPath.AllowedVerbs.Contains("PATCH"));
Assert.That(restPath.AllowedVerbs.Contains("HEAD"));
Assert.That(restPath.AllowedVerbs.Contains("OPTIONS"));
Assert.IsTrue(typeof(RequestNoMembers).Name.EqualsIgnoreCase(restPath.Path.Remove(0,1)));
}
[Test]
public void Should_infer_route_from_AnyPublicProperty_named_Id()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithMemberCalledId)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 2);
Assert.IsTrue(restPath.Path.EndsWithInvariant("{Id}"));
}
[Test]
public void Should_infer_route_from_AnyPublicProperty_named_Ids()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithMemberCalledIds)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 2);
Assert.IsTrue(restPath.Path.EndsWithInvariant("{Ids}"));
}
[Test]
public void Should_infer_route_from_AnyPublicProperty_in_MatchingNameStrategy()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithMemberCalledSpecialName)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 2);
Assert.IsTrue(restPath.Path.EndsWithInvariant("{Key}"));
}
[Test]
public void Should_infer_route_from_AnyPublicProperty_with_PrimaryKeyAttribute()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithPrimaryKeyAttribute)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 2);
//it doesn't matter what the placeholder name is; only that 1 placeholer is in the path
Assert.IsTrue(restPath.Path.Count(c => c == '}') == 1);
}
[Test]
public void Should_infer_route_from_AnyPublicProperty_in_MatchingAttributeStrategy()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithMemberWithKeyAttribute)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 2);
//it doesn't matter what the placeholder name is; only that 1 placeholer is in the path
Assert.IsTrue(restPath.Path.Count(c => c == '}') == 1);
}
[Test]
public void Should_infer_route_from_AnyPubicProperty_FromAnyStrategy_AndCompositeTheRoute()
{
var restPath = (from r in appHost.RestPaths
where r.RequestType == typeof(RequestWithCompositeKeys)
//routes without {placeholders} are tested above
&& r.PathComponentsCount > 1
select r).FirstOrDefault();
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.PathComponentsCount == 3);
//it doesn't matter what the placeholder name is; only that 1 placeholer is in the path
Assert.IsTrue(restPath.Path.Count(c => c == '}') == 2);
}
}
public class RequestNoMembers { }
public class RequestWithMemberCalledId
{
public object Id { get; set; }
}
public class RequestWithMemberCalledIds
{
public object[] Ids { get; set; }
}
public class RequestWithMemberCalledSpecialName
{
public string Key { get; set; }
}
public class RequestWithPrimaryKeyAttribute
{
[PrimaryKey]
public int PrimaryKeyAttributeProperty { get; set; }
}
public class RequestWithMemberWithKeyAttribute
{
[Key]
public string KeyAttributeProperty { get; set; }
}
public class RequestWithCompositeKeys
{
public int Id { get; set; }
public int Key { get; set; }
}
public class RequestNoMembersService : TestRestService<RequestNoMembers> { }
public class RequestWithMemberCalledIdService : TestRestService<RequestWithMemberCalledId> { }
public class RequestWithMemberCalledIdsService : TestRestService<RequestWithMemberCalledIds> { }
public class RequestWithMemberCalledSpecialNameService : TestRestService<RequestWithMemberCalledSpecialName> { }
public class RequestWithPrimaryKeyAttributeService : TestRestService<RequestWithPrimaryKeyAttribute> { }
public class RequestWithMemberWithKeyAttributeService : TestRestService<RequestWithMemberWithKeyAttribute> { }
public class RequestWithCompositeKeysService : TestRestService<RequestWithCompositeKeys> { }
}