-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy patherrors_test.go
163 lines (149 loc) · 3.38 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
package signalfx
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRequestError(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
re *ResponseError
err string
}{
{
name: "simple request error",
re: &ResponseError{
code: 404,
route: "/internal/heathz",
details: "failed due to overworked",
},
err: "route \"/internal/heathz\" had issues with status code 404",
},
{
name: "missed details",
re: &ResponseError{
code: 404,
route: "/internal/heathz",
},
err: "route \"/internal/heathz\" had issues with status code 404",
},
{
name: "missed code",
re: &ResponseError{
route: "/internal/heathz",
},
err: "route \"/internal/heathz\" had issues with status code 0",
},
{
name: "missed all info",
re: &ResponseError{},
err: "route \"\" had issues with status code 0",
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.EqualError(t, tc.re, tc.err, "Must match the expected error string")
})
}
}
func TestNewRequestError(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
resp *http.Response
expect error
}{
{
name: "no response",
resp: nil,
expect: nil,
},
{
name: "successful response",
resp: &http.Response{
StatusCode: http.StatusOK,
Request: &http.Request{
URL: &url.URL{Host: "localhost", Path: "/internal/heathz"},
},
Body: io.NopCloser(strings.NewReader("successs")),
},
expect: nil,
},
{
name: "failed response",
resp: &http.Response{
StatusCode: http.StatusInternalServerError,
Request: &http.Request{
URL: &url.URL{Host: "localhost", Path: "/internal/heathz"},
},
Body: io.NopCloser(strings.NewReader("issue trying to connect")),
},
expect: &ResponseError{
code: http.StatusInternalServerError,
route: "/internal/heathz",
details: "issue trying to connect",
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expect, newResponseError(tc.resp, http.StatusOK), "Must match the exected value")
})
}
}
func TestIsRequestError(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
err error
expected bool
}{
{
name: "no error provided",
err: nil,
expected: false,
},
{
name: "not a request error",
err: errors.New("failed"),
expected: false,
},
{
name: "is a request error",
err: &ResponseError{},
expected: true,
},
{
name: "joined errors",
err: errors.Join(errors.New("boom"), &ResponseError{}),
expected: true,
},
{
name: "fmt error",
err: fmt.Errorf("check permissions: %w", &ResponseError{}),
expected: true,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, ok := AsResponseError(tc.err)
assert.Equal(t, tc.expected, ok, "Must match the expected value")
})
}
}
func TestRequestErrorMethods(t *testing.T) {
t.Parallel()
re := &ResponseError{code: http.StatusOK, route: "/heathz", details: "service alive"}
assert.Equal(t, 200, re.Code(), "Must match the expected code")
assert.Equal(t, "/heathz", re.Route(), "Must match the expected route")
assert.Equal(t, "service alive", re.Details(), "Must match the expected details")
}