-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_test.go
207 lines (173 loc) · 5.77 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
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
198
199
200
201
202
203
204
205
206
207
package otto
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Router_Error_Default(t *testing.T) {
t.Parallel()
r := NewRouter(false)
r.GET("/asd", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/asd", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, "some error", string(b))
}
func Test_Router_Error_Default_JSON(t *testing.T) {
t.Parallel()
r := NewRouter(false)
r.GET("/asd", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/asd", ts.URL), nil)
req.Header.Set("Accept", "application/json")
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
var body struct {
Err string `json:"error"`
Code int `json:"code"`
}
err = json.Unmarshal(b, &body)
assert.NoError(t, err, "should not return error on unmarshal")
assert.Equal(t, "some error", body.Err)
assert.Equal(t, 500, body.Code)
}
func Test_Router_Error_Custom(t *testing.T) {
t.Parallel()
r := NewRouter(false)
triggered := 0
r.SetErrorHandlers(map[int]ErrorHandler{
500: func(code int, err error, ctx Context) error {
triggered++
return ctx.String(code, err.Error())
},
})
r.GET("/asd", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/asd", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, "some error", string(b))
assert.Equal(t, 1, triggered)
}
func Test_Router_Group_Error_Custom(t *testing.T) {
t.Parallel()
r := NewRouter(false)
triggered := 0
r.SetErrorHandlers(map[int]ErrorHandler{
500: func(code int, err error, ctx Context) error {
triggered++
return ctx.String(code, err.Error())
},
})
r.GET("/first", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
// The Group should get a copy of the original Router's custom error handlers
r.Group("/group").GET("/second", func(ctx Context) error {
return ctx.Error(500, errors.New("some other error"))
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/first", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, "some error", string(b))
// Another request to a route under a Group should trigger the same custom error handler
req2, err := http.NewRequest("GET", fmt.Sprintf("%s/group/second", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res2, err := http.DefaultClient.Do(req2)
assert.NoError(t, err, "should not throw any error")
b2, _ := ioutil.ReadAll(res2.Body)
assert.Equal(t, "some other error", string(b2))
assert.Equal(t, 2, triggered)
}
func Test_Router_Group_Different_Error_Custom(t *testing.T) {
t.Parallel()
r := NewRouter(false)
// Set up first endpoint with custom error handler
triggered := 0
r.SetErrorHandlers(map[int]ErrorHandler{
500: func(code int, err error, ctx Context) error {
triggered++
return ctx.String(code, err.Error())
},
})
r.GET("/first", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
// Set up second endpoint under a Group with it's own custom error handler
r2 := r.Group("/group")
r2.GET("/second", func(ctx Context) error {
return ctx.Error(500, errors.New("some other error"))
})
groupTriggered := 0
r.SetErrorHandlers(map[int]ErrorHandler{
500: func(code int, err error, ctx Context) error {
groupTriggered++
return ctx.String(code, err.Error())
},
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/first", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, "some error", string(b))
// Another request to a route under a Group should trigger a different error handler
req2, err := http.NewRequest("GET", fmt.Sprintf("%s/group/second", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res2, err := http.DefaultClient.Do(req2)
assert.NoError(t, err, "should not throw any error")
b2, _ := ioutil.ReadAll(res2.Body)
assert.Equal(t, "some other error", string(b2))
assert.Equal(t, 1, triggered)
assert.Equal(t, 1, groupTriggered)
}
func Test_Router_Error_Fail(t *testing.T) {
t.Parallel()
r := NewRouter(false)
triggered := 0
r.SetErrorHandlers(map[int]ErrorHandler{
500: func(code int, err error, ctx Context) error {
triggered++
return err
},
})
r.GET("/asd", func(ctx Context) error {
return ctx.Error(500, errors.New("some error"))
})
ts := httptest.NewServer(r)
defer ts.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s/asd", ts.URL), nil)
assert.NoError(t, err, "should not throw any error")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "should not throw any error")
b, _ := ioutil.ReadAll(res.Body)
assert.Contains(t, string(b), "some error")
assert.Equal(t, 1, triggered)
}