|
7 | 7 | "net/http"
|
8 | 8 | "net/http/httptest"
|
9 | 9 | "net/url"
|
| 10 | + "strconv" |
10 | 11 | "strings"
|
11 | 12 | "testing"
|
12 | 13 | "time"
|
@@ -244,3 +245,49 @@ func BenchmarkLoggerWithConfig_withMapFields(b *testing.B) {
|
244 | 245 | buf.Reset()
|
245 | 246 | }
|
246 | 247 | }
|
| 248 | + |
| 249 | +func TestLoggerTemplateWithTimeUnixMilli(t *testing.T) { |
| 250 | + buf := new(bytes.Buffer) |
| 251 | + |
| 252 | + e := echo.New() |
| 253 | + e.Use(LoggerWithConfig(LoggerConfig{ |
| 254 | + Format: `${time_unix_milli}`, |
| 255 | + Output: buf, |
| 256 | + })) |
| 257 | + |
| 258 | + e.GET("/", func(c echo.Context) error { |
| 259 | + return c.String(http.StatusOK, "OK") |
| 260 | + }) |
| 261 | + |
| 262 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 263 | + |
| 264 | + rec := httptest.NewRecorder() |
| 265 | + e.ServeHTTP(rec, req) |
| 266 | + |
| 267 | + unixMillis, err := strconv.ParseInt(buf.String(), 10, 64) |
| 268 | + assert.NoError(t, err) |
| 269 | + assert.WithinDuration(t, time.Unix(unixMillis/1000, 0), time.Now(), 3*time.Second) |
| 270 | +} |
| 271 | + |
| 272 | +func TestLoggerTemplateWithTimeUnixMicro(t *testing.T) { |
| 273 | + buf := new(bytes.Buffer) |
| 274 | + |
| 275 | + e := echo.New() |
| 276 | + e.Use(LoggerWithConfig(LoggerConfig{ |
| 277 | + Format: `${time_unix_micro}`, |
| 278 | + Output: buf, |
| 279 | + })) |
| 280 | + |
| 281 | + e.GET("/", func(c echo.Context) error { |
| 282 | + return c.String(http.StatusOK, "OK") |
| 283 | + }) |
| 284 | + |
| 285 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 286 | + |
| 287 | + rec := httptest.NewRecorder() |
| 288 | + e.ServeHTTP(rec, req) |
| 289 | + |
| 290 | + unixMicros, err := strconv.ParseInt(buf.String(), 10, 64) |
| 291 | + assert.NoError(t, err) |
| 292 | + assert.WithinDuration(t, time.Unix(unixMicros/1000000, 0), time.Now(), 3*time.Second) |
| 293 | +} |
0 commit comments