logger_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bytes"
  7. "errors"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func init() {
  12. SetMode(TestMode)
  13. }
  14. func TestLogger(t *testing.T) {
  15. buffer := new(bytes.Buffer)
  16. router := New()
  17. router.Use(LoggerWithWriter(buffer))
  18. router.GET("/example", func(c *Context) {})
  19. router.POST("/example", func(c *Context) {})
  20. router.PUT("/example", func(c *Context) {})
  21. router.DELETE("/example", func(c *Context) {})
  22. router.PATCH("/example", func(c *Context) {})
  23. router.HEAD("/example", func(c *Context) {})
  24. router.OPTIONS("/example", func(c *Context) {})
  25. performRequest(router, "GET", "/example")
  26. assert.Contains(t, buffer.String(), "200")
  27. assert.Contains(t, buffer.String(), "GET")
  28. assert.Contains(t, buffer.String(), "/example")
  29. // I wrote these first (extending the above) but then realized they are more
  30. // like integration tests because they test the whole logging process rather
  31. // than individual functions. Im not sure where these should go.
  32. performRequest(router, "POST", "/example")
  33. assert.Contains(t, buffer.String(), "200")
  34. assert.Contains(t, buffer.String(), "POST")
  35. assert.Contains(t, buffer.String(), "/example")
  36. performRequest(router, "PUT", "/example")
  37. assert.Contains(t, buffer.String(), "200")
  38. assert.Contains(t, buffer.String(), "PUT")
  39. assert.Contains(t, buffer.String(), "/example")
  40. performRequest(router, "DELETE", "/example")
  41. assert.Contains(t, buffer.String(), "200")
  42. assert.Contains(t, buffer.String(), "DELETE")
  43. assert.Contains(t, buffer.String(), "/example")
  44. performRequest(router, "PATCH", "/example")
  45. assert.Contains(t, buffer.String(), "200")
  46. assert.Contains(t, buffer.String(), "PATCH")
  47. assert.Contains(t, buffer.String(), "/example")
  48. performRequest(router, "HEAD", "/example")
  49. assert.Contains(t, buffer.String(), "200")
  50. assert.Contains(t, buffer.String(), "HEAD")
  51. assert.Contains(t, buffer.String(), "/example")
  52. performRequest(router, "OPTIONS", "/example")
  53. assert.Contains(t, buffer.String(), "200")
  54. assert.Contains(t, buffer.String(), "OPTIONS")
  55. assert.Contains(t, buffer.String(), "/example")
  56. performRequest(router, "GET", "/notfound")
  57. assert.Contains(t, buffer.String(), "404")
  58. assert.Contains(t, buffer.String(), "GET")
  59. assert.Contains(t, buffer.String(), "/notfound")
  60. }
  61. func TestColorForMethod(t *testing.T) {
  62. assert.Equal(t, colorForMethod("GET"), string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), "get should be blue")
  63. assert.Equal(t, colorForMethod("POST"), string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), "post should be cyan")
  64. assert.Equal(t, colorForMethod("PUT"), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "put should be yellow")
  65. assert.Equal(t, colorForMethod("DELETE"), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "delete should be red")
  66. assert.Equal(t, colorForMethod("PATCH"), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "patch should be green")
  67. assert.Equal(t, colorForMethod("HEAD"), string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), "head should be magenta")
  68. assert.Equal(t, colorForMethod("OPTIONS"), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "options should be white")
  69. assert.Equal(t, colorForMethod("TRACE"), string([]byte{27, 91, 48, 109}), "trace is not defined and should be the reset color")
  70. }
  71. func TestColorForStatus(t *testing.T) {
  72. assert.Equal(t, colorForStatus(200), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "2xx should be green")
  73. assert.Equal(t, colorForStatus(301), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "3xx should be white")
  74. assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow")
  75. assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red")
  76. }
  77. func TestErrorLogger(t *testing.T) {
  78. router := New()
  79. router.Use(ErrorLogger())
  80. router.GET("/error", func(c *Context) {
  81. c.Error(errors.New("this is an error"))
  82. })
  83. router.GET("/abort", func(c *Context) {
  84. c.AbortWithError(401, errors.New("no authorized"))
  85. })
  86. router.GET("/print", func(c *Context) {
  87. c.Error(errors.New("this is an error"))
  88. c.String(500, "hola!")
  89. })
  90. w := performRequest(router, "GET", "/error")
  91. assert.Equal(t, w.Code, 200)
  92. assert.Equal(t, w.Body.String(), "{\"error\":\"this is an error\"}\n")
  93. w = performRequest(router, "GET", "/abort")
  94. assert.Equal(t, w.Code, 401)
  95. assert.Equal(t, w.Body.String(), "{\"error\":\"no authorized\"}\n")
  96. w = performRequest(router, "GET", "/print")
  97. assert.Equal(t, w.Code, 500)
  98. assert.Equal(t, w.Body.String(), "hola!{\"error\":\"this is an error\"}\n")
  99. }
  100. func TestSkippingPaths(t *testing.T) {
  101. buffer := new(bytes.Buffer)
  102. router := New()
  103. router.Use(LoggerWithWriter(buffer, "/skipped"))
  104. router.GET("/logged", func(c *Context) {})
  105. router.GET("/skipped", func(c *Context) {})
  106. performRequest(router, "GET", "/logged")
  107. assert.Contains(t, buffer.String(), "200")
  108. performRequest(router, "GET", "/skipped")
  109. assert.Contains(t, buffer.String(), "")
  110. }