-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
status_handler_test.go
222 lines (181 loc) Β· 6.48 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package goyave
import (
"bytes"
"fmt"
"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)
req.Lang = server.Lang.GetDefault()
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))
}
func TestParseErrorStatusHandler(t *testing.T) {
tests := []struct {
name string
err error
expectedMessage string
expectedStatus int
}{
{
name: "InvalidJSONBody",
err: ErrInvalidJSONBody,
expectedMessage: "The request Content-Type indicates JSON, but the request body is empty or invalid.",
expectedStatus: http.StatusBadRequest,
},
{
name: "InvalidQuery",
err: ErrInvalidQuery,
expectedMessage: "Failed to parse query string due to invalid syntax or unexpected input format.",
expectedStatus: http.StatusBadRequest,
},
{
name: "InvalidContentForType",
err: ErrInvalidContentForType,
expectedMessage: "The request content does not match its type. E.g. invalid multipart/form-data or a problem with the file upload.",
expectedStatus: http.StatusBadRequest,
},
{
name: "ErrorInRequestBody",
err: ErrErrorInRequestBody,
expectedMessage: "Failed to read request body due to connection issues, timeouts, size mismatches, or corrupted data.",
expectedStatus: http.StatusBadRequest,
},
{
name: "OtherError",
err: errors.New("some.other.error"),
expectedMessage: "some.other.error",
expectedStatus: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
handler := &ParseErrorStatusHandler{}
handler.Init(resp.server)
req.Extra[ExtraParseError{}] = tt.err
resp.Status(tt.expectedStatus)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
expectedResponse := fmt.Sprintf(`{"error":"%s"}`, tt.expectedMessage) + "\n"
assert.Equal(t, expectedResponse, string(body))
assert.Equal(t, tt.expectedStatus, res.StatusCode)
})
}
}
func TestParseErrorStatusHandlerWithoutExtra(t *testing.T) {
req, resp, recorder := prepareStatusHandlerTest()
handler := &ParseErrorStatusHandler{}
handler.Init(resp.server)
resp.Status(http.StatusBadRequest)
handler.Handle(resp, req)
res := recorder.Result()
body, err := io.ReadAll(res.Body)
assert.NoError(t, res.Body.Close())
require.NoError(t, err)
expectedResponse := fmt.Sprintf(`{"error":"%s"}`, "Bad Request") + "\n"
assert.Equal(t, expectedResponse, string(body))
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
}