forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_handler_test.go
133 lines (109 loc) Β· 3.99 KB
/
status_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
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
package goyave
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goyave.dev/goyave/v5/config"
"goyave.dev/goyave/v5/slog"
"goyave.dev/goyave/v5/util/errors"
"goyave.dev/goyave/v5/validation"
)
func prepareStatusHandlerTest() (*Request, *Response, *httptest.ResponseRecorder) {
server, err := New(Options{Config: config.LoadDefault()})
if err != nil {
panic(err)
}
httpReq := httptest.NewRequest(http.MethodGet, "/test", nil)
req := NewRequest(httpReq)
recorder := httptest.NewRecorder()
resp := NewResponse(server, req, recorder)
return req, resp, recorder
}
func TestPanicStatusHandler(t *testing.T) {
t.Run("no_debug", func(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
resp.server.config.Set("app.debug", false)
handler := &PanicStatusHandler{}
handler.Init(resp.server)
resp.err = errors.New("test error").(*errors.Error)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, "{\"error\":\"Internal Server Error\"}\n", string(body))
})
t.Run("debug", func(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
resp.server.config.Set("app.debug", true)
logBuffer := &bytes.Buffer{}
resp.server.Logger = slog.New(slog.NewHandler(false, logBuffer))
handler := &PanicStatusHandler{}
handler.Init(resp.server)
resp.err = errors.New("test error").(*errors.Error)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, "{\"error\":\"test error\"}\n", string(body))
// Error and stacktrace already printed by the recovery middleware or `response.Error`
// (those are not executed in this test, thus leaving the log buffer empty)
assert.Empty(t, logBuffer.String())
})
t.Run("nil_error", func(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
resp.server.config.Set("app.debug", true)
logBuffer := &bytes.Buffer{}
resp.server.Logger = slog.New(slog.NewHandler(false, logBuffer))
handler := &PanicStatusHandler{}
handler.Init(resp.server)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, "{\"error\":null}\n", string(body))
// Error and stacktrace are not printed to console because recovery middleware
// is not executed (no error raised, we just set the response status to 500 for example)
assert.Empty(t, logBuffer.String())
})
}
func TestErrorStatusHandler(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
handler := &ErrorStatusHandler{}
handler.Init(resp.server)
resp.Status(http.StatusNotFound)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, "{\"error\":\"Not Found\"}\n", string(body))
}
func TestValidationStatusHandler(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
handler := &ValidationStatusHandler{}
handler.Init(resp.server)
req.Extra[ExtraValidationError{}] = &validation.Errors{
Errors: []string{"The body is required"},
Fields: validation.FieldsErrors{
"field": &validation.Errors{Errors: []string{"The field is required"}},
},
}
req.Extra[ExtraQueryValidationError{}] = &validation.Errors{
Fields: validation.FieldsErrors{
"query": &validation.Errors{Errors: []string{"The query is required"}},
},
}
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
assert.Equal(t, "{\"error\":{\"body\":{\"fields\":{\"field\":{\"errors\":[\"The field is required\"]}},\"errors\":[\"The body is required\"]},\"query\":{\"fields\":{\"query\":{\"errors\":[\"The query is required\"]}}}}}\n", string(body))
}