forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStatusCodeTests.cs
151 lines (128 loc) · 5.18 KB
/
StatusCodeTests.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
using System;
using System.Linq;
using System.Net;
using NUnit.Framework;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class StatusCodeTests
{
[Test]
public void Handles_GET_Request_404_Error()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, UrlToStatusCodeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404");
IRestResponse response = client.Execute(request);
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
}
}
[Test]
public void Handles_GET_Request_404_Error_With_Body()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, UrlToStatusCodeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404");
request.AddBody("This is the body");
IRestResponse response = client.Execute(request);
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
}
}
private static void UrlToStatusCodeHandler(HttpListenerContext obj)
{
obj.Response.StatusCode = int.Parse(obj.Request.Url.Segments.Last());
}
[Test]
public void Handles_Different_Root_Element_On_Http_Error()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("error")
{
RootElement = "Success"
};
request.OnBeforeDeserialization = resp =>
{
if (resp.StatusCode == HttpStatusCode.BadRequest)
{
request.RootElement = "Error";
}
};
IRestResponse<Response> response = client.Execute<Response>(request);
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
Assert.AreEqual("Not found!", response.Data.Message);
}
}
[Test]
public void Handles_Default_Root_Element_On_No_Error()
{
Uri baseUrl = new Uri("http://localhost:8888/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.Generic<ResponseHandler>()))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("success")
{
RootElement = "Success"
};
request.OnBeforeDeserialization = resp =>
{
if (resp.StatusCode == HttpStatusCode.NotFound)
{
request.RootElement = "Error";
}
};
IRestResponse<Response> response = client.Execute<Response>(request);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("Works!", response.Data.Message);
}
}
}
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 errorwithbody(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>");
}
}
public class Response
{
public string Message { get; set; }
}
}