Browse Source

Adds unit tests for ErrorLogger()

Manu Mtz-Almeida 10 years ago
parent
commit
c9272120b4
1 changed files with 28 additions and 0 deletions
  1. 28 0
      logger_test.go

+ 28 - 0
logger_test.go

@@ -6,6 +6,7 @@ package gin
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"errors"
 	"testing"
 	"testing"
 
 
 	"github.com/stretchr/testify/assert"
 	"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(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")
 	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!")
+}