forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncRestClientTests.cs
More file actions
266 lines (223 loc) · 9.48 KB
/
SyncRestClientTests.cs
File metadata and controls
266 lines (223 loc) · 9.48 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Net;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.Logging;
using ServiceStack.Logging.Support.Logging;
using ServiceStack.Service;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
namespace ServiceStack.WebHost.Endpoints.Tests
{
/// <summary>
/// These tests fail with Unauthorized exception when left last to run,
/// so prefixing with '_' to hoist its priority till we find out wtf is up
/// </summary>
public abstract class SyncRestClientTests : IDisposable
{
protected const string ListeningOn = "http://localhost:85/";
ExampleAppHostHttpListener appHost;
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
LogManager.LogFactory = new ConsoleLogFactory();
appHost = new ExampleAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
Dispose();
}
public void Dispose()
{
if (appHost == null) return;
appHost.Dispose();
appHost = null;
}
protected abstract IRestClient CreateRestClient();
//protected virtual IRestClient CreateRestClient()
//{
// return new XmlServiceClient(ListeningOn);
//}
static object[] GetFactorialRoutes = { "factorial/3", "fact/3" };
[TestCase("factorial/3")]
[TestCase("fact/3")]
public void Can_GET_GetFactorial_using_RestClient(string path)
{
var restClient = CreateRestClient();
var response = restClient.Get<GetFactorialResponse>(path);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Result, Is.EqualTo(GetFactorialService.GetFactorial(3)));
}
[TestCase("movies")]
[TestCase("custom-movies")]
public void Can_GET_Movies_using_RestClient(string path)
{
var restClient = CreateRestClient();
var response = restClient.Get<MoviesResponse>(path);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movies.EquivalentTo(ResetMoviesService.Top5Movies));
}
[TestCase("movies/1")]
[TestCase("custom-movies/1")]
public void Can_GET_single_Movie_using_RestClient(string path)
{
var restClient = CreateRestClient();
var response = restClient.Get<MovieResponse>(path);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movie.Id, Is.EqualTo(1));
}
[TestCase("movies")]
[TestCase("custom-movies")]
public void Can_POST_to_add_new_Movie_using_RestClient(string path)
{
var restClient = CreateRestClient();
var newMovie = new Movie {
ImdbId = "tt0450259",
Title = "Blood Diamond",
Rating = 8.0m,
Director = "Edward Zwick",
ReleaseDate = new DateTime(2007, 1, 26),
TagLine = "A fisherman, a smuggler, and a syndicate of businessmen match wits over the possession of a priceless diamond.",
Genres = new List<string> { "Adventure", "Drama", "Thriller" },
};
var response = restClient.Post<MovieResponse>(path, newMovie);
Assert.That(response, Is.Not.Null, "No response received");
var createdMovie = response.Movie;
Assert.That(createdMovie.Id, Is.GreaterThan(0));
Assert.That(createdMovie.ImdbId, Is.EqualTo(newMovie.ImdbId));
}
[Test]
public void Can_Deserialize_Xml_MovieResponse()
{
try
{
var xml = "<MovieResponse xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.servicestack.net/types\"><Movie><Director>Edward Zwick</Director><Genres xmlns:d3p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><d3p1:string>Adventure</d3p1:string><d3p1:string>Drama</d3p1:string><d3p1:string>Thriller</d3p1:string></Genres><Id>6</Id><ImdbId>tt0450259</ImdbId><Rating>8</Rating><ReleaseDate>2007-01-26T00:00:00+00:00</ReleaseDate><TagLine>A fisherman, a smuggler, and a syndicate of businessmen match wits over the possession of a priceless diamond.</TagLine><Title>Blood Diamond</Title></Movie></MovieResponse>";
var response = DataContractDeserializer.Instance.Parse<MovieResponse>(xml);
var toXml = DataContractSerializer.Instance.Parse(response);
Console.WriteLine("XML: " + toXml);
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
[TestCase("movies", "movies/")]
[TestCase("custom-movies", "custom-movies/")]
public void Can_DELETE_Movie_using_RestClient(string postPath, string deletePath)
{
var restClient = CreateRestClient();
var newMovie = new Movie {
ImdbId = "tt0450259",
Title = "Blood Diamond",
Rating = 8.0m,
Director = "Edward Zwick",
ReleaseDate = new DateTime(2007, 1, 26),
TagLine = "A fisherman, a smuggler, and a syndicate of businessmen match wits over the possession of a priceless diamond.",
Genres = new List<string> { "Adventure", "Drama", "Thriller" },
};
var response = restClient.Post<MovieResponse>(postPath, newMovie);
var createdMovie = response.Movie;
response = restClient.Delete<MovieResponse>(deletePath + createdMovie.Id);
Assert.That(createdMovie, Is.Not.Null);
Assert.That(response.Movie, Is.Null);
}
[Test]
public void Can_PUT_complex_type_with_custom_path()
{
var client = CreateRestClient();
var request = new InboxPostResponseRequest {
Id = 123,
Responses = new List<PageElementResponseDTO> {
new PageElementResponseDTO {
PageElementId = 123,
PageElementResponse = "something",
PageElementType = "Question"
}
}
};
var response = client.Put<InboxPostResponseRequestResponse>(
"inbox/123/responses",
request);
Assert.That(response.Id, Is.EqualTo(request.Id));
Assert.That(response.Responses[0].PageElementId,
Is.EqualTo(request.Responses[0].PageElementId));
}
[Test]
public void Does_throw_400_for_Argument_exceptions()
{
var client = CreateRestClient();
try
{
var response = client.Put<InboxPostResponseRequestResponse>(
"inbox/123/responses",
new InboxPostResponseRequest());
response.PrintDump();
Assert.Fail("Should throw");
}
catch (WebServiceException webEx)
{
Assert.That(webEx.StatusCode, Is.EqualTo(400));
}
}
[Test]
public void Does_throw_400_for_Argument_exceptions_without_response_DTOs()
{
var client = CreateRestClient();
try
{
var response = client.Put<InboxPost>(
"inbox/123/responses",
new InboxPost { Throw = true });
response.PrintDump();
Assert.Fail("Should throw");
}
catch (WebServiceException webEx)
{
Assert.That(webEx.StatusCode, Is.EqualTo(400));
}
}
}
[TestFixture]
public class JsonSyncRestClientTests : SyncRestClientTests
{
protected override IRestClient CreateRestClient()
{
return new JsonServiceClient(ListeningOn);
}
[Test]
public void Can_use_response_filters()
{
var isActioncalledGlobal = false;
var isActioncalledLocal = false;
ServiceClientBase.HttpWebResponseFilter = r => isActioncalledGlobal = true;
var restClient = (JsonServiceClient)CreateRestClient();
restClient.LocalHttpWebResponseFilter = r => isActioncalledLocal = true;
restClient.Get<MoviesResponse>("movies");
Assert.That(isActioncalledGlobal, Is.True);
Assert.That(isActioncalledLocal, Is.True);
}
}
[TestFixture]
public class JsvSyncRestClientTests : SyncRestClientTests
{
protected override IRestClient CreateRestClient()
{
return new JsvServiceClient(ListeningOn);
}
}
[TestFixture]
public class XmlSyncRestClientTests : SyncRestClientTests
{
protected override IRestClient CreateRestClient()
{
return new XmlServiceClient(ListeningOn);
}
}
}