logger_test.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. //TODO
  11. // func (engine *Engine) LoadHTMLGlob(pattern string) {
  12. // func (engine *Engine) LoadHTMLFiles(files ...string) {
  13. // func (engine *Engine) Run(addr string) error {
  14. // func (engine *Engine) RunTLS(addr string, cert string, key string) error {
  15. func init() {
  16. SetMode(TestMode)
  17. }
  18. func TestLogger(t *testing.T) {
  19. buffer := new(bytes.Buffer)
  20. router := New()
  21. router.Use(LoggerWithWriter(buffer))
  22. router.GET("/example", func(c *Context) {})
  23. router.POST("/example", func(c *Context) {})
  24. router.PUT("/example", func(c *Context) {})
  25. router.DELETE("/example", func(c *Context) {})
  26. router.PATCH("/example", func(c *Context) {})
  27. router.HEAD("/example", func(c *Context) {})
  28. router.OPTIONS("/example", func(c *Context) {})
  29. performRequest(router, "GET", "/example")
  30. assert.Contains(t, buffer.String(), "200")
  31. assert.Contains(t, buffer.String(), "GET")
  32. assert.Contains(t, buffer.String(), "/example")
  33. // I wrote these first (extending the above) but then realized they are more
  34. // like integration tests because they test the whole logging process rather
  35. // than individual functions. Im not sure where these should go.
  36. performRequest(router, "POST", "/example")
  37. assert.Contains(t, buffer.String(), "200")
  38. assert.Contains(t, buffer.String(), "POST")
  39. assert.Contains(t, buffer.String(), "/example")
  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. 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. 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. performRequest(router, "HEAD", "/example")
  53. assert.Contains(t, buffer.String(), "200")
  54. assert.Contains(t, buffer.String(), "HEAD")
  55. assert.Contains(t, buffer.String(), "/example")
  56. performRequest(router, "OPTIONS", "/example")
  57. assert.Contains(t, buffer.String(), "200")
  58. assert.Contains(t, buffer.String(), "OPTIONS")
  59. assert.Contains(t, buffer.String(), "/example")
  60. performRequest(router, "GET", "/notfound")
  61. assert.Contains(t, buffer.String(), "404")
  62. assert.Contains(t, buffer.String(), "GET")
  63. assert.Contains(t, buffer.String(), "/notfound")
  64. }
  65. func TestColorForMethod(t *testing.T) {
  66. assert.Equal(t, colorForMethod("GET"), string([]byte{27, 91, 57, 55, 59, 52, 52, 109}), "get should be blue")
  67. assert.Equal(t, colorForMethod("POST"), string([]byte{27, 91, 57, 55, 59, 52, 54, 109}), "post should be cyan")
  68. assert.Equal(t, colorForMethod("PUT"), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "put should be yellow")
  69. assert.Equal(t, colorForMethod("DELETE"), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "delete should be red")
  70. assert.Equal(t, colorForMethod("PATCH"), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "patch should be green")
  71. assert.Equal(t, colorForMethod("HEAD"), string([]byte{27, 91, 57, 55, 59, 52, 53, 109}), "head should be magenta")
  72. assert.Equal(t, colorForMethod("OPTIONS"), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "options should be white")
  73. assert.Equal(t, colorForMethod("TRACE"), string([]byte{27, 91, 48, 109}), "trace is not defined and should be the reset color")
  74. }
  75. func TestColorForStatus(t *testing.T) {
  76. assert.Equal(t, colorForStatus(200), string([]byte{27, 91, 57, 55, 59, 52, 50, 109}), "2xx should be green")
  77. assert.Equal(t, colorForStatus(301), string([]byte{27, 91, 57, 48, 59, 52, 55, 109}), "3xx should be white")
  78. assert.Equal(t, colorForStatus(404), string([]byte{27, 91, 57, 55, 59, 52, 51, 109}), "4xx should be yellow")
  79. assert.Equal(t, colorForStatus(2), string([]byte{27, 91, 57, 55, 59, 52, 49, 109}), "other things should be red")
  80. }