-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors_test.go
More file actions
197 lines (173 loc) · 5.45 KB
/
Copy patherrors_test.go
File metadata and controls
197 lines (173 loc) · 5.45 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
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
package internal
import (
"errors"
"net/http"
"testing"
)
func TestNewAuthError(t *testing.T) {
err := NewAuthError("msg", errors.New("inner"))
if err.Message != "msg" || err.Err.Error() != "inner" {
t.Errorf("unexpected error: %+v", err)
}
}
func TestNewConfigError(t *testing.T) {
err := NewConfigError("field", "val", "msg", errors.New("inner"))
if err.Field != "field" || err.Value != "val" || err.Message != "msg" || err.Err.Error() != "inner" {
t.Errorf("unexpected error: %+v", err)
}
}
func TestNewNetworkError(t *testing.T) {
err := NewNetworkError("op", "url", "msg", errors.New("inner"))
if err.Operation != "op" || err.URL != "url" || err.Message != "msg" || err.Err.Error() != "inner" {
t.Errorf("unexpected error: %+v", err)
}
}
func TestNewValidationError(t *testing.T) {
err := NewValidationError("field", "val", "msg", errors.New("inner"))
if err.Field != "field" || err.Value != "val" || err.Message != "msg" || err.Err.Error() != "inner" {
t.Errorf("unexpected error: %+v", err)
}
}
func TestNewProxyError(t *testing.T) {
err := NewProxyError("op", "msg", errors.New("inner"))
if err.Operation != "op" || err.Message != "msg" || err.Err.Error() != "inner" {
t.Errorf("unexpected error: %+v", err)
}
}
func TestErrorImplementations(t *testing.T) {
auth := NewAuthError("msg", errors.New("inner"))
if auth.Error() == "" {
t.Error("expected non-empty error string")
}
if !errors.Is(auth, auth.Err) {
t.Error("expected errors.Is to match inner error")
}
conf := NewConfigError("f", "v", "m", errors.New("inner"))
if conf.Error() == "" {
t.Error("expected non-empty error string")
}
if !errors.Is(conf, conf.Err) {
t.Error("expected errors.Is to match inner error")
}
neterr := NewNetworkError("op", "url", "m", errors.New("inner"))
if neterr.Error() == "" {
t.Error("expected non-empty error string")
}
if !errors.Is(neterr, neterr.Err) {
t.Error("expected errors.Is to match inner error")
}
val := NewValidationError("f", "v", "m", errors.New("inner"))
if val.Error() == "" {
t.Error("expected non-empty error string")
}
if !errors.Is(val, val.Err) {
t.Error("expected errors.Is to match inner error")
}
proxy := NewProxyError("op", "m", errors.New("inner"))
if proxy.Error() == "" {
t.Error("expected non-empty error string")
}
if !errors.Is(proxy, proxy.Err) {
t.Error("expected errors.Is to match inner error")
}
}
func TestWriteHTTPError(t *testing.T) {
w := &mockResponseWriter{}
WriteHTTPError(w, http.StatusBadRequest, "bad request")
if w.status != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.status)
}
if w.header.Get("Content-Type") != "application/json" {
t.Errorf("expected application/json header")
}
}
func TestWriteHTTPErrorWithDetails(t *testing.T) {
w := &mockResponseWriter{}
WriteHTTPErrorWithDetails(w, http.StatusForbidden, "auth", "forbidden", "details")
if w.status != http.StatusForbidden {
t.Errorf("expected status %d, got %d", http.StatusForbidden, w.status)
}
if w.header.Get("Content-Type") != "application/json" {
t.Errorf("expected application/json header")
}
}
func TestWriteAuthenticationError(t *testing.T) {
w := &mockResponseWriter{}
WriteAuthenticationError(w)
if w.status != http.StatusUnauthorized {
t.Errorf("expected status %d, got %d", http.StatusUnauthorized, w.status)
}
}
func TestWriteAuthorizationError(t *testing.T) {
w := &mockResponseWriter{}
WriteAuthorizationError(w)
if w.status != http.StatusForbidden {
t.Errorf("expected status %d, got %d", http.StatusForbidden, w.status)
}
}
func TestWriteValidationError(t *testing.T) {
w := &mockResponseWriter{}
WriteValidationError(w, "validation error")
if w.status != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.status)
}
}
func TestWriteInternalError(t *testing.T) {
w := &mockResponseWriter{}
WriteInternalError(w)
if w.status != http.StatusInternalServerError {
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.status)
}
}
func TestWriteServiceUnavailableError(t *testing.T) {
w := &mockResponseWriter{}
WriteServiceUnavailableError(w)
if w.status != http.StatusServiceUnavailable {
t.Errorf("expected status %d, got %d", http.StatusServiceUnavailable, w.status)
}
}
func TestWriteRateLimitError(t *testing.T) {
w := &mockResponseWriter{}
WriteRateLimitError(w)
if w.status != http.StatusTooManyRequests {
t.Errorf("expected status %d, got %d", http.StatusTooManyRequests, w.status)
}
}
func TestErrorTypeChecks(t *testing.T) {
auth := NewAuthError("msg", nil)
if !IsAuthenticationError(auth) {
t.Error("expected IsAuthenticationError true")
}
conf := NewConfigError("f", "v", "m", nil)
if !IsConfigurationError(conf) {
t.Error("expected IsConfigurationError true")
}
neterr := NewNetworkError("op", "url", "m", nil)
if !IsNetworkError(neterr) {
t.Error("expected IsNetworkError true")
}
val := NewValidationError("f", "v", "m", nil)
if !IsValidationError(val) {
t.Error("expected IsValidationError true")
}
proxy := NewProxyError("op", "m", nil)
if !IsProxyError(proxy) {
t.Error("expected IsProxyError true")
}
}
type mockResponseWriter struct {
header http.Header
status int
}
func (m *mockResponseWriter) Header() http.Header {
if m.header == nil {
m.header = make(http.Header)
}
return m.header
}
func (m *mockResponseWriter) Write(b []byte) (int, error) {
return len(b), nil
}
func (m *mockResponseWriter) WriteHeader(statusCode int) {
m.status = statusCode
}