forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceControllerTests.cs
More file actions
172 lines (142 loc) · 6.6 KB
/
ServiceControllerTests.cs
File metadata and controls
172 lines (142 loc) · 6.6 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
using System;
using Funq;
using NUnit.Framework;
using ServiceStack.Host;
using ServiceStack.ServiceHost.Tests.Support;
using ServiceStack.Testing;
namespace ServiceStack.ServiceHost.Tests
{
[TestFixture]
public class ServiceControllerTests
{
[Test]
public void Can_register_all_services_in_an_assembly()
{
using (var appHost = new BasicAppHost(typeof(BasicService).GetAssembly()).Init())
{
var container = appHost.Container;
var serviceController = appHost.ServiceController;
container.Register<IFoo>(c => new Foo());
container.Register<IBar>(c => new Bar());
var request = new AutoWire();
var response = serviceController.Execute(request) as AutoWireResponse;
Assert.That(response, Is.Not.Null);
}
}
[Test]
public void Can_override_service_creation_with_custom_implementation()
{
using (var appHost = new BasicAppHost(typeof(BasicService).GetAssembly()).Init())
{
var container = appHost.Container;
var serviceController = appHost.ServiceController;
container.Register<IFoo>(c => new Foo());
container.Register<IBar>(c => new Bar());
var request = new AutoWire();
var response = serviceController.Execute(request) as AutoWireResponse;
Assert.That(response, Is.Not.Null);
Assert.That(response.Foo as Foo, Is.Not.Null);
Assert.That(response.Bar as Bar, Is.Not.Null);
container.Register(c => new AutoWireService(new Foo2())
{
Bar = new Bar2()
});
response = serviceController.Execute(request) as AutoWireResponse;
Assert.That(response, Is.Not.Null);
Assert.That(response.Foo as Foo2, Is.Not.Null);
Assert.That(response.Bar as Bar2, Is.Not.Null);
}
}
[Test]
public void Can_inject_RequestContext_for_IRequiresRequestContext_services()
{
using (var appHost = new BasicAppHost(typeof(RequiresService).GetAssembly()).Init())
{
var serviceController = appHost.ServiceController;
var request = new RequiresContext();
var response = serviceController.Execute(request, new BasicRequest(request))
as RequiresContextResponse;
Assert.That(response, Is.Not.Null);
}
}
[Test]
public void Generic_Service_should_not_get_registered_with_generic_parameter()
{
using (var appHost = new BasicAppHost(typeof(GenericService<>).GetAssembly()).Init())
{
// We should definately *not* be able to call the generic service with a "T" request object :)
var requestType = typeof(GenericService<>).GetGenericArguments()[0];
var exception = Assert.Throws<NotImplementedException>(() => appHost.ServiceController.GetService(requestType));
Assert.That(exception.Message, Does.Contain("Unable to resolve service"));
}
}
[Test]
public void Generic_service_with_recursive_ceneric_type_should_not_get_registered()
{
using (var appHost = new BasicAppHost
{
UseServiceController = x =>
new ServiceController(x, () => new[] {
typeof(GenericService<>).MakeGenericType(new[] { typeof(Generic3<>) })
})
}.Init())
{
// Tell manager to register GenericService<Generic3<>>, which should not be possible since Generic3<> is an open type
var exception = Assert.Throws<System.NotImplementedException>(() =>
appHost.ServiceController.GetService(typeof(Generic3<>)));
Assert.That(exception.Message, Does.Contain("Unable to resolve service"));
}
}
[Test]
public void Generic_service_can_be_registered_with_closed_types()
{
using (var appHost = new BasicAppHost
{
UseServiceController = x => new ServiceController(x, () => new[]
{
typeof (GenericService<Generic1>),
typeof (GenericService<>).MakeGenericType(new[] {typeof (Generic2)}),
// GenericService<Generic2> created through reflection
typeof (GenericService<Generic3<string>>),
typeof (GenericService<Generic3<int>>),
typeof (GenericService<>).MakeGenericType(new[]
{typeof (Generic3<>).MakeGenericType(new[] {typeof (double)})}),
// GenericService<Generic3<double>> created through reflection
})
}.Init())
{
var serviceController = appHost.ServiceController;
Assert.AreEqual(typeof(Generic1).FullName, ((Generic1Response)serviceController.Execute(new Generic1())).Data);
Assert.AreEqual(typeof(Generic2).FullName, ((Generic1Response)serviceController.Execute(new Generic2())).Data);
Assert.AreEqual(typeof(Generic3<string>).FullName, ((Generic1Response)serviceController.Execute(new Generic3<string>())).Data);
Assert.AreEqual(typeof(Generic3<int>).FullName, ((Generic1Response)serviceController.Execute(new Generic3<int>())).Data);
Assert.AreEqual(typeof(Generic3<double>).FullName, ((Generic1Response)serviceController.Execute(new Generic3<double>())).Data);
}
}
[Test]
public void Service_with_generic_IGet_marker_interface_can_be_registered_without_DefaultRequestAttribute()
{
var appHost = new AppHost();
Assert.That(appHost.RestPaths.Count, Is.EqualTo(0));
appHost.RegisterService<GetMarkerService>("/route");
Assert.That(appHost.RestPaths.Count, Is.EqualTo(1));
}
}
public class GetRequest { }
public class GetRequestResponse { }
[DefaultRequest(typeof(GetRequest))]
public class GetMarkerService : Service
{
public object Get(GetRequest request)
{
return new GetRequestResponse();
}
}
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("Test", typeof(AppHost).GetAssembly()) { }
public override void Configure(Container container)
{
}
}
}