forked from Teamwork/nylas-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
61 lines (57 loc) · 1.24 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
package nylas
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewError(t *testing.T) {
body := func(s string) io.ReadCloser {
return ioutil.NopCloser(bytes.NewBufferString(s))
}
tests := map[string]struct {
in *http.Response
out error
}{
"nylas error": {
in: &http.Response{
StatusCode: 400,
Body: body(`{
"message": "Invalid datetime value z for start_time",
"type": "invalid_request_error"
}`),
},
out: &Error{
StatusCode: 400,
Body: []byte(`{
"message": "Invalid datetime value z for start_time",
"type": "invalid_request_error"
}`),
Message: "Invalid datetime value z for start_time",
Type: "invalid_request_error",
},
},
"invalid json": {
in: &http.Response{
StatusCode: 403,
Body: body(`something went wrong`),
},
out: &Error{
StatusCode: 403,
Body: []byte(`something went wrong`),
Message: "something went wrong",
Type: "unknown_error_format",
},
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
err := NewError(tt.in)
if diff := cmp.Diff(err, tt.out); diff != "" {
t.Errorf("req body: (-got +want):\n%s", diff)
}
})
}
}