forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
202 lines (170 loc) · 5.36 KB
/
errors_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
package linodego
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-resty/resty/v2"
"github.com/google/go-cmp/cmp"
)
type testStringer string
func (t testStringer) String() string {
return string(t)
}
type testError string
func (e testError) Error() string {
return string(e)
}
func restyError(reason, field string) *resty.Response {
var reasons []APIErrorReason
// allow for an empty reasons
if reason != "" && field != "" {
reasons = append(reasons, APIErrorReason{
Reason: reason,
Field: field,
})
}
return &resty.Response{
RawResponse: &http.Response{
StatusCode: 500,
},
Request: &resty.Request{
Error: &APIError{
Errors: reasons,
},
},
}
}
func TestNewError(t *testing.T) {
if NewError(nil) != nil {
t.Errorf("nil error should return nil")
}
if NewError(struct{}{}).Code != ErrorUnsupported {
t.Error("empty struct should return unsupported error type")
}
err := errors.New("test")
newErr := NewError(err)
if newErr.Message != err.Error() && newErr.Code != ErrorFromError {
t.Error("error should return ErrorFromError")
}
if newErr.Error() != "[002] test" {
t.Error("Error should support Error() formatter with code")
}
if NewError(newErr) != newErr {
t.Error("Error should be itself")
}
if err := NewError(&resty.Response{Request: &resty.Request{}}); err.Message != "Unexpected Resty Error Response, no error" {
t.Error("Unexpected Resty Error Response, no error")
}
if err := NewError(restyError("testreason", "testfield")); err.Message != "[testfield] testreason" {
t.Error("rest response error should should be set")
}
if err := NewError("stringerror"); err.Message != "stringerror" || err.Code != ErrorFromString {
t.Errorf("string error should be set")
}
if err := NewError(testStringer("teststringer")); err.Message != "teststringer" || err.Code != ErrorFromStringer {
t.Errorf("error should be set for a stringer interface")
}
if err := NewError(testError("testerror")); err.Message != "testerror" || err.Code != ErrorFromError {
t.Errorf("error should be set for an error interface")
}
}
func createTestServer(method, route, contentType, body string, statusCode int) (*httptest.Server, *Client) {
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method == method && r.URL.Path == route {
rw.Header().Add("Content-Type", contentType)
rw.WriteHeader(statusCode)
rw.Write([]byte(body))
return
}
rw.WriteHeader(http.StatusNotImplemented)
})
ts := httptest.NewServer(h)
client := NewClient(nil)
client.SetBaseURL(ts.URL)
return ts, &client
}
func TestCoupleAPIErrors(t *testing.T) {
t.Run("not nil error generates error", func(t *testing.T) {
err := errors.New("test")
if _, err := coupleAPIErrors(nil, err); !cmp.Equal(err, NewError(err)) {
t.Errorf("expect a not nil error to be returned as an Error")
}
})
t.Run("resty 500 response error with reasons", func(t *testing.T) {
if _, err := coupleAPIErrors(restyError("testreason", "testfield"), nil); err.Error() != "[500] [testfield] testreason" {
t.Error("resty error should return with proper format [code] [field] reason")
}
})
t.Run("resty 500 response error without reasons", func(t *testing.T) {
if _, err := coupleAPIErrors(restyError("", ""), nil); err != nil {
t.Error("resty error with no reasons should return no error")
}
})
t.Run("resty response with nil error", func(t *testing.T) {
emptyErr := &resty.Response{
RawResponse: &http.Response{
StatusCode: 500,
},
Request: &resty.Request{
Error: nil,
},
}
if _, err := coupleAPIErrors(emptyErr, nil); err != nil {
t.Error("resty error with no reasons should return no error")
}
})
t.Run("generic html error", func(t *testing.T) {
rawResponse := `<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>`
route := "/v4/linode/instances/123"
ts, client := createTestServer(http.MethodGet, route, "text/html", rawResponse, http.StatusInternalServerError)
// client.SetDebug(true)
defer ts.Close()
expectedError := Error{
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}
_, err := coupleAPIErrors(client.R(context.Background()).SetResult(&Instance{}).Get(ts.URL + route))
if diff := cmp.Diff(expectedError, err); diff != "" {
t.Errorf("expected error to match but got diff:\n%s", diff)
}
})
t.Run("bad gateway error", func(t *testing.T) {
rawResponse := []byte(`<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>`)
buf := io.NopCloser(bytes.NewBuffer(rawResponse))
resp := &resty.Response{
Request: &resty.Request{
Error: errors.New("Bad Gateway"),
},
RawResponse: &http.Response{
Header: http.Header{
"Content-Type": []string{"text/html"},
},
StatusCode: http.StatusBadGateway,
Body: buf,
},
}
expectedError := Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}
if _, err := coupleAPIErrors(resp, nil); !cmp.Equal(err, expectedError) {
t.Errorf("expected error %#v to match error %#v", err, expectedError)
}
})
}