Skip to content

Commit

Permalink
refactor: refactor file paths to use constants
Browse files Browse the repository at this point in the history
- Add a constant `testPath` with the value `/test`
- Change the route path from `/test` to use the `testPath` constant
- Update the request creation to use the `testPath` constant
- Update the comparison of the logged path to use the `testPath` constant

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 2, 2024
1 parent 372417f commit d5accec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion zap.go
Expand Up @@ -152,7 +152,7 @@ func CustomRecoveryWithZap(logger ZapLogger, stack bool, recovery gin.RecoveryFu
zap.String("request", string(httpRequest)),
)
// If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck
c.Error(err.(error)) //nolint: errcheck
c.Abort()
return
}
Expand Down
34 changes: 22 additions & 12 deletions zap_test.go
@@ -1,6 +1,7 @@
package ginzap

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -14,6 +15,8 @@ import (
"go.uber.org/zap/zaptest/observer"
)

const testPath = "/test"

func buildDummyLogger() (*zap.Logger, *observer.ObservedLogs) {
core, obs := observer.New(zap.InfoLevel)
logger := zap.New(core)
Expand All @@ -33,6 +36,8 @@ func timestampLocationCheck(timestampStr string, location *time.Location) error
}

func TestGinzap(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := gin.New()

utcLogger, utcLoggerObserved := buildDummyLogger()
Expand All @@ -41,12 +46,12 @@ func TestGinzap(t *testing.T) {
localLogger, localLoggerObserved := buildDummyLogger()
r.Use(Ginzap(localLogger, time.RFC3339, false))

r.GET("/test", func(c *gin.Context) {
r.GET(testPath, func(c *gin.Context) {
c.JSON(204, nil)
})

res1 := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/test", nil)
req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil)
r.ServeHTTP(res1, req1)

if len(utcLoggerObserved.All()) != 1 {
Expand All @@ -55,7 +60,7 @@ func TestGinzap(t *testing.T) {

logLine := utcLoggerObserved.All()[0]
pathStr := logLine.Context[2].String
if pathStr != "/test" {
if pathStr != testPath {
t.Fatalf("logged path should be /test but %s", pathStr)
}

Expand All @@ -70,12 +75,15 @@ func TestGinzap(t *testing.T) {

logLine = localLoggerObserved.All()[0]
pathStr = logLine.Context[2].String
if pathStr != "/test" {
if pathStr != testPath {
t.Fatalf("logged path should be /test but %s", pathStr)
}
}

func TestGinzapWithConfig(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r := gin.New()

utcLogger, utcLoggerObserved := buildDummyLogger()
Expand All @@ -86,7 +94,7 @@ func TestGinzapWithConfig(t *testing.T) {
DefaultLevel: zapcore.WarnLevel,
}))

r.GET("/test", func(c *gin.Context) {
r.GET(testPath, func(c *gin.Context) {
c.JSON(204, nil)
})

Expand All @@ -95,11 +103,11 @@ func TestGinzapWithConfig(t *testing.T) {
})

res1 := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/test", nil)
req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil)
r.ServeHTTP(res1, req1)

res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/no_log", nil)
req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil)
r.ServeHTTP(res2, req2)

if res2.Code != 204 {
Expand All @@ -112,7 +120,7 @@ func TestGinzapWithConfig(t *testing.T) {

logLine := utcLoggerObserved.All()[0]
pathStr := logLine.Context[2].String
if pathStr != "/test" {
if pathStr != testPath {
t.Fatalf("logged path should be /test but %s", pathStr)
}

Expand All @@ -127,6 +135,8 @@ func TestGinzapWithConfig(t *testing.T) {
}

func TestLoggerSkipper(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := gin.New()

utcLogger, utcLoggerObserved := buildDummyLogger()
Expand All @@ -138,7 +148,7 @@ func TestLoggerSkipper(t *testing.T) {
},
}))

r.GET("/test", func(c *gin.Context) {
r.GET(testPath, func(c *gin.Context) {
c.JSON(204, nil)
})

Expand All @@ -147,11 +157,11 @@ func TestLoggerSkipper(t *testing.T) {
})

res1 := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/test", nil)
req1, _ := http.NewRequestWithContext(ctx, "GET", testPath, nil)
r.ServeHTTP(res1, req1)

res2 := httptest.NewRecorder()
req2, _ := http.NewRequest("GET", "/no_log", nil)
req2, _ := http.NewRequestWithContext(ctx, "GET", "/no_log", nil)
r.ServeHTTP(res2, req2)

if res2.Code != 204 {
Expand All @@ -164,7 +174,7 @@ func TestLoggerSkipper(t *testing.T) {

logLine := utcLoggerObserved.All()[0]
pathStr := logLine.Context[2].String
if pathStr != "/test" {
if pathStr != testPath {
t.Fatalf("logged path should be /test but %s", pathStr)
}
}

0 comments on commit d5accec

Please sign in to comment.