log_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package internal
  2. import (
  3. "context"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestInfo(t *testing.T) {
  12. collector := new(LogCollector)
  13. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  14. req = req.WithContext(context.WithValue(req.Context(), LogContext, collector))
  15. Info(req, "first")
  16. Infof(req, "second %s", "third")
  17. val := collector.Flush()
  18. assert.True(t, strings.Contains(val, "first"))
  19. assert.True(t, strings.Contains(val, "second"))
  20. assert.True(t, strings.Contains(val, "third"))
  21. assert.True(t, strings.Contains(val, "\n"))
  22. }
  23. func TestError(t *testing.T) {
  24. var writer strings.Builder
  25. log.SetOutput(&writer)
  26. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  27. Error(req, "first")
  28. Errorf(req, "second %s", "third")
  29. val := writer.String()
  30. assert.True(t, strings.Contains(val, "first"))
  31. assert.True(t, strings.Contains(val, "second"))
  32. assert.True(t, strings.Contains(val, "third"))
  33. assert.True(t, strings.Contains(val, "\n"))
  34. }