MCPcopy
hub / github.com/gin-gonic/gin / TestErrorLogger

Function TestErrorLogger

logger_test.go:376–401  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

374}
375
376func TestErrorLogger(t *testing.T) {
377 router := New()
378 router.Use(ErrorLogger())
379 router.GET("/error", func(c *Context) {
380 c.Error(errors.New("this is an error")) //nolint: errcheck
381 })
382 router.GET("/abort", func(c *Context) {
383 c.AbortWithError(http.StatusUnauthorized, errors.New("no authorized")) //nolint: errcheck
384 })
385 router.GET("/print", func(c *Context) {
386 c.Error(errors.New("this is an error")) //nolint: errcheck
387 c.String(http.StatusInternalServerError, "hola!")
388 })
389
390 w := PerformRequest(router, http.MethodGet, "/error")
391 assert.Equal(t, http.StatusOK, w.Code)
392 assert.JSONEq(t, "{\"error\":\"this is an error\"}", w.Body.String())
393
394 w = PerformRequest(router, http.MethodGet, "/abort")
395 assert.Equal(t, http.StatusUnauthorized, w.Code)
396 assert.JSONEq(t, "{\"error\":\"no authorized\"}", w.Body.String())
397
398 w = PerformRequest(router, http.MethodGet, "/print")
399 assert.Equal(t, http.StatusInternalServerError, w.Code)
400 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())
401}
402
403func TestLoggerWithWriterSkippingPaths(t *testing.T) {
404 buffer := new(strings.Builder)

Callers

nothing calls this directly

Calls 8

NewFunction · 0.85
ErrorLoggerFunction · 0.85
PerformRequestFunction · 0.85
AbortWithErrorMethod · 0.80
UseMethod · 0.65
GETMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected