forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegrationTestBase.cs
More file actions
112 lines (94 loc) · 3.47 KB
/
IntegrationTestBase.cs
File metadata and controls
112 lines (94 loc) · 3.47 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
using System;
using Funq;
using ServiceStack.Configuration;
using ServiceStack.Logging;
using ServiceStack.Logging.Support.Logging;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ServiceStack.ServiceClient.Web;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
namespace ServiceStack.WebHost.Endpoints.Tests.IntegrationTests
{
public class IntegrationTestBase
: AppHostHttpListenerBase
{
protected const string BaseUrl = "http://localhost:82/";
//Fiddler can debug local HTTP requests when using the hostname
//private const string BaseUrl = "http://io:8081/";
//private static ILog log;
public IntegrationTestBase()
: base("ServiceStack Examples", typeof(RestMovieService).Assembly)
{
LogManager.LogFactory = new DebugLogFactory();
//log = LogManager.GetLogger(GetType());
Instance = null;
Init();
try
{
Start(BaseUrl);
}
catch (Exception ex)
{
Console.WriteLine("Error trying to run ConsoleHost: " + ex.Message);
}
}
public override void Configure(Container container)
{
container.Register<IResourceManager>(new ConfigurationResourceManager());
container.Register(c => new ExampleConfig(c.Resolve<IResourceManager>()));
//var appConfig = container.Resolve<ExampleConfig>();
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(
":memory:", //Use an in-memory database instead
false, //keep the same in-memory db connection open
SqliteOrmLiteDialectProvider.Instance));
Routes.Add<Movies>("/custom-movies", "GET")
.Add<Movies>("/custom-movies/genres/{Genre}")
.Add<Movie>("/custom-movies", "POST,PUT")
.Add<Movie>("/custom-movies/{Id}");
ConfigureDatabase.Init(container.Resolve<IDbConnectionFactory>());
}
public void SendToEachEndpoint<TRes>(object request, Action<TRes> validate)
{
SendToEachEndpoint(request, null, validate);
}
/// <summary>
/// Run the request against each Endpoint
/// </summary>
/// <typeparam name="TRes"></typeparam>
/// <param name="request"></param>
/// <param name="validate"></param>
/// <param name="httpMethod"></param>
public void SendToEachEndpoint<TRes>(object request, string httpMethod, Action<TRes> validate)
{
using (var xmlClient = new XmlServiceClient(BaseUrl))
using (var jsonClient = new JsonServiceClient(BaseUrl))
using (var jsvClient = new JsvServiceClient(BaseUrl))
{
xmlClient.HttpMethod = httpMethod;
jsonClient.HttpMethod = httpMethod;
jsvClient.HttpMethod = httpMethod;
var xmlResponse = xmlClient.Send<TRes>(request);
if (validate != null) validate(xmlResponse);
var jsonResponse = jsonClient.Send<TRes>(request);
if (validate != null) validate(jsonResponse);
var jsvResponse = jsvClient.Send<TRes>(request);
if (validate != null) validate(jsvResponse);
}
}
public void DeleteOnEachEndpoint<TRes>(string relativePathOrAbsoluteUri, Action<TRes> validate)
{
using (var xmlClient = new XmlServiceClient(BaseUrl))
using (var jsonClient = new JsonServiceClient(BaseUrl))
using (var jsvClient = new JsvServiceClient(BaseUrl))
{
var xmlResponse = xmlClient.Delete<TRes>(relativePathOrAbsoluteUri);
if (validate != null) validate(xmlResponse);
var jsonResponse = jsonClient.Delete<TRes>(relativePathOrAbsoluteUri);
if (validate != null) validate(jsonResponse);
var jsvResponse = jsvClient.Delete<TRes>(relativePathOrAbsoluteUri);
if (validate != null) validate(jsvResponse);
}
}
}
}