@@ -39,7 +39,7 @@ func Test_LogMiddleware_GeneratesTracesAndSubSpans(t *testing.T) {
3939 w .WriteHeader (200 )
4040 }))
4141
42- r , _ := http .NewRequest ("GET" , "https://www.example.org/foo" , nil )
42+ r , _ := http .NewRequest (http . MethodGet , "https://www.example.org/foo" , nil )
4343
4444 lm .ServeHTTP (httptest .NewRecorder (), r )
4545
@@ -77,7 +77,7 @@ func Test_LogMiddleware_Panic(t *testing.T) {
7777 i [100 ]++
7878 }))
7979
80- r , _ := http .NewRequest ("GET" , "http://www.example.org/foo" , nil )
80+ r , _ := http .NewRequest (http . MethodGet , "http://www.example.org/foo" , nil )
8181
8282 lm .ServeHTTP (httptest .NewRecorder (), r )
8383
@@ -96,12 +96,12 @@ func Test_LogMiddleware_Log_implicit200(t *testing.T) {
9696 b := bytes .NewBuffer (nil )
9797 Log .Out = b
9898
99- // and a handler which gets an 200er code implicitly
99+ // and a handler which gets a 200er code implicitly
100100 lm := NewLogMiddleware (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
101101 _ , _ = w .Write ([]byte ("hello" ))
102102 }))
103103
104- r , _ := http .NewRequest ("GET" , "http://www.example.org/foo" , nil )
104+ r , _ := http .NewRequest (http . MethodGet , "http://www.example.org/foo" , nil )
105105
106106 lm .ServeHTTP (httptest .NewRecorder (), r )
107107
@@ -112,6 +112,36 @@ func Test_LogMiddleware_Log_implicit200(t *testing.T) {
112112 a .Equal ("info" , data .Level )
113113}
114114
115+ func Test_LogMiddleware_Log_HealthRequestAreNotLogged (t * testing.T ) {
116+ tests := []struct {
117+ name string
118+ method string
119+ url string
120+ containsLogMsg bool
121+ }{
122+ {"health on base path" , http .MethodGet , "http://www.example.org/health" , false },
123+ {"health on sub path" , http .MethodGet , "http://www.example.org/sub/health" , false },
124+ {"post to health" , http .MethodPost , "http://www.example.org/sub/health" , true },
125+ {"health in query param" , http .MethodGet , "http://www.example.org/sub?health" , true },
126+ {"health somewhere in path" , http .MethodGet , "http://www.example.org/health/more" , true },
127+ }
128+ for _ , test := range tests {
129+ t .Run (test .name , func (t * testing.T ) {
130+ logBuffer := bytes .NewBuffer (nil )
131+ Log .Out = logBuffer
132+
133+ lm := NewLogMiddleware (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
134+ _ , _ = w .Write ([]byte ("OK++" ))
135+ }))
136+
137+ r , _ := http .NewRequest (test .method , test .url , nil )
138+
139+ lm .ServeHTTP (httptest .NewRecorder (), r )
140+ assert .Equal (t , test .containsLogMsg , logBuffer .Len () > 0 )
141+ })
142+ }
143+ }
144+
115145func Test_LogMiddleware_Log_404 (t * testing.T ) {
116146 a := assert .New (t )
117147
@@ -124,7 +154,7 @@ func Test_LogMiddleware_Log_404(t *testing.T) {
124154 w .WriteHeader (404 )
125155 }))
126156
127- r , _ := http .NewRequest ("GET" , "http://www.example.org/foo" , nil )
157+ r , _ := http .NewRequest (http . MethodGet , "http://www.example.org/foo" , nil )
128158
129159 lm .ServeHTTP (httptest .NewRecorder (), r )
130160
0 commit comments