logger_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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?a=100")
  26. assert.Contains(t, buffer.String(), "200")
  27. assert.Contains(t, buffer.String(), "GET")
  28. assert.Contains(t, buffer.String(), "/example")
  29. assert.Contains(t, buffer.String(), "a=100")
  30. // I wrote these first (extending the above) but then realized they are more
  31. // like integration tests because they test the whole logging process rather
  32. // than individual functions. Im not sure where these should go.
  33. buffer.Reset()
  34. performRequest(router, "POST", "/example")
  35. assert.Contains(t, buffer.String(), "200")
  36. assert.Contains(t, buffer.String(), "POST")
  37. assert.Contains(t, buffer.String(), "/example")
  38. buffer.Reset()
  39. performRequest(router, "PUT", "/example")
  40. assert.Contains(t, buffer.String(), "200")
  41. assert.Contains(t, buffer.String(), "PUT")
  42. assert.Contains(t, buffer.String(), "/example")
  43. buffer.Reset()
  44. performRequest(router, "DELETE", "/example")
  45. assert.Contains(t, buffer.String(), "200")
  46. assert.Contains(t, buffer.String(), "DELETE")
  47. assert.Contains(t, buffer.String(), "/example")
  48. buffer.Reset()
  49. performRequest(router, "PATCH", "/example")
  50. assert.Contains(t, buffer.String(), "200")
  51. assert.Contains(t, buffer.String(), "PATCH")
  52. assert.Contains(t, buffer.String(), "/example")
  53. buffer.Reset()
  54. performRequest(router, "HEAD", "/example")
  55. assert.Contains(t, buffer.String(), "200")
  56. assert.Contains(t, buffer.String(), "HEAD")
  57. assert.Contains(t, buffer.String(), "/example")
  58. buffer.Reset()
  59. performRequest(router, "OPTIONS", "/example")
  60. assert.Contains(t, buffer.String(), "200")
  61. assert.Contains(t, buffer.String(), "OPTIONS")
  62. assert.Contains(t, buffer.String(), "/example")
  63. buffer.Reset()
  64. performRequest(router, "GET", "/notfound")
  65. assert.Contains(t, buffer.String(), "404")
  66. assert.Contains(t, buffer.String(), "GET")
  67. assert.Contains(t, buffer.String(), "/notfound")
  68. }
  69. func TestColorForMethod(t *testing.T) {
  70. assert.Equal(t, colorForMethod("GET"), string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), "get should be blue")
  71. assert.Equal(t, colorForMethod("POST"), string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), "post should be cyan")
  72. assert.Equal(t, colorForMethod("PUT"), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "put should be yellow")
  73. assert.Equal(t, colorForMethod("DELETE"), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "delete should be red")
  74. assert.Equal(t, colorForMethod("PATCH"), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "patch should be green")
  75. assert.Equal(t, colorForMethod("HEAD"), string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), "head should be magenta")
  76. assert.Equal(t, colorForMethod("OPTIONS"), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "options should be white")
  77. assert.Equal(t, colorForMethod("TRACE"), string([]byte{27, 91, 48, 109}), "trace is not defined and should be the reset color")
  78. }
  79. func TestColorForStatus(t *testing.T) {
  80. assert.Equal(t, colorForStatus(200), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "2xx should be green")
  81. assert.Equal(t, colorForStatus(301), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "3xx should be white")
  82. assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow")
  83. assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red")
  84. }
  85. func TestErrorLogger(t *testing.T) {
  86. router := New()
  87. router.Use(ErrorLogger())
  88. router.GET("/error", func(c *Context) {
  89. c.Error(errors.New("this is an error"))
  90. })
  91. router.GET("/abort", func(c *Context) {
  92. c.AbortWithError(401, errors.New("no authorized"))
  93. })
  94. router.GET("/print", func(c *Context) {
  95. c.Error(errors.New("this is an error"))
  96. c.String(500, "hola!")
  97. })
  98. w := performRequest(router, "GET", "/error")
  99. assert.Equal(t, 200, w.Code)
  100. assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())
  101. w = performRequest(router, "GET", "/abort")
  102. assert.Equal(t, 401, w.Code)
  103. assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())
  104. w = performRequest(router, "GET", "/print")
  105. assert.Equal(t, 500, w.Code)
  106. assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())
  107. }
  108. func TestSkippingPaths(t *testing.T) {
  109. buffer := new(bytes.Buffer)
  110. router := New()
  111. router.Use(LoggerWithWriter(buffer, "/skipped"))
  112. router.GET("/logged", func(c *Context) {})
  113. router.GET("/skipped", func(c *Context) {})
  114. performRequest(router, "GET", "/logged")
  115. assert.Contains(t, buffer.String(), "200")
  116. buffer.Reset()
  117. performRequest(router, "GET", "/skipped")
  118. assert.Contains(t, buffer.String(), "")
  119. }
  120. func TestDisableConsoleColor(t *testing.T) {
  121. New()
  122. assert.False(t, disableColor)
  123. DisableConsoleColor()
  124. assert.True(t, disableColor)
  125. }