-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorjson_test.go
203 lines (185 loc) · 5.14 KB
/
errorjson_test.go
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
package mstdn
import (
"testing"
"strings"
)
func TestErrorJSON(t *testing.T) {
tests := []struct{
StatusCode int
Message []interface{}
Expected string
}{
{
StatusCode: 400,
Message: []interface{}{},
Expected: `{"error":"Bad Request"}`,
},
{
StatusCode: 401,
Message: []interface{}{},
Expected: `{"error":"Unauthorized"}`,
},
{
StatusCode: 403,
Message: []interface{}{},
Expected: `{"error":"Forbidden"}`,
},
{
StatusCode: 404,
Message: []interface{}{},
Expected: `{"error":"Not Found"}`,
},
{
StatusCode: 422,
Message: []interface{}{},
Expected: `{"error":"Unprocessable Entity"}`,
},
{
StatusCode: 429,
Message: []interface{}{},
Expected: `{"error":"Too Many Requests"}`,
},
{
StatusCode: 500,
Message: []interface{}{},
Expected: `{"error":"Internal Server Error"}`,
},
{
StatusCode: 503,
Message: []interface{}{},
Expected: `{"error":"Service Unavailable"}`,
},
{
StatusCode: 400,
Message: []interface{}{"very bad request"},
Expected: `{"error":"very bad request"}`,
},
{
StatusCode: 401,
Message: []interface{}{"get out of here"},
Expected: `{"error":"get out of here"}`,
},
{
StatusCode: 403,
Message: []interface{}{"no!!!!"},
Expected: `{"error":"no!!!!"}`,
},
{
StatusCode: 404,
Message: []interface{}{"huh?"},
Expected: `{"error":"huh?"}`,
},
{
StatusCode: 422,
Message: []interface{}{"WTF?"},
Expected: `{"error":"WTF?"}`,
},
{
StatusCode: 429,
Message: []interface{}{"f-off"},
Expected: `{"error":"f-off"}`,
},
{
StatusCode: 500,
Message: []interface{}{"crap!"},
Expected: `{"error":"crap!"}`,
},
{
StatusCode: 503,
Message: []interface{}{"nope"},
Expected: `{"error":"nope"}`,
},
{
StatusCode: 400,
Message: []interface{}{"very bad request", "you have been naughty"},
Expected: `{"error":"very bad request","error_description":"you have been naughty"}`,
},
{
StatusCode: 401,
Message: []interface{}{"get out of here", "access denied!!! — you can keep on knocking but you can't come in"},
Expected: `{"error":"get out of here","error_description":"access denied!!! — you can keep on knocking but you can't come in"}`,
},
{
StatusCode: 403,
Message: []interface{}{"no!!!!", "go away"},
Expected: `{"error":"no!!!!","error_description":"go away"}`,
},
{
StatusCode: 404,
Message: []interface{}{"huh?", "what are you talking about"},
Expected: `{"error":"huh?","error_description":"what are you talking about"}`,
},
{
StatusCode: 422,
Message: []interface{}{"WTF?", "i don't understand the words coming out of your mouth"},
Expected: `{"error":"WTF?","error_description":"i don't understand the words coming out of your mouth"}`,
},
{
StatusCode: 429,
Message: []interface{}{"f-off", "slow it down, back it up"},
Expected: `{"error":"f-off","error_description":"slow it down, back it up"}`,
},
{
StatusCode: 500,
Message: []interface{}{"crap!", "i f-ed up"},
Expected: `{"error":"crap!","error_description":"i f-ed up"}`,
},
{
StatusCode: 503,
Message: []interface{}{"nope", "it not workie"},
Expected: `{"error":"nope","error_description":"it not workie"}`,
},
{
StatusCode: 400,
Message: []interface{}{"very bad request", "you have been naughty", " :-)"},
Expected: `{"error":"very bad request","error_description":"you have been naughty :-)"}`,
},
{
StatusCode: 401,
Message: []interface{}{"get out of here", "access denied!!! — you can keep on knocking but you can't come in", "."},
Expected: `{"error":"get out of here","error_description":"access denied!!! — you can keep on knocking but you can't come in."}`,
},
{
StatusCode: 403,
Message: []interface{}{"no!!!!", "go away", "!"},
Expected: `{"error":"no!!!!","error_description":"go away!"}`,
},
{
StatusCode: 404,
Message: []interface{}{"huh?", "what are you talking about", "?"},
Expected: `{"error":"huh?","error_description":"what are you talking about?"}`,
},
{
StatusCode: 422,
Message: []interface{}{"WTF?", "i don't understand the words coming out of your mouth", "."},
Expected: `{"error":"WTF?","error_description":"i don't understand the words coming out of your mouth."}`,
},
{
StatusCode: 429,
Message: []interface{}{"f-off", "slow it down, back it up", "."},
Expected: `{"error":"f-off","error_description":"slow it down, back it up."}`,
},
{
StatusCode: 500,
Message: []interface{}{"crap!", "i f-ed up", " :-("},
Expected: `{"error":"crap!","error_description":"i f-ed up :-("}`,
},
{
StatusCode: 503,
Message: []interface{}{"nope", "it not workie", "."},
Expected: `{"error":"nope","error_description":"it not workie."}`,
},
}
for testNumber, test := range tests {
var actualBuffer strings.Builder
errorJSON(&actualBuffer, test.StatusCode, test.Message...)
actual := actualBuffer.String()
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED:\n%s", expected)
t.Logf("ACTUAL:\n%s", actual)
continue
}
}
}