loghandler_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package handler
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "time"
  9. "git.i2edu.net/i2/go-zero/rest/internal"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func init() {
  13. log.SetOutput(ioutil.Discard)
  14. }
  15. func TestLogHandler(t *testing.T) {
  16. handlers := []func(handler http.Handler) http.Handler{
  17. LogHandler,
  18. DetailedLogHandler,
  19. }
  20. for _, logHandler := range handlers {
  21. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  22. handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  23. r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
  24. w.Header().Set("X-Test", "test")
  25. w.WriteHeader(http.StatusServiceUnavailable)
  26. _, err := w.Write([]byte("content"))
  27. assert.Nil(t, err)
  28. flusher, ok := w.(http.Flusher)
  29. assert.True(t, ok)
  30. flusher.Flush()
  31. }))
  32. resp := httptest.NewRecorder()
  33. handler.ServeHTTP(resp, req)
  34. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  35. assert.Equal(t, "test", resp.Header().Get("X-Test"))
  36. assert.Equal(t, "content", resp.Body.String())
  37. }
  38. }
  39. func TestLogHandlerSlow(t *testing.T) {
  40. handlers := []func(handler http.Handler) http.Handler{
  41. LogHandler,
  42. DetailedLogHandler,
  43. }
  44. for _, logHandler := range handlers {
  45. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  46. handler := logHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  47. time.Sleep(slowThreshold + time.Millisecond*50)
  48. }))
  49. resp := httptest.NewRecorder()
  50. handler.ServeHTTP(resp, req)
  51. assert.Equal(t, http.StatusOK, resp.Code)
  52. }
  53. }
  54. func TestLogHandler_Hijack(t *testing.T) {
  55. resp := httptest.NewRecorder()
  56. writer := &loggedResponseWriter{
  57. w: resp,
  58. }
  59. assert.NotPanics(t, func() {
  60. writer.Hijack()
  61. })
  62. writer = &loggedResponseWriter{
  63. w: mockedHijackable{resp},
  64. }
  65. assert.NotPanics(t, func() {
  66. writer.Hijack()
  67. })
  68. }
  69. func TestDetailedLogHandler_Hijack(t *testing.T) {
  70. resp := httptest.NewRecorder()
  71. writer := &detailLoggedResponseWriter{
  72. writer: &loggedResponseWriter{
  73. w: resp,
  74. },
  75. }
  76. assert.NotPanics(t, func() {
  77. writer.Hijack()
  78. })
  79. writer = &detailLoggedResponseWriter{
  80. writer: &loggedResponseWriter{
  81. w: mockedHijackable{resp},
  82. },
  83. }
  84. assert.NotPanics(t, func() {
  85. writer.Hijack()
  86. })
  87. }
  88. func BenchmarkLogHandler(b *testing.B) {
  89. b.ReportAllocs()
  90. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  91. handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  92. w.WriteHeader(http.StatusOK)
  93. }))
  94. for i := 0; i < b.N; i++ {
  95. resp := httptest.NewRecorder()
  96. handler.ServeHTTP(resp, req)
  97. }
  98. }