forked from chargehound/chargehound-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
109 lines (95 loc) · 2.25 KB
/
errors_test.go
File metadata and controls
109 lines (95 loc) · 2.25 KB
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
package chargehound_test
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/chargehound/chargehound-go/v8.6.2"
)
var errorTests = []struct {
error string
code int
errorType chargehound.ErrorType
apiType string
message string
}{
{
"{\"error\": { \"status\": 404, \"message\": \"A dispute with id 'puppy' was not found\"}}",
404,
chargehound.NotFoundError,
"",
"Not Found: A dispute with id 'puppy' was not found",
},
{
"{\"error\": { \"status\": 404, \"type\": \"dispute_not_found\", \"message\": \"A dispute with id 'puppy' was not found\"}}",
404,
chargehound.NotFoundError,
"dispute_not_found",
"Not Found: A dispute with id 'puppy' was not found",
},
{
"{\"error\": { \"status\": 400, \"message\": \"Wrong param\"}}",
400,
chargehound.BadRequestError,
"",
"Bad Request: Wrong param",
},
{
"{\"error\": { \"status\": 401, \"message\": \"No user\"}}",
401,
chargehound.UnauthorizedError,
"",
"Unauthorized: No user",
},
{
"{\"error\": { \"status\": 403, \"message\": \"Wrong user\"}}",
403,
chargehound.ForbiddenError,
"",
"Forbidden: Wrong user",
},
{
"{\"error\": { \"status\": 500, \"message\": \"Server error\"}}",
500,
chargehound.InternalServerError,
"",
"Server Error: Server error",
},
}
func TestErrors(t *testing.T) {
ch := chargehound.New("api_key", nil)
for _, test := range errorTests {
error := test.error
code := test.code
errorType := test.errorType
apiType := test.apiType
message := test.message
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, error, code)
}))
defer ts.Close()
url, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
ch.Host = url.Host
ch.Protocol = url.Scheme + "://"
_, err = ch.Disputes.Retrieve(&chargehound.RetrieveDisputeParams{ID: "puppy"})
if err == nil {
t.Error(err)
}
if message != err.Error() {
t.Error("Expected error: ", message)
}
chErr := err.(chargehound.Error)
if code != chErr.StatusCode() {
t.Error("Expected status: ", code)
}
if errorType != chErr.Type() {
t.Error("Expected type: ", errorType)
}
if apiType != chErr.ApiErrorType() {
t.Error("Expected error type: ", apiType)
}
}
}