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
152 lines (123 loc) · 5.14 KB
/
Copy pathAsyncRestClientTests.cs
File metadata and controls
152 lines (123 loc) · 5.14 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
namespace ServiceStack.WebHost.Endpoints.Tests
{
public abstract class AsyncRestClientTests
{
private const string ListeningOn = "http://localhost:1337/";
ServiceStackHost appHost;
[OneTimeSetUp]
public void OnTestFixtureSetUp()
{
appHost = new ExampleAppHostHttpListener()
.Init()
.Start(ListeningOn);
}
[OneTimeTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
protected abstract IRestClientAsync CreateAsyncRestClient();
[Test]
public async Task Can_call_GetAsync_on_GetFactorial_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
var response = await asyncClient.GetAsync<GetFactorialResponse>("factorial/3");
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Result, Is.EqualTo(GetFactorialService.GetFactorial(3)));
}
[Test]
public async Task Can_call_GetAsync_on_Movies_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
var response = await asyncClient.GetAsync<MoviesResponse>("all-movies");
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movies.EquivalentTo(ResetMoviesService.Top5Movies));
}
[Test]
public async Task Can_call_GetAsync_on_single_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
var response = await asyncClient.GetAsync<MovieResponse>("all-movies/1");
Assert.That(response, Is.Not.Null, "No response received");
Assert.That(response.Movie.Id, Is.EqualTo(1));
}
[Test]
public async Task Can_call_PostAsync_to_add_new_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
var newMovie = new Support.Host.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 = await asyncClient.PostAsync<MovieResponse>("all-movies", 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 async Task Can_call_DeleteAsync_to_delete_Movie_using_RestClientAsync()
{
var asyncClient = CreateAsyncRestClient();
var newMovie = new Support.Host.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 = await asyncClient.PostAsync<MovieResponse>("all-movies", newMovie);
var createdMovie = response.Movie;
response = await asyncClient.DeleteAsync<MovieResponse>("all-movies/" + createdMovie.Id);
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 JsonServiceClient(ListeningOn);
}
}
[TestFixture]
public class JsonAsyncRestServiceHttpClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new JsonHttpClient(ListeningOn);
}
}
[TestFixture]
public class JsvAsyncRestServiceClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new JsvServiceClient(ListeningOn);
}
}
[TestFixture]
public class XmlAsyncRestServiceClientTests : AsyncRestClientTests
{
protected override IRestClientAsync CreateAsyncRestClient()
{
return new XmlServiceClient(ListeningOn);
}
}
}
}