forked from yfuruyama/stackdriver-request-context-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackdriver_test.go
179 lines (158 loc) · 4.65 KB
/
stackdriver_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package stalog
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestIntegration(t *testing.T) {
r, _ := http.NewRequest("GET", "/foo?bar=baz", nil)
r.Header.Add("User-Agent", "test")
w := httptest.NewRecorder()
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
logger := RequestContextLogger(r)
logger.Debugf("1")
logger.Infof("2")
logger.Warnf("3")
logger.Errorf("4")
_, _ = fmt.Fprintf(w, "OK\n")
})
requestLogOut := new(bytes.Buffer)
contextLogOut := new(bytes.Buffer)
config := NewConfig("test")
config.RequestLogOut = requestLogOut
config.ContextLogOut = contextLogOut
config.Severity = SeverityInfo
config.AdditionalData = AdditionalData{
"service": "foo",
"version": 1.0,
}
handler := RequestLogging(config)(mux)
handler.ServeHTTP(w, r)
// check request log
var httpRequestLog HTTPRequestLog
err := json.Unmarshal(requestLogOut.Bytes(), &httpRequestLog)
if err != nil {
t.Fatal(err)
}
opts := []cmp.Option{
cmpopts.IgnoreFields(HTTPRequestLog{}, "Time", "Trace"),
cmpopts.IgnoreFields(HTTPRequest{}, "RemoteIP", "ServerIP", "Latency"),
}
expected := HTTPRequestLog{
Severity: "ERROR",
AdditionalData: AdditionalData{
"service": "foo",
"version": 1.0,
},
HTTPRequest: HTTPRequest{
RequestMethod: "GET",
RequestUrl: "/foo?bar=baz",
RequestSize: "0",
Status: 200,
ResponseSize: "3",
UserAgent: "test",
Referer: "",
CacheLookup: false,
CacheHit: false,
CacheValidatedWithOriginServer: false,
Protocol: "HTTP/1.1",
},
}
if !cmp.Equal(httpRequestLog, expected, opts...) {
t.Errorf("diff: %s", cmp.Diff(httpRequestLog, expected, opts...))
}
if !strings.HasSuffix(httpRequestLog.HTTPRequest.Latency, "s") {
t.Errorf("invalid latency: %s", httpRequestLog.HTTPRequest.Latency)
}
// check context log
logs := strings.Split(contextLogOut.String(), "\n")
logs = logs[:len(logs)-1]
logExpected := []struct {
Severity string
Message string
}{
{"INFO", "2"},
{"WARNING", "3"},
{"ERROR", "4"},
}
for idx, log := range logs {
var cLog contextLog
if err := json.Unmarshal([]byte(log), &cLog); err != nil {
t.Fatal(err)
}
expected := contextLog{
Severity: logExpected[idx].Severity,
Message: logExpected[idx].Message,
AdditionalData: AdditionalData{
"service": "foo",
"version": 1.0,
},
}
opts := []cmp.Option{
cmpopts.IgnoreFields(contextLog{}, "Time", "Trace", "SourceLocation"),
}
if !cmp.Equal(cLog, expected, opts...) {
t.Errorf("diff: %s", cmp.Diff(cLog, expected, opts...))
}
// check trace
if httpRequestLog.Trace != cLog.Trace {
t.Errorf("different trace: httpRequestLog=%s, contextLog=%s", httpRequestLog.Trace, cLog.Trace)
}
}
}
func TestNoContextLog(t *testing.T) {
r, _ := http.NewRequest("GET", "/foo?bar=baz", nil)
r.Header.Add("User-Agent", "test")
w := httptest.NewRecorder()
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "OK\n")
})
requestLogOut := new(bytes.Buffer)
contextLogOut := new(bytes.Buffer)
config := NewConfig("test")
config.RequestLogOut = requestLogOut
config.ContextLogOut = contextLogOut
handler := RequestLogging(config)(mux)
handler.ServeHTTP(w, r)
// check request log
var httpRequestLog HTTPRequestLog
err := json.Unmarshal(requestLogOut.Bytes(), &httpRequestLog)
if err != nil {
t.Fatal(err)
}
opts := []cmp.Option{
cmpopts.IgnoreFields(HTTPRequestLog{}, "Time", "Trace"),
cmpopts.IgnoreFields(HTTPRequest{}, "RemoteIP", "ServerIP", "Latency"),
}
expected := HTTPRequestLog{
Severity: "DEFAULT",
AdditionalData: nil,
HTTPRequest: HTTPRequest{
RequestMethod: "GET",
RequestUrl: "/foo?bar=baz",
RequestSize: "0",
Status: 200,
ResponseSize: "3",
UserAgent: "test",
Referer: "",
CacheLookup: false,
CacheHit: false,
CacheValidatedWithOriginServer: false,
Protocol: "HTTP/1.1",
},
}
if !cmp.Equal(httpRequestLog, expected, opts...) {
t.Errorf("diff: %s", cmp.Diff(httpRequestLog, expected, opts...))
}
if len(contextLogOut.Bytes()) != 0 {
t.Errorf("context log exists: %s", contextLogOut.String())
}
}