logger_test.go 5.5 KB

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