-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_test.go
376 lines (310 loc) · 9.31 KB
/
engine_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package echo
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/im-kulikov/helium/module"
"github.com/labstack/echo/v4"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"go.uber.org/dig"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func newTestViper() *viper.Viper {
v := viper.New()
v.SetDefault("api.debug", true)
return v
}
func jsonUnmarshalError() error {
return &json.UnmarshalTypeError{
Value: "",
Type: reflect.TypeOf("something"),
Offset: 0,
Struct: "",
Field: "",
}
}
func jsonSyntaxError() error {
return &json.SyntaxError{
Offset: 50,
}
}
func xmlUnsuportTypeError() error {
return &xml.UnsupportedTypeError{
Type: reflect.TypeOf("something"),
}
}
func xmlSyntaxError() error {
return &xml.SyntaxError{}
}
type myTestCustomError struct {
Message string
Fail bool
}
func (e myTestCustomError) Error() string {
return e.Message
}
func (e myTestCustomError) FormatResponse(ctx echo.Context) error {
if e.Fail {
return errors.New(e.Message)
}
return ctx.String(http.StatusBadRequest, e.Message)
}
func customError() error {
return myTestCustomError{Message: "this is custom error"}
}
func customErrorFail() error {
return myTestCustomError{
Message: "this is custom error",
Fail: true,
}
}
func httpError() error {
return echo.NewHTTPError(http.StatusBadRequest, "some bad request")
}
func httpInternalError() error {
return echo.NewHTTPError(http.StatusInternalServerError, "some internal error")
}
func unknownError() error {
return errors.New("unknown error")
}
type testBuffer struct {
*bytes.Buffer
}
func (testBuffer) Sync() error {
return nil
}
type respWriter struct{}
func newTestWriter() http.ResponseWriter {
return &respWriter{}
}
func (*respWriter) Header() http.Header {
return make(http.Header)
}
func (*respWriter) Write([]byte) (int, error) {
return 0, errors.New("respWriter error")
}
func (*respWriter) WriteHeader(statusCode int) {}
func newTestLogger(rw zapcore.WriteSyncer) *zap.Logger {
encoderCfg := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}
core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), rw, zap.DebugLevel)
return zap.New(core)
}
func TestLoggerMiddleware(t *testing.T) {
var (
err error
va = NewValidator()
b = NewBinder(va)
vi = newTestViper()
dic = dig.New()
)
err = module.Provide(dic, module.Module{
{Constructor: NewEngine},
{Constructor: func() echo.Binder { return b }},
{Constructor: func() *viper.Viper { return vi }},
{Constructor: func() echo.Validator { return va }},
{Constructor: func() *zap.Logger { return zap.L() }},
// debug logger:
// {Constructor: func() (*zap.Logger, error) { return zap.NewDevelopment() }},
})
require.NoError(t, err)
err = dic.Invoke(func(e *echo.Echo, z *zap.Logger) {
require.True(t, e.Debug)
require.Equal(t, b, e.Binder)
require.Equal(t, va, e.Validator)
require.IsType(t, (*echoLogger)(nil), e.Logger)
e.Use(LoggerMiddleware(z))
{ // Fail
req, err := http.NewRequest(echo.GET, "http://localhost", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
}
{ // OK
req, err := http.NewRequest(echo.GET, "http://localhost", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
e.GET("/", func(ctx echo.Context) error { return ctx.String(http.StatusOK, "OK") })
e.ServeHTTP(rec, req)
}
})
require.NoError(t, err)
}
func TestEngine(t *testing.T) {
Convey("Engine", t, func(c C) {
c.Convey("try create and check new engine", func(c C) {
var err error
// Debug logger
// log, err := zap.NewDevelopment()
// c.So(err, ShouldBeNil)
var (
va = NewValidator()
b = NewBinder(va)
z = zap.L()
l = NewLogger(z)
vi = newTestViper()
_ = vi
dic = dig.New()
)
c.Convey("create engine with empty params", func(c C) {
err = module.Provide(dic, module.Module{
{Constructor: NewEngine},
})
c.So(err, ShouldBeNil)
err = dic.Invoke(func(e *echo.Echo) {
c.So(e.Binder, ShouldNotEqual, b)
c.So(e.Logger, ShouldNotEqual, l)
c.So(e.Validator, ShouldNotEqual, va)
c.So(e.Debug, ShouldBeFalse)
})
c.So(err, ShouldBeNil)
})
c.Convey("create engine with binder, zap.logger, validate and debug", func(c C) {
err = module.Provide(dic, module.Module{
{Constructor: func() echo.Validator { return va }},
{Constructor: func() echo.Binder { return b }},
{Constructor: func() *zap.Logger { return z }},
{Constructor: func() *viper.Viper { return vi }},
{Constructor: NewEngine},
})
c.So(err, ShouldBeNil)
err = dic.Invoke(func(e *echo.Echo) {
c.So(e.Binder, ShouldEqual, b)
_, ok := e.Logger.(*echoLogger)
c.So(ok, ShouldBeTrue)
c.So(e.Validator, ShouldEqual, va)
c.So(e.Debug, ShouldBeTrue)
})
c.So(err, ShouldBeNil)
})
c.Convey("create engine with binder, logger, validate and debug", func(c C) {
err = module.Provide(dic, module.Module{
{Constructor: func() echo.Validator { return va }},
{Constructor: func() echo.Binder { return b }},
{Constructor: func() echo.Logger { return l }},
{Constructor: func() *viper.Viper { return vi }},
{Constructor: NewEngine},
})
c.So(err, ShouldBeNil)
err = dic.Invoke(func(e *echo.Echo) {
c.So(e.Binder, ShouldEqual, b)
c.So(e.Logger, ShouldEqual, l)
c.So(e.Validator, ShouldEqual, va)
c.So(e.Debug, ShouldBeTrue)
})
c.So(err, ShouldBeNil)
})
})
c.Convey("try to capture errors", func(c C) {
var (
buf = new(bytes.Buffer)
z = newTestLogger(testBuffer{Buffer: buf})
e = NewEngine(EngineParams{Logger: z})
req, err = http.NewRequest("POST", "/some-url", nil)
rec = httptest.NewRecorder()
ctx = e.NewContext(req, rec)
)
c.Convey("try to capture json.Unmarshal errors", func(c C) {
c.So(err, ShouldBeNil)
err = jsonUnmarshalError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "JSON parse error: expected=")
})
c.Convey("try to capture json.Syntax errors", func(c C) {
c.So(err, ShouldBeNil)
err = jsonSyntaxError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "JSON parse error: offset=")
})
c.Convey("try to capture xml.Unmarshal errors", func(c C) {
c.So(err, ShouldBeNil)
err = xmlUnsuportTypeError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "XML parse error: type=")
})
c.Convey("try to capture xml.Syntax errors", func(c C) {
c.So(err, ShouldBeNil)
err = xmlSyntaxError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "XML parse error: line=")
})
c.Convey("try to capture custom errors", func(c C) {
c.So(err, ShouldBeNil)
err = customError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "this is custom error")
})
c.Convey("try to capture custom errors and fail", func(c C) {
c.So(err, ShouldBeNil)
err = customErrorFail()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldEqual, 0)
c.So(buf.String(), ShouldContainSubstring, "Capture error")
c.So(buf.String(), ShouldContainSubstring, "this is custom error")
})
c.Convey("try to capture http.Error", func(c C) {
c.So(err, ShouldBeNil)
err = httpError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "some bad request")
})
c.Convey("try to capture http.Error 500", func(c C) {
c.So(err, ShouldBeNil)
err = httpInternalError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, "some internal error")
c.So(buf.String(), ShouldContainSubstring, "Request error")
})
c.Convey("try to capture unknown Error", func(c C) {
c.So(err, ShouldBeNil)
err = unknownError()
c.So(err, ShouldBeError)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeGreaterThan, 0)
c.So(rec.Body.String(), ShouldContainSubstring, http.StatusText(http.StatusBadRequest))
})
c.Convey("try to capture ctx.JSON Error", func(c C) {
c.So(err, ShouldBeNil)
err = unknownError()
c.So(err, ShouldBeError)
ctx.Reset(
ctx.Request(),
echo.NewResponse(newTestWriter(), e),
)
captureError(z)(err, ctx)
c.So(rec.Body.Len(), ShouldBeZeroValue)
c.So(buf.Len(), ShouldBeGreaterThan, 0)
c.So(buf.String(), ShouldContainSubstring, "respWriter error")
})
})
})
}