-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
173 lines (162 loc) · 3.4 KB
/
Copy patherrors_test.go
File metadata and controls
173 lines (162 loc) · 3.4 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
package routeit
import (
"errors"
"testing"
)
func TestRegisterHandler(t *testing.T) {
t.Run("panics", func(t *testing.T) {
tests := []struct {
name string
in HttpStatus
}{
{
name: "1xx",
in: StatusContinue,
},
{
name: "2xx",
in: StatusOK,
},
{
name: "3xx",
in: StatusPermanentRedirect,
},
{
name: "no code",
in: HttpStatus{},
},
{
name: "> 599",
in: HttpStatus{code: 600},
},
}
eh := newErrorHandlerNoMapper()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("wanted panic, got none")
}
}()
eh.RegisterHandler(tc.in, func(erw *ErrorResponseWriter, req *Request) {})
})
}
})
t.Run("stores handler", func(t *testing.T) {
tests := []struct {
name string
in HttpStatus
}{
{
name: "400",
in: StatusBadRequest,
},
{
name: "4xx",
in: StatusNotFound,
},
{
name: "500",
in: StatusInternalServerError,
},
{
name: "5xx",
in: StatusBadGateway,
},
{
// Although currently impossible due to how status codes must
// be constructed, this ensures the validity check is sound.
name: "599",
in: HttpStatus{code: 599},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
eh := newErrorHandlerNoMapper()
want := "from inside test handler"
eh.RegisterHandler(tc.in, func(erw *ErrorResponseWriter, req *Request) {
erw.Text(want)
})
h, found := eh.handlers[tc.in]
if !found {
t.Error("expected handler to be found, was not")
}
erw := &ErrorResponseWriter{rw: newResponseWithStatus(tc.in)}
h(erw, &Request{})
if string(erw.rw.bdy) != want {
t.Errorf("Body() = %#q, wanted %#q", string(erw.rw.bdy), want)
}
})
}
})
}
func TestIs(t *testing.T) {
tests := []struct {
name string
in error
cmp error
want bool
}{
{
name: "same status code",
in: ErrUnauthorized(),
cmp: ErrUnauthorized(),
want: true,
},
{
name: "same status code with different message",
in: ErrUnauthorized().WithMessage("foobar"),
cmp: ErrUnauthorized(),
want: true,
},
{
name: "same status code with different cause",
in: ErrUnauthorized().WithCause(errors.New("bad error")),
cmp: ErrUnauthorized(),
want: true,
},
{
name: "first nil",
cmp: ErrBadRequest(),
},
{
name: "cmp nil",
in: ErrBadRequest(),
},
{
name: "different status code",
in: ErrUnauthorized(),
cmp: ErrNotFound(),
},
{
name: "different status code (manual construction)",
in: ErrUnauthorized(),
cmp: func() error {
e := ErrUnauthorized()
e.status = StatusNotFound
return e
}(),
},
{
name: "same status code, invalid < 100",
in: &HttpError{status: HttpStatus{code: 99}},
cmp: &HttpError{status: HttpStatus{code: 99}},
},
{
name: "same status code, invalid >= 600",
in: &HttpError{status: HttpStatus{code: 600}},
cmp: &HttpError{status: HttpStatus{code: 600}},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := errors.Is(tc.in, tc.cmp)
if res != tc.want {
t.Errorf(`in = %v, cmp = %v, res = %t, want = %t`, tc.in, tc.cmp, res, tc.want)
}
})
}
}
func newErrorHandlerNoMapper() *errorHandler {
return newErrorHandler(func(e error) *HttpError { return nil })
}