/
RestResponseTests.cs
164 lines (144 loc) · 6.12 KB
/
RestResponseTests.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
// ---------------------------------------------------------------------------------------
// <copyright file="RestResponseTests.cs" company="Corale">
// Copyright © 2015-2021 by Adam Hellberg and Brandon Scott.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// "Razer" is a trademark of Razer USA Ltd.
// </copyright>
// ---------------------------------------------------------------------------------------
namespace Colore.Tests.Rest
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Colore.Rest;
using Newtonsoft.Json;
using NUnit.Framework;
[TestFixture]
public class RestResponseTests
{
[TestCase(HttpStatusCode.OK)]
[TestCase(HttpStatusCode.Accepted)]
[TestCase(HttpStatusCode.Created)]
[TestCase(HttpStatusCode.BadRequest)]
[TestCase(HttpStatusCode.InternalServerError)]
[TestCase(HttpStatusCode.MethodNotAllowed)]
[TestCase(HttpStatusCode.Forbidden)]
[TestCase(HttpStatusCode.NotFound)]
public void ShouldConstructWithCorrectStatusCode(HttpStatusCode statusCode)
{
var response = new RestResponse<object>(statusCode, null);
Assert.AreEqual(statusCode, response.Status);
}
[TestCase(HttpStatusCode.OK)]
[TestCase(HttpStatusCode.Created)]
public void ShouldConsider200StatusesSuccessful(HttpStatusCode statusCode)
{
Assert.True(new RestResponse<object>(statusCode, null).IsSuccessful);
}
[TestCase(HttpStatusCode.Continue)]
public void ShouldConsider100StatusesNonSuccessful(HttpStatusCode statusCode)
{
Assert.False(new RestResponse<object>(statusCode, null).IsSuccessful);
}
[TestCase(HttpStatusCode.Moved)]
[TestCase(HttpStatusCode.MovedPermanently)]
[TestCase(HttpStatusCode.NotModified)]
public void ShouldConsider300StatusesNonSuccessful(HttpStatusCode statusCode)
{
Assert.False(new RestResponse<object>(statusCode, null).IsSuccessful);
}
[TestCase(HttpStatusCode.BadRequest)]
[TestCase(HttpStatusCode.Unauthorized)]
[TestCase(HttpStatusCode.Forbidden)]
public void ShouldConsider400StatusesNonSuccessful(HttpStatusCode statusCode)
{
Assert.False(new RestResponse<object>(statusCode, null).IsSuccessful);
}
[TestCase(HttpStatusCode.InternalServerError)]
[TestCase(HttpStatusCode.BadGateway)]
public void ShouldConsider500StatusesNonSuccessful(HttpStatusCode statusCode)
{
Assert.False(new RestResponse<object>(statusCode, null).IsSuccessful);
}
[TestCase("hello")]
[TestCase("hello world")]
[TestCase("This is a more complex string, with various 5ymb0l5 and %stuff")]
public void ShouldConstructWithCorrectContent(string content)
{
var response = new RestResponse<object>(HttpStatusCode.OK, content);
Assert.AreEqual(content, response.Content);
}
[Test]
public void ShouldConstructWithNullContent()
{
var response = new RestResponse<object>(HttpStatusCode.OK, null);
Assert.IsNull(response.Content);
}
[Test]
public void DataShouldBeNullIfContentIsNull()
{
var response = new RestResponse<object>(HttpStatusCode.OK, null);
Assert.IsNull(response.Data);
}
[Test]
public void DataShouldBeNullIfContentIsEmpty()
{
var response = new RestResponse<object>(HttpStatusCode.OK, string.Empty);
Assert.IsNull(response.Data);
}
[TestCase(" ")]
[TestCase(" ")]
[TestCase("\n")]
[TestCase("\t")]
[TestCase("\r")]
[TestCase("\r\n")]
[TestCase("\t\n")]
[TestCase("\n \t \r\t \r\n \n\r \t")]
public void DataShouldBeNullIfContentIsWhitespace(string content)
{
var response = new RestResponse<object>(HttpStatusCode.OK, content);
Assert.IsNull(response.Data);
}
[Test]
public void DataShouldDefaultIfContentIsInvalidJson()
{
const string Json = "This is not valid JSON, not at all!";
var response = new RestResponse<object>(HttpStatusCode.OK, Json);
Assert.IsNull(response.Data);
}
[Test]
public void DataShouldDeserializeProperly()
{
var data = new MyData { Name = "Foobar", Age = 42 };
var response = new RestResponse<MyData>(HttpStatusCode.OK, JsonConvert.SerializeObject(data));
Assert.True(data.Equals(response.Data));
}
[SuppressMessage("ReSharper", "MemberCanBePrivate.Local")]
private class MyData : IEquatable<MyData>
{
public string? Name { get; set; }
public int Age { get; set; }
public bool Equals(MyData? other)
{
return other != null && Name == other.Name && Age == other.Age;
}
}
}
}