-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handler_test.go
65 lines (57 loc) · 1.41 KB
/
error_handler_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
package poteto
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandleHttpError(t *testing.T) {
handler := httpErrorHandler{}
tests := []struct {
name string
err error
expected string
}{
{
"Test Not Handled Error -> Server Error",
errors.New("not httpError"),
`{"message":"Internal Server Error"}`,
},
{
"Test Handled Error",
NewHttpError(http.StatusBadRequest),
`{"message":"Bad Request"}`,
},
{
"Test wrapped Error",
&httpError{
Code: http.StatusBadRequest,
Message: "",
InternalError: NewHttpError(http.StatusBadRequest),
},
`{"message":"Bad Request"}`,
},
}
for _, it := range tests {
t.Run(it.name, func(t *testing.T) {
url := "/example.com"
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", url, nil)
ctx := NewContext(w, req).(*context)
handler.HandleHttpError(it.err, ctx)
if res := w.Body.String()[0:20]; res != it.expected[0:20] {
t.Errorf(res)
t.Errorf(it.expected)
t.Errorf("Unmatched")
}
})
}
}
func TestIsCommitedHandlerHttpErrorCase(t *testing.T) {
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/users", nil)
ctx := NewContext(w, req).(*context)
ctx.GetResponse().IsCommitted = true
handler := httpErrorHandler{}
handler.HandleHttpError(errors.New("error"), ctx)
}