Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "gitea.com/lunny/log"
  11. )
  12. func TestLogger1(t *testing.T) {
  13. buff := bytes.NewBufferString("")
  14. recorder := httptest.NewRecorder()
  15. n := NewWithLog(log.New(buff, "[tango] ", 0))
  16. n.Use(Logging())
  17. n.UseHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  18. rw.WriteHeader(http.StatusNotFound)
  19. }))
  20. req, err := http.NewRequest("GET", "http://localhost:3000/foobar", nil)
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. n.ServeHTTP(recorder, req)
  25. expect(t, recorder.Code, http.StatusNotFound)
  26. refute(t, len(buff.String()), 0)
  27. }
  28. type LoggerAction struct {
  29. Log
  30. }
  31. func (l *LoggerAction) Get() string {
  32. l.Warn("this is a warn")
  33. l.Warnf("This is a %s", "warnf")
  34. l.Error("this is an error")
  35. l.Errorf("This is a %s", "errorf")
  36. l.Infof("This is a %s", "infof")
  37. l.Debugf("This is a %s", "debuf")
  38. return "log"
  39. }
  40. func TestLogger2(t *testing.T) {
  41. buff := bytes.NewBufferString("")
  42. recorder := httptest.NewRecorder()
  43. recorder.Body = buff
  44. n := Classic()
  45. n.Get("/", new(LoggerAction))
  46. req, err := http.NewRequest("GET", "http://localhost:3000/", nil)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. n.ServeHTTP(recorder, req)
  51. expect(t, recorder.Code, http.StatusOK)
  52. refute(t, len(buff.String()), 0)
  53. expect(t, buff.String(), "log")
  54. }
  55. func TestLogger3(t *testing.T) {
  56. buff := bytes.NewBufferString("")
  57. recorder := httptest.NewRecorder()
  58. recorder.Body = buff
  59. logger := NewCompositeLogger(log.Std, log.New(log.NewFileWriter(log.FileOptions{
  60. Dir: "./",
  61. ByType: log.ByDay,
  62. }), "file", log.Ldefault()))
  63. n := Classic(logger)
  64. n.Get("/", new(LoggerAction))
  65. req, err := http.NewRequest("GET", "http://localhost:3000/", nil)
  66. if err != nil {
  67. t.Error(err)
  68. }
  69. n.ServeHTTP(recorder, req)
  70. expect(t, recorder.Code, http.StatusOK)
  71. refute(t, len(buff.String()), 0)
  72. expect(t, buff.String(), "log")
  73. }
  74. type Logger4Action struct {
  75. }
  76. func (l *Logger4Action) Get() {
  77. }
  78. func TestLogger4(t *testing.T) {
  79. buff := bytes.NewBufferString("")
  80. recorder := httptest.NewRecorder()
  81. recorder.Body = buff
  82. n := Classic()
  83. n.Get("/", new(Logger4Action))
  84. req, err := http.NewRequest("GET", "http://localhost:3000/", nil)
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. n.ServeHTTP(recorder, req)
  89. expect(t, recorder.Code, http.StatusOK)
  90. }