-
Notifications
You must be signed in to change notification settings - Fork 13
/
context_test.go
131 lines (109 loc) · 3.26 KB
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package logging
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func TestGinContext(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
NewCtxLogger(c, CloneLogger("test"), "1234")
_, exists := c.Get(string(CtxLoggerName))
if !exists {
t.Fatal("set ctxLogger failed")
}
if tid := CtxTraceID(c); tid != "1234" {
t.Fatal("invalid tid", tid)
}
}
func TestContext(t *testing.T) {
c := context.Background()
c, _ = NewCtxLogger(c, CloneLogger("test"), "1234")
if tid := CtxTraceID(c); tid != "1234" {
t.Fatal("invalid tid", c, tid)
}
}
func TestGinCtxLoggerEmpty(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "/", nil)
logger := CtxLogger(c)
if logger == nil {
t.Fatal("empty context also must should return a logger")
}
logger.Info("this is a logger from empty ctx")
}
func TestCtxLoggerEmpty(t *testing.T) {
c := context.Background()
logger := CtxLogger(c)
if logger == nil {
t.Fatal("empty context also must should return a logger")
}
logger.Info("this is a logger from empty ctx")
}
func TestCtxLoggerEmptyField(t *testing.T) {
c := context.Background()
logger := CtxLogger(c, zap.String("field1", "1"))
if logger == nil {
t.Fatal("empty context also must should return a logger")
}
logger.Info("this is a logger from empty ctx but with field")
}
func TestGinCtxLoggerDefaultLogger(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
NewCtxLogger(c, CloneLogger("test"), "rid")
logger := CtxLogger(c)
if logger == nil {
t.Fatal("context also must should return a logger")
}
logger.Info("this is a logger from default logger")
}
func TestGinCtxLoggerDefaultLoggerWithField(t *testing.T) {
ginctx, _ := gin.CreateTestContext(httptest.NewRecorder())
NewCtxLogger(ginctx, CloneLogger("test"), "rid")
ginCtxlogger := CtxLogger(ginctx, zap.String("myfield", "xxx"))
if ginCtxlogger == nil {
t.Fatal("gin context logger is nil")
}
ginCtxlogger.Info("this is a logger from default logger with field")
}
func TestCtxLoggerDefaultLogger(t *testing.T) {
c := context.Background()
NewCtxLogger(c, CloneLogger("test"), "rid")
logger := CtxLogger(c)
if logger == nil {
t.Fatal("context also must should return a logger")
}
}
func TestCtxLoggerDefaultLoggerWithField(t *testing.T) {
c := context.Background()
NewCtxLogger(c, CtxLogger(c), "rid-xx")
ctxlogger := CtxLogger(c, zap.String("field", "xx"))
if ctxlogger == nil {
t.Fatal("context logger is nil")
}
ctxlogger.Info("this is a logger from default logger with field")
}
func TestGinCtxTraceID(t *testing.T) {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "/", nil)
if CtxTraceID(c) == "" {
t.Fatal("context should return default value")
}
c.Set(string(TraceIDKeyname), "IAMAREQUESTID")
if CtxTraceID(c) != "IAMAREQUESTID" {
t.Fatal("context should return set value")
}
}
func TestCtxTraceID(t *testing.T) {
c := context.Background()
if CtxTraceID(c) == "" {
t.Fatal("context should return default value")
}
c = context.WithValue(c, TraceIDKeyname, "IAMAREQUESTID")
if CtxTraceID(c) != "IAMAREQUESTID" {
t.Fatal("context should return set value")
}
}