-
Notifications
You must be signed in to change notification settings - Fork 91
/
error_test.go
195 lines (174 loc) · 5.72 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
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
package cos
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)
// func Test_checkResponse_error(t *testing.T) {
// setup()
// defer teardown()
// mux.HandleFunc("/test_409", func(w http.ResponseWriter, r *http.Request) {
// w.WriteHeader(http.StatusConflict)
// fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
// <Error>
// <Code>BucketAlreadyExists</Code>
// <Message>The requested bucket name is not available.</Message>
// <Resource>testdelete-1253846586.cos.ap-guangzhou.myqcloud.com</Resource>
// <RequestId>NTk0NTRjZjZfNTViMjM1XzlkMV9hZTZh</RequestId>
// <TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NTk3NWFiNjAyMzdlM2RlMmVmOGNiNWIxYjYwNDFhYmQ=</TraceId>
// </Error>`)
// })
// req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_409", nil)
// resp, _ := client.client.Do(req)
// err := checkResponse(resp)
// if e, ok := err.(*ErrorResponse); ok {
// if e.Error() == "" {
// t.Errorf("Expected e.Error() not empty, got %+v", e.Error())
// }
// if e.Code != "BucketAlreadyExists" {
// t.Errorf("Expected BucketAlreadyExists error, got %+v", e.Code)
// }
// } else {
// t.Errorf("Expected ErrorResponse error, got %+v", err)
// }
// }
func Test_checkResponse_no_error(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/test_200", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `test`)
})
req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_200", nil)
resp, _ := client.client.Do(req)
err := checkResponse(resp)
if err != nil {
t.Errorf("Expected error == nil, got %+v", err)
}
}
func Test_checkResponse_with_error(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/test_409", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
<Error>
<Code>BucketAlreadyExists</Code>
<Message>The requested bucket name is not available.</Message>
<Resource>testdelete-1253846586.cos.ap-guangzhou.myqcloud.com</Resource>
<RequestId>NTk0NTRjZjZfNTViMjM1XzlkMV9hZTZh</RequestId>
<TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NTk3NWFiNjAyMzdlM2RlMmVmOGNiNWIxYjYwNDFhYmQ=</TraceId>
</Error>`)
})
req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_409", nil)
resp, _ := client.client.Do(req)
err := checkResponse(resp)
if e, ok := err.(*ErrorResponse); ok {
if e.Error() == "" {
t.Errorf("Expected e.Error() not empty, got %+v", e.Error())
}
if e.Code != "BucketAlreadyExists" {
t.Errorf("Expected BucketAlreadyExists error, got %+v", e.Code)
}
} else {
t.Errorf("Expected ErrorResponse error, got %+v", err)
}
}
func Test_IsNotFoundError(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/test_404", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Resource>examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/test_404</Resource>
<RequestId>NjA3OGY4NGFfNjJkMmMwYl8***</RequestId>
<TraceId>OGVmYzZiMmQzYjA2OWNh***</TraceId>
</Error>`)
})
req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_404", nil)
resp, _ := client.client.Do(req)
err := checkResponse(resp)
e, ok := IsCOSError(err)
if !ok {
t.Errorf("IsCOSError Return Failed")
}
ok = IsNotFoundError(e)
if !ok {
t.Errorf("IsNotFoundError Return Failed")
}
if e.Code != "NoSuchKey" {
t.Errorf("Expected NoSuchKey error, got %+v", e.Code)
}
_, ok = IsCOSError(nil)
if ok {
t.Errorf("IsCOSError Return Failed")
}
_, ok = IsCOSError(errors.New("test error"))
if ok {
t.Errorf("IsNotFoundError Return Failed")
}
ok = IsNotFoundError(nil)
if ok {
t.Errorf("IsNotFoundError Return Failed")
}
ok = IsNotFoundError(errors.New("test error"))
if ok {
t.Errorf("IsNotFoundError Return Failed")
}
}
func Test_CheckReponse(t *testing.T) {
setup()
defer teardown()
resp := &http.Response{
StatusCode: 404,
Header: http.Header{},
Body: ioutil.NopCloser(strings.NewReader(`{
"code": 404,
"message": "404 NotFound",
"request_id": "requestid"}`)),
}
resp.Header.Add("Content-Type", "application/json")
err := checkResponse(resp)
e, _ := err.(*ErrorResponse)
if e.Code != "404" || e.Message != "404 NotFound" || e.RequestID != "requestid" {
t.Errorf("checkResponse failed: %v", e)
}
}
func Test_RetryError(t *testing.T) {
var errs RetryError
errs.Add(errors.New("err1"))
errs.Add(errors.New("err2"))
errs.Add(errors.New("err3"))
if errs.Error() != "err1; err2; err3" {
t.Errorf("RetryError return err: %v", errs.Error())
}
}
func Test_ErrorResponse(t *testing.T) {
setup()
defer teardown()
request, _ := http.NewRequest(http.MethodGet, client.BaseURL.BucketURL.String(), nil)
request.Header.Add("X-Cos-Request-Id", "requestid")
request.Header.Add("X-Cos-Trace-Id", "traceid")
resp := &http.Response{
StatusCode: 404,
Header: http.Header{},
Request: request,
Body: ioutil.NopCloser(strings.NewReader(`{
"code": 404,
"message": "404 NotFound",
"request_id": "requestid"}`)),
}
resp.Header.Add("Content-Type", "application/json")
err := &ErrorResponse{
Response: resp,
}
except := fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)", resp.Request.Method, client.BaseURL.BucketURL.String(), resp.StatusCode, err.Code, err.Message, err.RequestID, err.TraceID)
if err.Error() != except {
t.Errorf("error message is invalid, return: %v, except: %v", err.Error(), except)
}
}