forked from g8rswimmer/go-sfdc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
156 lines (147 loc) · 3.53 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
package sfdc
import (
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestError_UnmarshalJSON(t *testing.T) {
type fields struct {
ErrorCode string
Message string
Fields []string
}
type args struct {
data []byte
}
tests := []struct {
name string
fields fields
args args
want *Error
wantErr bool
}{
{
name: "Success with Status Code",
fields: fields{},
args: args{
data: []byte(`
{
"statusCode" : "MALFORMED_ID",
"message" : "Contact ID: id value of incorrect type: 001xx000003DGb2999",
"fields" : [
"Id"
]
}`),
},
want: &Error{
ErrorCode: "MALFORMED_ID",
Message: "Contact ID: id value of incorrect type: 001xx000003DGb2999",
Fields: []string{"Id"},
},
wantErr: false,
},
{
name: "Success with Error Code",
fields: fields{},
args: args{
data: []byte(`
{
"fields" : [ "Id" ],
"message" : "Account ID: id value of incorrect type: 001900K0001pPuOAAU",
"errorCode" : "MALFORMED_ID"
}`),
},
want: &Error{
ErrorCode: "MALFORMED_ID",
Message: "Account ID: id value of incorrect type: 001900K0001pPuOAAU",
Fields: []string{"Id"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &Error{
ErrorCode: tt.fields.ErrorCode,
Message: tt.fields.Message,
Fields: tt.fields.Fields,
}
if err := e.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("Error.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
type alwaysError struct{}
func (alwaysError) Read(p []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}
func TestHandleError(t *testing.T) {
const (
singleErr = "{\"message\":\"invalid record id\",\"errorCode\":\"INVALID_ID_FIELD\",\"fields\":[\"id\"]}"
singleErrBody = "[" + singleErr + "]"
multipleErrBody = "[" + singleErr + "," + singleErr + "]"
)
tests := map[string]struct {
resp *http.Response
wantErr string
errors Errors
}{
"single_error": {
resp: &http.Response{
Status: "400 " + http.StatusText(400),
Body: ioutil.NopCloser(strings.NewReader(singleErrBody)),
},
wantErr: `400 Bad Request: INVALID_ID_FIELD: invalid record id (id)`,
errors: Errors{
{
Message: "invalid record id",
ErrorCode: "INVALID_ID_FIELD",
Fields: []string{"id"},
},
},
},
"multiple_error": {
resp: &http.Response{
Status: "400 " + http.StatusText(400),
Body: ioutil.NopCloser(strings.NewReader(multipleErrBody)),
},
wantErr: `400 Bad Request: INVALID_ID_FIELD: invalid record id (id), INVALID_ID_FIELD: invalid record id (id)`,
errors: Errors{
{
Message: "invalid record id",
ErrorCode: "INVALID_ID_FIELD",
Fields: []string{"id"},
},
{
Message: "invalid record id",
ErrorCode: "INVALID_ID_FIELD",
Fields: []string{"id"},
},
},
},
"read_body_error": {
resp: &http.Response{
Status: "500 " + http.StatusText(500),
Body: ioutil.NopCloser(alwaysError{}),
},
wantErr: `500 Internal Server Error: could not read the body with error: unexpected EOF`,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := HandleError(tt.resp)
t.Log("err:", err)
require.EqualError(t, err, tt.wantErr)
if tt.errors != nil {
sfdcErr := Errors{}
require.True(t, errors.As(err, &sfdcErr))
require.Equal(t, tt.errors, sfdcErr)
}
})
}
}