logger_test.go 5.3 KB

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