forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncRestClientTests.cs
More file actions
174 lines (141 loc) · 4.89 KB
/
AsyncRestClientTests.cs
File metadata and controls
174 lines (141 loc) · 4.89 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
using System;
using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.Logging;
using ServiceStack.Logging.Support.Logging;
using ServiceStack.Service;
using ServiceStack.ServiceClient.Web;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
namespace ServiceStack.WebHost.Endpoints.Tests
{
public abstract class AsyncRestClientTests
{
private const string ListeningOn = "http://localhost:82/";
ExampleAppHostHttpListener appHost;
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
LogManager.LogFactory = new ConsoleLogFactory();
appHost = new ExampleAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
protected abstract IRestClientAsync CreateAsyncRestClient();
private static void FailOnAsyncError<T>(T response, Exception ex)
{
Assert.Fail(ex.Message);
}
[Test]
public void Can_call_GetAsync_on_GetFactorial_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
GetFactorialResponse response = null;
asyncClient.GetAsync<GetFactorialResponse>("factorial/3", r => response = r, FailOnAsyncError);
Thread.Sleep(1000);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Result, Is.EqualTo(GetFactorialService.GetFactorial(3)));
}
[Test]
public void Can_call_GetAsync_on_Movies_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
MoviesResponse response = null;
asyncClient.GetAsync<MoviesResponse>("movies", r => response = r, FailOnAsyncError);
Thread.Sleep(1000);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movies.EquivalentTo(ResetMoviesService.Top5Movies));
}
[Test]
public void Can_call_GetAsync_on_single_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
MovieResponse response = null;
asyncClient.GetAsync<MovieResponse>("movies/1", r => response = r, FailOnAsyncError);
Thread.Sleep(1000);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movie.Id, Is.EqualTo(1));
}
[Test]
public void Can_call_PostAsync_to_add_new_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
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" },
};
MovieResponse response = null;
asyncClient.PostAsync<MovieResponse>("movies", newMovie,
r => response = r, FailOnAsyncError);
Thread.Sleep(1000);
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_call_DeleteAsync_to_delete_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
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" },
};
MovieResponse response = null;
Movie createdMovie = null;
asyncClient.PostAsync<MovieResponse>("movies", newMovie,
r =>
{
createdMovie = r.Movie;
asyncClient.DeleteAsync<MovieResponse>("movies/" + createdMovie.Id,
dr => response = dr, FailOnAsyncError);
}, FailOnAsyncError);
Thread.Sleep(1000);
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(createdMovie, Is.Not.Null);
Assert.That(response.Movie, Is.Null);
}
[TestFixture]
public class JsonAsyncRestServiceClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new JsonRestClientAsync(ListeningOn);
}
}
[TestFixture]
public class JsvAsyncRestServiceClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new JsvRestClientAsync(ListeningOn);
}
}
[TestFixture]
public class XmlAsyncRestServiceClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new XmlRestClientAsync(ListeningOn);
}
}
}
}