logger_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. //TODO
  12. // func (engine *Engine) LoadHTMLGlob(pattern string) {
  13. // func (engine *Engine) LoadHTMLFiles(files ...string) {
  14. // func (engine *Engine) Run(addr string) error {
  15. // func (engine *Engine) RunTLS(addr string, cert string, key string) error {
  16. func init() {
  17. SetMode(TestMode)
  18. }
  19. func TestLogger(t *testing.T) {
  20. buffer := new(bytes.Buffer)
  21. router := New()
  22. router.Use(LoggerWithWriter(buffer))
  23. router.GET("/example", func(c *Context) {})
  24. router.POST("/example", func(c *Context) {})
  25. router.PUT("/example", func(c *Context) {})
  26. router.DELETE("/example", func(c *Context) {})
  27. router.PATCH("/example", func(c *Context) {})
  28. router.HEAD("/example", func(c *Context) {})
  29. router.OPTIONS("/example", func(c *Context) {})
  30. performRequest(router, "GET", "/example")
  31. assert.Contains(t, buffer.String(), "200")
  32. assert.Contains(t, buffer.String(), "GET")
  33. assert.Contains(t, buffer.String(), "/example")
  34. // I wrote these first (extending the above) but then realized they are more
  35. // like integration tests because they test the whole logging process rather
  36. // than individual functions. Im not sure where these should go.
  37. performRequest(router, "POST", "/example")
  38. assert.Contains(t, buffer.String(), "200")
  39. assert.Contains(t, buffer.String(), "POST")
  40. assert.Contains(t, buffer.String(), "/example")
  41. performRequest(router, "PUT", "/example")
  42. assert.Contains(t, buffer.String(), "200")
  43. assert.Contains(t, buffer.String(), "PUT")
  44. assert.Contains(t, buffer.String(), "/example")
  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. 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. 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. performRequest(router, "OPTIONS", "/example")
  58. assert.Contains(t, buffer.String(), "200")
  59. assert.Contains(t, buffer.String(), "OPTIONS")
  60. assert.Contains(t, buffer.String(), "/example")
  61. performRequest(router, "GET", "/notfound")
  62. assert.Contains(t, buffer.String(), "404")
  63. assert.Contains(t, buffer.String(), "GET")
  64. assert.Contains(t, buffer.String(), "/notfound")
  65. }
  66. func TestColorForMethod(t *testing.T) {
  67. assert.Equal(t, colorForMethod("GET"), string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), "get should be blue")
  68. assert.Equal(t, colorForMethod("POST"), string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), "post should be cyan")
  69. assert.Equal(t, colorForMethod("PUT"), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "put should be yellow")
  70. assert.Equal(t, colorForMethod("DELETE"), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "delete should be red")
  71. assert.Equal(t, colorForMethod("PATCH"), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "patch should be green")
  72. assert.Equal(t, colorForMethod("HEAD"), string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), "head should be magenta")
  73. assert.Equal(t, colorForMethod("OPTIONS"), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "options should be white")
  74. assert.Equal(t, colorForMethod("TRACE"), string([]byte{27, 91, 48, 109}), "trace is not defined and should be the reset color")
  75. }
  76. func TestColorForStatus(t *testing.T) {
  77. assert.Equal(t, colorForStatus(200), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "2xx should be green")
  78. assert.Equal(t, colorForStatus(301), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "3xx should be white")
  79. assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow")
  80. assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red")
  81. }
  82. func TestErrorLogger(t *testing.T) {
  83. router := New()
  84. router.Use(ErrorLogger())
  85. router.GET("/error", func(c *Context) {
  86. c.Error(errors.New("this is an error"))
  87. })
  88. router.GET("/abort", func(c *Context) {
  89. c.AbortWithError(401, errors.New("no authorized"))
  90. })
  91. router.GET("/print", func(c *Context) {
  92. c.Error(errors.New("this is an error"))
  93. c.String(500, "hola!")
  94. })
  95. w := performRequest(router, "GET", "/error")
  96. assert.Equal(t, w.Code, 200)
  97. assert.Equal(t, w.Body.String(), "{\"error\":\"this is an error\"}\n")
  98. w = performRequest(router, "GET", "/abort")
  99. assert.Equal(t, w.Code, 401)
  100. assert.Equal(t, w.Body.String(), "{\"error\":\"no authorized\"}\n")
  101. w = performRequest(router, "GET", "/print")
  102. assert.Equal(t, w.Code, 500)
  103. assert.Equal(t, w.Body.String(), "hola!")
  104. }