Repository navigation
Expand file tree
/
Copy patherrors_http_test.go
More file actions
106 lines (96 loc) · 2.92 KB
/
Copy patherrors_http_test.go
File metadata and controls
106 lines (96 loc) · 2.92 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
package ragy
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"testing"
)
func TestErrorFromHTTPResponse(t *testing.T) {
t.Parallel()
tests := []struct {
name string
code int
wantIs error
}{
{"400", http.StatusBadRequest, ErrInvalidArgument},
{"401", http.StatusUnauthorized, ErrInvalidArgument},
{"404", http.StatusNotFound, ErrInvalidArgument},
{"429", http.StatusTooManyRequests, ErrUnavailable},
{"503", http.StatusServiceUnavailable, ErrUnavailable},
{"500", http.StatusInternalServerError, ErrUnavailable},
{"502", http.StatusBadGateway, ErrUnavailable},
{"200", http.StatusOK, ErrProtocol},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := ErrorFromHTTPResponse(tt.code, "test", "body")
if !errors.Is(err, tt.wantIs) {
t.Fatalf("ErrorFromHTTPResponse(%d): %v, want errors.Is(..., %v)", tt.code, err, tt.wantIs)
}
})
}
}
func TestWrapTransportError_CanceledUnchanged(t *testing.T) {
t.Parallel()
in := context.Canceled
out := WrapTransportError(in)
if !errors.Is(out, context.Canceled) {
t.Fatalf("WrapTransportError(canceled) = %v", out)
}
}
func TestWrapTransportError_DeadlineExceeded(t *testing.T) {
t.Parallel()
in := context.DeadlineExceeded
out := WrapTransportError(in)
if !errors.Is(out, ErrUnavailable) || !errors.Is(out, context.DeadlineExceeded) {
t.Fatalf("WrapTransportError(deadline) = %v", out)
}
}
type timeoutError struct{}
func (timeoutError) Error() string { return "timeout" }
func (timeoutError) Timeout() bool { return true }
func (timeoutError) Temporary() bool { return true }
func TestWrapTransportError_NetTimeout(t *testing.T) {
t.Parallel()
var err net.Error = timeoutError{}
out := WrapTransportError(err)
if !errors.Is(out, ErrUnavailable) {
t.Fatalf("WrapTransportError(timeout) = %v", out)
}
}
func TestWrapTransportError_Generic(t *testing.T) {
t.Parallel()
in := errors.New("connection refused")
out := WrapTransportError(in)
if !errors.Is(out, ErrUnavailable) || !errors.Is(out, in) {
t.Fatalf("WrapTransportError(refused) = %v", out)
}
}
func TestWrapBackendError(t *testing.T) {
t.Parallel()
if WrapBackendError(nil, "op") != nil {
t.Fatal("WrapBackendError(nil) != nil")
}
if err := WrapBackendError(context.Canceled, "op"); !errors.Is(err, context.Canceled) {
t.Fatalf("WrapBackendError(canceled) = %v", err)
}
if err := WrapBackendError(ErrInvalidArgument, "op"); !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("WrapBackendError(ErrInvalidArgument) = %v", err)
}
raw := errors.New("db down")
wrapped := WrapBackendError(raw, "search")
if !errors.Is(wrapped, ErrUnavailable) || !errors.Is(wrapped, raw) {
t.Fatalf("WrapBackendError(raw) = %v", wrapped)
}
}
func TestWrapBackendError_AlreadyUnavailable(t *testing.T) {
t.Parallel()
inner := fmt.Errorf("%w: inner", ErrUnavailable)
out := WrapBackendError(inner, "op")
if !errors.Is(out, ErrUnavailable) {
t.Fatalf("got %v", out)
}
}