github.com/msales/pkg/v3@v3.24.0/httpx/middleware/stats_test.go (about) 1 package middleware_test 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 "time" 9 10 "github.com/msales/pkg/v3/httpx/middleware" 11 "github.com/msales/pkg/v3/stats" 12 "github.com/stretchr/testify/mock" 13 ) 14 15 func TestWithRequestStats(t *testing.T) { 16 tests := []struct { 17 path string 18 tagFuncs []middleware.TagsFunc 19 expectedTags []interface{} 20 }{ 21 {"/test", nil, []interface{}{"method", "GET", "path", "/test"}}, 22 {"", nil, []interface{}{"method", "GET", "path", ""}}, 23 {"/test", []middleware.TagsFunc{testTags}, []interface{}{"method", "GET"}}, 24 {"", []middleware.TagsFunc{testTags}, []interface{}{"method", "GET"}}, 25 } 26 27 for _, tt := range tests { 28 s := new(MockStats) 29 s.On("Inc", "request.start", int64(1), float32(1.0), tt.expectedTags).Return(nil).Once() 30 s.On("Inc", "request.complete", int64(1), float32(1.0), append([]interface{}{ 31 "status", "0", 32 }, tt.expectedTags...)).Return(nil).Once() 33 34 m := middleware.WithRequestStats(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), tt.tagFuncs...) 35 36 resp := httptest.NewRecorder() 37 req, _ := http.NewRequest("GET", tt.path, nil) 38 39 ctx := stats.WithStats(context.Background(), s) 40 m.ServeHTTP(resp, req.WithContext(ctx)) 41 42 s.AssertExpectations(t) 43 } 44 } 45 46 func TestWithRequestStats_NoStats(t *testing.T) { 47 m := middleware.WithRequestStats(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) 48 49 resp := httptest.NewRecorder() 50 req, _ := http.NewRequest("GET", "/", nil) 51 52 m.ServeHTTP(resp, req.WithContext(context.Background())) 53 } 54 55 func TestWithResponseTime(t *testing.T) { 56 tests := []struct { 57 tagFuncs []middleware.TagsFunc 58 expectedTags []interface{} 59 }{ 60 { 61 tagFuncs: nil, 62 expectedTags: []interface{}{"method", "GET", "path", "/"}, 63 }, 64 { 65 tagFuncs: []middleware.TagsFunc{testTags}, 66 expectedTags: []interface{}{"method", "GET"}, 67 }, 68 } 69 70 for _, tt := range tests { 71 s := new(MockStats) 72 s.On("Timing", "response.time", mock.Anything, float32(1.0), tt.expectedTags).Return(nil).Once() 73 74 m := middleware.WithResponseTime(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), tt.tagFuncs...) 75 76 resp := httptest.NewRecorder() 77 req, _ := http.NewRequest("GET", "/", nil) 78 79 ctx := stats.WithStats(context.Background(), s) 80 m.ServeHTTP(resp, req.WithContext(ctx)) 81 82 s.AssertExpectations(t) 83 } 84 } 85 86 func testTags(r *http.Request) []interface{} { 87 return []interface{}{ 88 "method", r.Method, 89 } 90 } 91 92 type MockStats struct { 93 mock.Mock 94 } 95 96 func (m *MockStats) Inc(name string, value int64, rate float32, tags ...interface{}) error { 97 args := m.Called(name, value, rate, tags) 98 return args.Error(0) 99 } 100 101 func (m *MockStats) Dec(name string, value int64, rate float32, tags ...interface{}) error { 102 args := m.Called(name, value, rate, tags) 103 return args.Error(0) 104 } 105 106 func (m *MockStats) Gauge(name string, value float64, rate float32, tags ...interface{}) error { 107 args := m.Called(name, value, rate, tags) 108 return args.Error(0) 109 } 110 111 func (m *MockStats) Timing(name string, value time.Duration, rate float32, tags ...interface{}) error { 112 args := m.Called(name, value, rate, tags) 113 return args.Error(0) 114 } 115 116 func (m *MockStats) Close() error { 117 args := m.Called() 118 return args.Error(0) 119 }