forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNonProtocolExceptionHandlingTests.cs
164 lines (138 loc) · 6.23 KB
/
NonProtocolExceptionHandlingTests.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
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class NonProtocolExceptionHandlingTests
{
/// <summary>
/// Success of this test is based largely on the behavior of your current DNS.
/// For example, if you're using OpenDNS this will test will fail; ResponseStatus will be Completed.
/// </summary>
[Test]
public void Handles_Non_Existent_Domain()
{
RestClient client = new RestClient("http://nonexistantdomainimguessing.org");
RestRequest request = new RestRequest("foo");
IRestResponse response = client.Execute(request);
Assert.AreEqual(ResponseStatus.Error, response.ResponseStatus);
}
public class StupidClass
{
public string Property { get; set; }
}
[Test]
public void Task_Handles_Non_Existent_Domain()
{
RestClient client = new RestClient("http://192.168.1.200:8001");
RestRequest request = new RestRequest("/")
{
RequestFormat = DataFormat.Json,
Method = Method.GET
};
Task<IRestResponse<StupidClass>> task = client.ExecuteTaskAsync<StupidClass>(request);
task.Wait();
IRestResponse<StupidClass> response = task.Result;
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(WebExceptionStatus.ConnectFailure, ((WebException)response.ErrorException).Status);
Assert.AreEqual(ResponseStatus.Error, response.ResponseStatus);
}
/// <summary>
/// Tests that RestSharp properly handles a non-protocol error.
/// Simulates a server timeout, then verifies that the ErrorException
/// property is correctly populated.
/// </summary>
[Test]
public void Handles_Server_Timeout_Error()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404")
{
Timeout = 500
};
IRestResponse response = client.Execute(request);
Assert.NotNull(response.ErrorException);
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(ResponseStatus.TimedOut, response.ResponseStatus);
}
}
[Test]
public void Handles_Server_Timeout_Error_Async()
{
const string baseUrl = "http://localhost:8888/";
ManualResetEvent resetEvent = new ManualResetEvent(false);
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404")
{
Timeout = 500
};
IRestResponse response = null;
client.ExecuteAsync(request, responseCb =>
{
response = responseCb;
resetEvent.Set();
});
resetEvent.WaitOne();
Assert.NotNull(response);
Assert.AreEqual(response.ResponseStatus, ResponseStatus.TimedOut);
Assert.NotNull(response.ErrorException);
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(response.ErrorException.Message, "The request timed-out.");
}
}
[Test]
public void Handles_Server_Timeout_Error_AsyncTask()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404") { Timeout = 500 };
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
IRestResponse response = task.Result;
Assert.NotNull(response);
Assert.AreEqual(response.ResponseStatus, ResponseStatus.TimedOut);
Assert.NotNull(response.ErrorException);
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(response.ErrorException.Message, "The request timed-out.");
}
}
/// <summary>
/// Tests that RestSharp properly handles a non-protocol error.
/// Simulates a server timeout, then verifies that the ErrorException
/// property is correctly populated.
/// </summary>
[Test]
public void Handles_Server_Timeout_Error_With_Deserializer()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404") { Timeout = 500 };
IRestResponse<Response> response = client.Execute<Response>(request);
Assert.Null(response.Data);
Assert.NotNull(response.ErrorException);
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(response.ResponseStatus, ResponseStatus.TimedOut);
}
}
/// <summary>
/// Simulates a long server process that should result in a client timeout
/// </summary>
/// <param name="context"></param>
public static void TimeoutHandler(HttpListenerContext context)
{
Thread.Sleep(101000);
}
}
}