forked from andygrunwald/go-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
165 lines (135 loc) · 4 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
package jira
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func TestError_NewJiraError(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"errorMessages":["Issue does not exist or you do not have permission to see it."],"errors":{}}`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(resp, errors.New("Original http error"))
if err, ok := err.(*Error); !ok {
t.Errorf("Expected jira Error. Got %s", err.Error())
}
if !strings.Contains(err.Error(), "Issue does not exist") {
t.Errorf("Expected issue message. Got: %s", err.Error())
}
}
func TestError_NoResponse(t *testing.T) {
err := NewJiraError(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, `<html>Not JSON</html>`)
})
req, _ := testClient.NewRequest("GET", "/", nil)
resp, _ := testClient.Do(req, nil)
err := NewJiraError(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{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue 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{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue 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{"Issue does not exist"},
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
mapErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: map[string]string{
"issuetype": "issue type is required",
"title": "title is required",
},
}
noErr := &Error{
HTTPError: errors.New("Original http error"),
ErrorMessages: nil,
Errors: nil,
}
err := msgErr.Error()
if err != "Issue does not exist: Original http error" {
t.Errorf("Expected short message. Got %s", err)
}
err = mapErr.Error()
if !(strings.Contains(err, "issue 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{"Issue does not exist."},
Errors: map[string]string{
"issuetype": "issue 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, "Issue 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)
}
}