debug_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. // TODO
  16. // func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
  17. // func debugPrint(format string, values ...interface{}) {
  18. func TestIsDebugging(t *testing.T) {
  19. SetMode(DebugMode)
  20. assert.True(t, IsDebugging())
  21. SetMode(ReleaseMode)
  22. assert.False(t, IsDebugging())
  23. SetMode(TestMode)
  24. assert.False(t, IsDebugging())
  25. }
  26. func TestDebugPrint(t *testing.T) {
  27. var w bytes.Buffer
  28. setup(&w)
  29. defer teardown()
  30. SetMode(ReleaseMode)
  31. debugPrint("DEBUG this!")
  32. SetMode(TestMode)
  33. debugPrint("DEBUG this!")
  34. assert.Empty(t, w.String())
  35. SetMode(DebugMode)
  36. debugPrint("these are %d %s\n", 2, "error messages")
  37. assert.Equal(t, w.String(), "[GIN-debug] these are 2 error messages\n")
  38. }
  39. func TestDebugPrintError(t *testing.T) {
  40. var w bytes.Buffer
  41. setup(&w)
  42. defer teardown()
  43. SetMode(DebugMode)
  44. debugPrintError(nil)
  45. assert.Empty(t, w.String())
  46. debugPrintError(errors.New("this is an error"))
  47. assert.Equal(t, w.String(), "[GIN-debug] [ERROR] this is an error\n")
  48. }
  49. func TestDebugPrintRoutes(t *testing.T) {
  50. var w bytes.Buffer
  51. setup(&w)
  52. defer teardown()
  53. debugPrintRoute("GET", "/path/to/route/:param", HandlersChain{func(c *Context) {}, handlerNameTest})
  54. assert.Regexp(t, `^\[GIN-debug\] GET /path/to/route/:param --> (.*/vendor/)?github.com/gin-gonic/gin.handlerNameTest \(2 handlers\)\n$`, w.String())
  55. }
  56. func TestDebugPrintLoadTemplate(t *testing.T) {
  57. var w bytes.Buffer
  58. setup(&w)
  59. defer teardown()
  60. templ := template.Must(template.New("").Delims("{[{", "}]}").ParseGlob("./fixtures/basic/hello.tmpl"))
  61. debugPrintLoadTemplate(templ)
  62. assert.Equal(t, w.String(), "[GIN-debug] Loaded HTML Templates (2): \n\t- \n\t- hello.tmpl\n\n")
  63. }
  64. func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {
  65. var w bytes.Buffer
  66. setup(&w)
  67. defer teardown()
  68. debugPrintWARNINGSetHTMLTemplate()
  69. assert.Equal(t, w.String(), "[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")
  70. }
  71. func setup(w io.Writer) {
  72. SetMode(DebugMode)
  73. log.SetOutput(w)
  74. }
  75. func teardown() {
  76. SetMode(TestMode)
  77. log.SetOutput(os.Stdout)
  78. }