debug_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "html/template"
  9. "io"
  10. "log"
  11. "os"
  12. "sync"
  13. "testing"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. // TODO
  17. // func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  18. // func debugPrint(format string, values ...interface{}) {
  19. func TestIsDebugging(t *testing.T) {
  20. SetMode(DebugMode)
  21. assert.True(t, IsDebugging())
  22. SetMode(ReleaseMode)
  23. assert.False(t, IsDebugging())
  24. SetMode(TestMode)
  25. assert.False(t, IsDebugging())
  26. }
  27. func TestDebugPrint(t *testing.T) {
  28. re := captureOutput(func() {
  29. SetMode(DebugMode)
  30. SetMode(ReleaseMode)
  31. debugPrint("DEBUG this!")
  32. SetMode(TestMode)
  33. debugPrint("DEBUG this!")
  34. SetMode(DebugMode)
  35. debugPrint("these are %d %s\n", 2, "error messages")
  36. SetMode(TestMode)
  37. })
  38. assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
  39. }
  40. func TestDebugPrintError(t *testing.T) {
  41. re := captureOutput(func() {
  42. SetMode(DebugMode)
  43. debugPrintError(nil)
  44. debugPrintError(errors.New("this is an error"))
  45. SetMode(TestMode)
  46. })
  47. assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", re)
  48. }
  49. func TestDebugPrintRoutes(t *testing.T) {
  50. re := captureOutput(func() {
  51. SetMode(DebugMode)
  52. debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
  53. SetMode(TestMode)
  54. })
  55. assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)
  56. }
  57. func TestDebugPrintLoadTemplate(t *testing.T) {
  58. re := captureOutput(func() {
  59. SetMode(DebugMode)
  60. templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./testdata/template/hello.tmpl"))
  61. debugPrintLoadTemplate(templ)
  62. SetMode(TestMode)
  63. })
  64. assert.Regexp(t, `^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n){2}\n`, re)
  65. }
  66. func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {
  67. re := captureOutput(func() {
  68. SetMode(DebugMode)
  69. debugPrintWARNINGSetHTMLTemplate()
  70. SetMode(TestMode)
  71. })
  72. assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", re)
  73. }
  74. func TestDebugPrintWARNINGDefault(t *testing.T) {
  75. re := captureOutput(func() {
  76. SetMode(DebugMode)
  77. debugPrintWARNINGDefault()
  78. SetMode(TestMode)
  79. })
  80. assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
  81. }
  82. func TestDebugPrintWARNINGNew(t *testing.T) {
  83. re := captureOutput(func() {
  84. SetMode(DebugMode)
  85. debugPrintWARNINGNew()
  86. SetMode(TestMode)
  87. })
  88. assert.Equal(t, "[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n\n", re)
  89. }
  90. func captureOutput(f func()) string {
  91. reader, writer, err := os.Pipe()
  92. if err != nil {
  93. panic(err)
  94. }
  95. stdout := os.Stdout
  96. stderr := os.Stderr
  97. defer func() {
  98. os.Stdout = stdout
  99. os.Stderr = stderr
  100. log.SetOutput(os.Stderr)
  101. }()
  102. os.Stdout = writer
  103. os.Stderr = writer
  104. log.SetOutput(writer)
  105. out := make(chan string)
  106. wg := new(sync.WaitGroup)
  107. wg.Add(1)
  108. go func() {
  109. var buf bytes.Buffer
  110. wg.Done()
  111. io.Copy(&buf, reader)
  112. out <- buf.String()
  113. }()
  114. wg.Wait()
  115. f()
  116. writer.Close()
  117. return <-out
  118. }