|
5 | 5 | "fmt"
|
6 | 6 | "net/http"
|
7 | 7 | "net/http/httptest"
|
| 8 | + "regexp" |
8 | 9 | "testing"
|
9 | 10 | "time"
|
10 | 11 |
|
@@ -178,3 +179,48 @@ func TestLoggerSkipper(t *testing.T) {
|
178 | 179 | t.Fatalf("logged path should be /test but %s", pathStr)
|
179 | 180 | }
|
180 | 181 | }
|
| 182 | + |
| 183 | +func TestSkipPathRegexps(t *testing.T) { |
| 184 | + ctx, cancel := context.WithCancel(context.Background()) |
| 185 | + defer cancel() |
| 186 | + r := gin.New() |
| 187 | + |
| 188 | + rxURL := regexp.MustCompile(`^/no_\s*`) |
| 189 | + |
| 190 | + utcLogger, utcLoggerObserved := buildDummyLogger() |
| 191 | + r.Use(GinzapWithConfig(utcLogger, &Config{ |
| 192 | + TimeFormat: time.RFC3339, |
| 193 | + UTC: true, |
| 194 | + SkipPathRegexps: []*regexp.Regexp{rxURL}, |
| 195 | + })) |
| 196 | + |
| 197 | + r.GET(testPath, func(c *gin.Context) { |
| 198 | + c.JSON(204, nil) |
| 199 | + }) |
| 200 | + |
| 201 | + r.GET("/no_log", func(c *gin.Context) { |
| 202 | + c.JSON(204, nil) |
| 203 | + }) |
| 204 | + |
| 205 | + res1 := httptest.NewRecorder() |
| 206 | + req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil) |
| 207 | + r.ServeHTTP(res1, req1) |
| 208 | + |
| 209 | + res2 := httptest.NewRecorder() |
| 210 | + req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil) |
| 211 | + r.ServeHTTP(res2, req2) |
| 212 | + |
| 213 | + if res2.Code != 204 { |
| 214 | + t.Fatalf("request /no_log is failed (%d)", res2.Code) |
| 215 | + } |
| 216 | + |
| 217 | + if len(utcLoggerObserved.All()) != 1 { |
| 218 | + t.Fatalf("Log should be 1 line but there're %d", len(utcLoggerObserved.All())) |
| 219 | + } |
| 220 | + |
| 221 | + logLine := utcLoggerObserved.All()[0] |
| 222 | + pathStr := logLine.Context[2].String |
| 223 | + if pathStr != testPath { |
| 224 | + t.Fatalf("logged path should be /test but %s", pathStr) |
| 225 | + } |
| 226 | +} |
0 commit comments