From c9272120b485bf5faf7717c13327962011f586d5 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 4 Jul 2015 20:15:15 +0200 Subject: [PATCH] Adds unit tests for ErrorLogger() --- logger_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/logger_test.go b/logger_test.go index 1cdaa94c..267f9c5b 100644 --- a/logger_test.go +++ b/logger_test.go @@ -6,6 +6,7 @@ package gin import ( "bytes" + "errors" "testing" "github.com/stretchr/testify/assert" @@ -96,3 +97,30 @@ func TestColorForStatus(t *testing.T) { assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow") assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red") } + +func TestErrorLogger(t *testing.T) { + router := New() + router.Use(ErrorLogger()) + router.GET("/error", func(c *Context) { + c.Error(errors.New("this is an error")) + }) + router.GET("/abort", func(c *Context) { + c.AbortWithError(401, errors.New("no authorized")) + }) + router.GET("/print", func(c *Context) { + c.Error(errors.New("this is an error")) + c.String(500, "hola!") + }) + + w := performRequest(router, "GET", "/error") + assert.Equal(t, w.Code, 200) + assert.Equal(t, w.Body.String(), "{\"error\":\"this is an error\"}\n") + + w = performRequest(router, "GET", "/abort") + assert.Equal(t, w.Code, 401) + assert.Equal(t, w.Body.String(), "{\"error\":\"no authorized\"}\n") + + w = performRequest(router, "GET", "/print") + assert.Equal(t, w.Code, 500) + assert.Equal(t, w.Body.String(), "hola!") +}