forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsyncTests.cs
301 lines (237 loc) · 10.1 KB
/
AsyncTests.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using NUnit.Framework;
using RestSharp.IntegrationTests.Helpers;
using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class AsyncTests
{
[Test]
public void Can_Perform_GET_Async()
{
Uri baseUrl = new Uri("http://localhost:8888/");
const string val = "Basic async test";
ManualResetEvent resetEvent = new ManualResetEvent(false);
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.EchoValue(val)))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
client.ExecuteAsync(request, (response, asyncHandle) =>
{
Assert.NotNull(response.Content);
Assert.AreEqual(val, response.Content);
resetEvent.Set();
});
resetEvent.WaitOne();
}
}
[Test]
public void Can_Perform_GET_Async_Without_Async_Handle()
{
Uri baseUrl = new Uri("http://localhost:8888/");
const string val = "Basic async test";
ManualResetEvent resetEvent = new ManualResetEvent(false);
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.EchoValue(val)))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
client.ExecuteAsync(request, response =>
{
Assert.NotNull(response.Content);
Assert.AreEqual(val, response.Content);
resetEvent.Set();
});
resetEvent.WaitOne();
}
}
[Test]
public void Can_Perform_GET_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
const string val = "Basic async task test";
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("");
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
Assert.NotNull(task.Result.Content);
Assert.AreEqual(val, task.Result.Content);
}
}
[Test]
public void Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler()
{
const string baseUrl = "http://localhost:8888/";
const string exceptionMessage = "Thrown from OnBeforeDeserialization";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("success");
request.OnBeforeDeserialization += r => { throw new Exception(exceptionMessage); };
Task<IRestResponse<Response>> task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
IRestResponse<Response> response = task.Result;
Assert.AreEqual(exceptionMessage, response.ErrorMessage);
Assert.AreEqual(ResponseStatus.Error, response.ResponseStatus);
}
}
[Test]
public void Can_Perform_ExecuteGetTaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("success");
Task<IRestResponse<Response>> task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
Assert.AreEqual("Works!", task.Result.Data.Message);
}
}
[Test]
public void Can_Perform_GetTaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("success");
Task<Response> task = client.GetTaskAsync<Response>(request);
task.Wait();
Assert.AreEqual("Works!", task.Result.Message);
}
}
#if !APPVEYOR
[Test]
public void Can_Cancel_GET_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
const string val = "Basic async task test";
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("timeout");
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
Task<IRestResponse> task = client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
cancellationTokenSource.Cancel();
Assert.True(task.IsCanceled);
}
}
#endif
[Test]
public void Can_Cancel_GET_TaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8888/";
const string val = "Basic async task test";
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("timeout");
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
Task<IRestResponse<Response>> task = client.ExecuteTaskAsync<Response>(request, cancellationTokenSource.Token);
cancellationTokenSource.Cancel();
Assert.True(task.IsCanceled);
}
}
[Test]
public void Handles_GET_Request_Errors_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, UrlToStatusCodeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404");
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
Assert.AreEqual(HttpStatusCode.NotFound, task.Result.StatusCode);
}
}
[Test]
public void Handles_GET_Request_Errors_TaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, UrlToStatusCodeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404");
Task<IRestResponse<Response>> task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
Assert.Null(task.Result.Data);
}
}
[Test]
public void Can_Timeout_GET_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
IRestRequest request = new RestRequest("timeout", Method.GET).AddBody("Body_Content");
// Half the value of ResponseHandler.Timeout
request.Timeout = 500;
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
IRestResponse response = task.Result;
Assert.AreEqual(ResponseStatus.TimedOut, response.ResponseStatus);
}
}
[Test]
public void Can_Timeout_PUT_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
IRestRequest request = new RestRequest("timeout", Method.PUT).AddBody("Body_Content");
// Half the value of ResponseHandler.Timeout
request.Timeout = 500;
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
IRestResponse response = task.Result;
Assert.AreEqual(ResponseStatus.TimedOut, response.ResponseStatus);
}
}
private static void UrlToStatusCodeHandler(HttpListenerContext obj)
{
obj.Response.StatusCode = int.Parse(obj.Request.Url.Segments.Last());
}
public class ResponseHandler
{
private void error(HttpListenerContext context)
{
context.Response.StatusCode = 400;
context.Response.Headers.Add("Content-Type", "application/xml");
context.Response.OutputStream.WriteStringUtf8(
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Response>
<Error>
<Message>Not found!</Message>
</Error>
</Response>");
}
private void success(HttpListenerContext context)
{
context.Response.OutputStream.WriteStringUtf8(
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Response>
<Success>
<Message>Works!</Message>
</Success>
</Response>");
}
private void timeout(HttpListenerContext context)
{
Thread.Sleep(1000);
}
}
public class Response
{
public string Message { get; set; }
}
}
}