@@ -88,6 +88,96 @@ func TestLogHandlerSlow(t *testing.T) {
8888 }
8989}
9090
91+ func TestLogHandlerSSE (t * testing.T ) {
92+ handlers := []func (handler http.Handler ) http.Handler {
93+ LogHandler ,
94+ DetailedLogHandler ,
95+ }
96+
97+ for _ , logHandler := range handlers {
98+ t .Run ("SSE request with normal duration" , func (t * testing.T ) {
99+ req := httptest .NewRequest (http .MethodGet , "http://localhost" , http .NoBody )
100+ req .Header .Set (headerAccept , valueSSE )
101+
102+ handler := logHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
103+ time .Sleep (defaultSlowThreshold + time .Second )
104+ w .WriteHeader (http .StatusOK )
105+ }))
106+
107+ resp := httptest .NewRecorder ()
108+ handler .ServeHTTP (resp , req )
109+ assert .Equal (t , http .StatusOK , resp .Code )
110+ })
111+
112+ t .Run ("SSE request exceeding SSE threshold" , func (t * testing.T ) {
113+ originalThreshold := sseSlowThreshold .Load ()
114+ SetSSESlowThreshold (time .Millisecond * 100 )
115+ defer SetSSESlowThreshold (originalThreshold )
116+
117+ req := httptest .NewRequest (http .MethodGet , "http://localhost" , http .NoBody )
118+ req .Header .Set (headerAccept , valueSSE )
119+
120+ handler := logHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
121+ time .Sleep (time .Millisecond * 150 )
122+ w .WriteHeader (http .StatusOK )
123+ }))
124+
125+ resp := httptest .NewRecorder ()
126+ handler .ServeHTTP (resp , req )
127+ assert .Equal (t , http .StatusOK , resp .Code )
128+ })
129+ }
130+ }
131+
132+ func TestLogHandlerThresholdSelection (t * testing.T ) {
133+ tests := []struct {
134+ name string
135+ acceptHeader string
136+ expectedIsSSE bool
137+ }{
138+ {
139+ name : "Regular HTTP request" ,
140+ acceptHeader : "text/html" ,
141+ expectedIsSSE : false ,
142+ },
143+ {
144+ name : "SSE request" ,
145+ acceptHeader : valueSSE ,
146+ expectedIsSSE : true ,
147+ },
148+ {
149+ name : "No Accept header" ,
150+ acceptHeader : "" ,
151+ expectedIsSSE : false ,
152+ },
153+ }
154+
155+ for _ , tt := range tests {
156+ t .Run (tt .name , func (t * testing.T ) {
157+ req := httptest .NewRequest (http .MethodGet , "http://localhost" , http .NoBody )
158+ if tt .acceptHeader != "" {
159+ req .Header .Set (headerAccept , tt .acceptHeader )
160+ }
161+
162+ SetSlowThreshold (time .Millisecond * 100 )
163+ SetSSESlowThreshold (time .Millisecond * 200 )
164+ defer func () {
165+ SetSlowThreshold (defaultSlowThreshold )
166+ SetSSESlowThreshold (defaultSSESlowThreshold )
167+ }()
168+
169+ handler := LogHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
170+ time .Sleep (time .Millisecond * 150 )
171+ w .WriteHeader (http .StatusOK )
172+ }))
173+
174+ resp := httptest .NewRecorder ()
175+ handler .ServeHTTP (resp , req )
176+ assert .Equal (t , http .StatusOK , resp .Code )
177+ })
178+ }
179+ }
180+
91181func TestDetailedLogHandler_LargeBody (t * testing.T ) {
92182 lbuf := logtest .NewCollector (t )
93183
@@ -139,6 +229,12 @@ func TestSetSlowThreshold(t *testing.T) {
139229 assert .Equal (t , time .Second , slowThreshold .Load ())
140230}
141231
232+ func TestSetSSESlowThreshold (t * testing.T ) {
233+ assert .Equal (t , defaultSSESlowThreshold , sseSlowThreshold .Load ())
234+ SetSSESlowThreshold (time .Minute * 10 )
235+ assert .Equal (t , time .Minute * 10 , sseSlowThreshold .Load ())
236+ }
237+
142238func TestWrapMethodWithColor (t * testing.T ) {
143239 // no tty
144240 assert .Equal (t , http .MethodGet , wrapMethod (http .MethodGet ))
0 commit comments