-
Notifications
You must be signed in to change notification settings - Fork 7
/
error_test.go
205 lines (166 loc) · 5.3 KB
/
error_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
204
205
package openproject
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func TestError_NewOpenProjectError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"errorMessages":["Workpackage does not exist or you do not have permission to see it."],"errors":{}}`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewOpenProjectError(resp, errors.New("Original http error"))
if err, ok := err.(*Error); !ok {
t.Errorf("Expected OpenProject Error. Got %s", err.Error())
}
if !strings.Contains(err.Error(), "Workpackage does not exist") {
t.Errorf("Expected workpackage message. Got: %s", err.Error())
}
}
func TestError_NoResponse(t *testing.T) {
err := NewOpenProjectError(nil, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "Original http error") {
t.Errorf("Expected the original error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "No response") {
t.Errorf("Expected the 'No response' error message: Got\n%s\n", msg)
}
}
func TestError_NoJSON(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `Original message body`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewOpenProjectError(resp, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "200 OK: Original message body: Original http error") {
t.Errorf("Expected the HTTP status: Got\n%s\n", msg)
}
}
func TestError_Unauthorized_NilError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, `User is not authorized`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewOpenProjectError(resp, nil)
msg := err.Error()
if !strings.Contains(msg, "401 Unauthorized:User is not authorized") {
t.Errorf("Expected Unauthorized HTTP status: Got\n%s\n", msg)
}
}
func TestError_BadJSON(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `<html>Not JSON</html>`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewOpenProjectError(resp, errors.New("Original http error"))
msg := err.Error()
if !strings.Contains(msg, "could not parse JSON") {
t.Errorf("Expected the 'could not parse JSON' error message: Got\n%s\n", msg)
}
}
func TestError_NilOriginalMessage(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected an error message. Got a panic (%v)", r)
}
}()
msgErr := &Error{
HTTPError: nil,
ErrorMessages: []string{"WorkPackage does not exist"},
Errors: map[string]string{
"workpackagetype": "WorkPackage type is required",
"title": "title is required",
},
}
_ = msgErr.Error()
}
func TestError_NilOriginalMessageLongError(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Expected an error message. Got a panic (%v)", r)
}
}()
msgErr := &Error{
HTTPError: nil,
ErrorMessages: []string{"WorkPackage does not exist"},
Errors: map[string]string{
"workpackagetype": "WorkPackage type is required",
"title": "title is required",
},
}
_ = msgErr.LongError()
}
func TestError_ShortMessage(t *testing.T) {
msgErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: []string{"WorkPackage does not exist"},
Errors: map[string]string{
"workpackagetype": "WorkPackage type is required",
"title": "title is required",
},
}
mapErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: map[string]string{
"workpackagetype": "WorkPackage type is required",
"title": "title is required",
},
}
noErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: nil,
}
err := msgErr.Error()
if err != "WorkPackage does not exist: Original http error" {
t.Errorf("Expected short message. Got %s", err)
}
err = mapErr.Error()
if !(strings.Contains(err, "WorkPackage type is required") || strings.Contains(err, "title is required")) {
t.Errorf("Expected short message. Got %s", err)
}
err = noErr.Error()
if err != "Original http error" {
t.Errorf("Expected original error message. Got %s", err)
}
}
func TestError_LongMessage(t *testing.T) {
longError := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: []string{"WorkPackage does not exist."},
Errors: map[string]string{
"workpackagetype": "WorkPackage type is required",
"title": "title is required",
},
}
msg := longError.LongError()
if !strings.Contains(msg, "Original http error") {
t.Errorf("Expected the error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "WorkPackage does not exist") {
t.Errorf("Expected the error message: Got\n%s\n", msg)
}
if !strings.Contains(msg, "title - title is required") {
t.Errorf("Expected the error map: Got\n%s\n", msg)
}
}