-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors_test.go
129 lines (96 loc) · 3.11 KB
/
errors_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
package errors
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetErrorClass(t *testing.T) {
var cases = []struct {
code int
expected int
}{
{301, 0},
{302, 0},
{600, 0},
{900, 0},
{401, 400},
{402, 400},
{403, 400},
{500, 500},
{501, 500},
{502, 500},
}
for _, tc := range cases {
assert.Equal(t, tc.expected, GetErrorClass(tc.code))
}
}
func TestNewBag(t *testing.T) {
bag := NewBag()
assert.IsType(t, &Bag{}, bag)
assert.Equal(t, 0, len(bag.Errors))
}
func TestNewBagWithError(t *testing.T) {
bag := NewBagWithError(500, "Oops")
assert.IsType(t, &Bag{}, bag)
assert.Equal(t, 1, len(bag.Errors))
assert.Equal(t, 500, bag.Status)
}
func TestBagStatusWithMultipleErrorsWithSameStatus(t *testing.T) {
bag := NewBag()
bag.AddError(502, "Server Error 1")
bag.AddError(502, "Server Error 2")
jsonString := `{"errors":[{"detail":"Server Error 1","status":"502"},{"detail":"Server Error 2","status":"502"}],"status":"502"}`
jsonBytes, _ := json.Marshal(bag)
assert.Equal(t, 502, bag.Status)
assert.Equal(t, jsonString, string(jsonBytes))
}
func TestBagStatusWithMultipleErrorsWithSameClass(t *testing.T) {
bag := NewBag()
bag.AddError(501, "Server Error 1")
bag.AddError(502, "Server Error 2")
jsonString := `{"errors":[{"detail":"Server Error 1","status":"501"},{"detail":"Server Error 2","status":"502"}],"status":"500"}`
jsonBytes, _ := json.Marshal(bag)
assert.Equal(t, 500, bag.Status)
assert.Equal(t, jsonString, string(jsonBytes))
}
func TestBagStatusWithMultipleErrorsWithDifferentClass(t *testing.T) {
bag := NewBag()
bag.AddError(401, "Client Error 1")
bag.AddError(502, "Server Error 1")
jsonString := `{"errors":[{"detail":"Client Error 1","status":"401"},{"detail":"Server Error 1","status":"502"}],"status":"400"}`
jsonBytes, _ := json.Marshal(bag)
assert.Equal(t, 400, bag.Status)
assert.Equal(t, jsonString, string(jsonBytes))
}
func TestError(t *testing.T) {
err := NewError(400, "Test error")
assert.Error(t, err)
assert.Equal(t, "400: Test error", err.Error())
}
func TestErrorSetAboutLink(t *testing.T) {
link := "http://google.com"
err := NewError(400, "Test error")
err.SetAboutLink(link)
jsonString := `{"detail":"Test error","links":{"about":"http://google.com"},"status":"400"}`
jsonBytes, _ := json.Marshal(err)
assert.Equal(t, link, err.Links.About)
assert.Equal(t, jsonString, string(jsonBytes))
}
func TestSetPointer(t *testing.T) {
pointer := "/data/attributes/password"
err := NewError(400, "Test error")
err.SetPointer(pointer)
jsonString := `{"detail":"Test error","source":{"pointer":"/data/attributes/password"},"status":"400"}`
jsonBytes, _ := json.Marshal(err)
assert.Equal(t, pointer, err.Source.Pointer)
assert.Equal(t, jsonString, string(jsonBytes))
}
func TestSetParameter(t *testing.T) {
parameter := "order"
err := NewError(400, "Test error")
err.SetParameter(parameter)
jsonString := `{"detail":"Test error","source":{"parameter":"order"},"status":"400"}`
jsonBytes, _ := json.Marshal(err)
assert.Equal(t, parameter, err.Source.Parameter)
assert.Equal(t, jsonString, string(jsonBytes))
}