debug_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "runtime"
  13. "sync"
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. // TODO
  18. // func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  19. // func debugPrint(format string, values ...interface{}) {
  20. func TestIsDebugging(t *testing.T) {
  21. SetMode(DebugMode)
  22. assert.True(t, IsDebugging())
  23. SetMode(ReleaseMode)
  24. assert.False(t, IsDebugging())
  25. SetMode(TestMode)
  26. assert.False(t, IsDebugging())
  27. }
  28. func TestDebugPrint(t *testing.T) {
  29. re := captureOutput(t, func() {
  30. SetMode(DebugMode)
  31. SetMode(ReleaseMode)
  32. debugPrint("DEBUG this!")
  33. SetMode(TestMode)
  34. debugPrint("DEBUG this!")
  35. SetMode(DebugMode)
  36. debugPrint("these are %d %s", 2, "error messages")
  37. SetMode(TestMode)
  38. })
  39. assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
  40. }
  41. func TestDebugPrintError(t *testing.T) {
  42. re := captureOutput(t, func() {
  43. SetMode(DebugMode)
  44. debugPrintError(nil)
  45. debugPrintError(errors.New("this is an error"))
  46. SetMode(TestMode)
  47. })
  48. assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", re)
  49. }
  50. func TestDebugPrintRoutes(t *testing.T) {
  51. re := captureOutput(t, func() {
  52. SetMode(DebugMode)
  53. debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
  54. SetMode(TestMode)
  55. })
  56. assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, re)
  57. }
  58. func TestDebugPrintLoadTemplate(t *testing.T) {
  59. re := captureOutput(t, func() {
  60. SetMode(DebugMode)
  61. templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./testdata/template/hello.tmpl"))
  62. debugPrintLoadTemplate(templ)
  63. SetMode(TestMode)
  64. })
  65. assert.Regexp(t, `^\[GIN-debug\] Loaded HTML Templates \(2\): \n(\t- \n|\t- hello\.tmpl\n){2}\n`, re)
  66. }
  67. func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {
  68. re := captureOutput(t, func() {
  69. SetMode(DebugMode)
  70. debugPrintWARNINGSetHTMLTemplate()
  71. SetMode(TestMode)
  72. })
  73. 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)
  74. }
  75. func TestDebugPrintWARNINGDefault(t *testing.T) {
  76. re := captureOutput(t, func() {
  77. SetMode(DebugMode)
  78. debugPrintWARNINGDefault()
  79. SetMode(TestMode)
  80. })
  81. m, e := getMinVer(runtime.Version())
  82. if e == nil && m <= ginSupportMinGoVer {
  83. assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.10 or later and Go 1.11 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
  84. } else {
  85. assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
  86. }
  87. }
  88. func TestDebugPrintWARNINGNew(t *testing.T) {
  89. re := captureOutput(t, func() {
  90. SetMode(DebugMode)
  91. debugPrintWARNINGNew()
  92. SetMode(TestMode)
  93. })
  94. 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)
  95. }
  96. func captureOutput(t *testing.T, f func()) string {
  97. reader, writer, err := os.Pipe()
  98. if err != nil {
  99. panic(err)
  100. }
  101. defaultWriter := DefaultWriter
  102. defaultErrorWriter := DefaultErrorWriter
  103. defer func() {
  104. DefaultWriter = defaultWriter
  105. DefaultErrorWriter = defaultErrorWriter
  106. log.SetOutput(os.Stderr)
  107. }()
  108. DefaultWriter = writer
  109. DefaultErrorWriter = writer
  110. log.SetOutput(writer)
  111. out := make(chan string)
  112. wg := new(sync.WaitGroup)
  113. wg.Add(1)
  114. go func() {
  115. var buf bytes.Buffer
  116. wg.Done()
  117. _, err := io.Copy(&buf, reader)
  118. assert.NoError(t, err)
  119. out <- buf.String()
  120. }()
  121. wg.Wait()
  122. f()
  123. writer.Close()
  124. return <-out
  125. }
  126. func TestGetMinVer(t *testing.T) {
  127. var m uint64
  128. var e error
  129. _, e = getMinVer("go1")
  130. assert.NotNil(t, e)
  131. m, e = getMinVer("go1.1")
  132. assert.Equal(t, uint64(1), m)
  133. assert.Nil(t, e)
  134. m, e = getMinVer("go1.1.1")
  135. assert.Nil(t, e)
  136. assert.Equal(t, uint64(1), m)
  137. _, e = getMinVer("go1.1.1.1")
  138. assert.NotNil(t, e)
  139. }