forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_helpers_test.go
134 lines (130 loc) · 3.73 KB
/
log_helpers_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
132
133
134
package main
import (
"net/http/httptest"
"testing"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/config"
)
func TestGetLogEntryForRequest(t *testing.T) {
testReq := httptest.NewRequest("GET", "http://tyk.io/test", nil)
testReq.RemoteAddr = "127.0.0.1:80"
testData := []struct {
EnableKeyLogging bool
Key string
Data map[string]interface{}
Result *logrus.Entry
}{
// enable_key_logging is set, key passed, no additional data fields
{
EnableKeyLogging: true,
Key: "abc",
Data: nil,
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"key": "abc",
}),
},
// enable_key_logging is set, key is not passed, no additional data fields
{
EnableKeyLogging: true,
Key: "",
Data: nil,
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
}),
},
// enable_key_logging is set, key passed, additional data fields are passed
{
EnableKeyLogging: true,
Key: "abc",
Data: map[string]interface{}{"a": 1, "b": "test"},
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"key": "abc",
"a": 1,
"b": "test",
}),
},
// enable_key_logging is set, key is not passed, additional data fields are passed
{
EnableKeyLogging: true,
Key: "",
Data: map[string]interface{}{"a": 1, "b": "test"},
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"a": 1,
"b": "test",
}),
},
// enable_key_logging is not set, key passed, no additional data field
{
EnableKeyLogging: false,
Key: "abc",
Data: nil,
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"key": logHiddenValue,
}),
},
// enable_key_logging is not set, key is not passed, no additional data field
{
EnableKeyLogging: false,
Key: "",
Data: nil,
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
}),
},
// enable_key_logging is not set, key passed, additional data fields are passed
{
EnableKeyLogging: false,
Key: "abc",
Data: map[string]interface{}{"a": 1, "b": "test"},
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"a": 1,
"b": "test",
"key": logHiddenValue,
}),
},
// enable_key_logging is not set, key is not passed, additional data fields are passed
{
EnableKeyLogging: false,
Key: "",
Data: map[string]interface{}{"a": 1, "b": "test"},
Result: logrus.WithFields(logrus.Fields{
"path": "/test",
"origin": "127.0.0.1",
"a": 1,
"b": "test",
}),
},
}
for _, test := range testData {
config.Global.EnableKeyLogging = test.EnableKeyLogging
logEntry := getLogEntryForRequest(testReq, test.Key, test.Data)
if logEntry.Data["path"] != test.Result.Data["path"] {
t.Error("Expected 'path':", test.Result.Data["path"], "Got:", logEntry.Data["path"])
}
if logEntry.Data["origin"] != test.Result.Data["origin"] {
t.Error("Expected 'origin':", test.Result.Data["origin"], "Got:", logEntry.Data["origin"])
}
if logEntry.Data["key"] != test.Result.Data["key"] {
t.Error("Expected 'key':", test.Result.Data["key"], "Got:", logEntry.Data["key"])
}
if test.Data != nil {
for key, val := range test.Data {
if logEntry.Data[key] != val {
t.Error("Expected data key:", key, "with value:", val, "Got:", logEntry.Data[key])
}
}
}
}
}