-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestlogger_test.go
158 lines (145 loc) · 4.24 KB
/
requestlogger_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
package log
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// MARK: Re-used variables
var (
mockHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(25 * time.Millisecond)
(rw).Header().Set("Content-Type", "text/plain")
(rw).WriteHeader(200)
_, _ = (rw).Write([]byte("done"))
})
mockBody = []byte(`{ "bodyProperty": "bodyValue" }`)
rr = httptest.NewRecorder()
mockRequest, _ = http.NewRequest(
http.MethodGet,
"/test-path?testVar=testVal",
bytes.NewBuffer(mockBody))
)
func TestMain(m *testing.M) {
// setup
mockRequest.Header.Set("Content-Type", "application/json")
// run
m.Run()
// teardown
}
func noExtras(t *testing.T) {
h := NewRequestLogger(RequestLoggerConfig{
Headers: false,
Params: false,
Body: false,
})
h.Handle(mockHandler).ServeHTTP(rr, mockRequest)
}
func headersAndParams(t *testing.T) {
h := NewRequestLogger(RequestLoggerConfig{
Headers: true,
Params: true,
Body: false,
})
h.Handle(mockHandler).ServeHTTP(rr, mockRequest)
}
func headersAndBody(t *testing.T) {
h := NewRequestLogger(RequestLoggerConfig{
Headers: true,
Params: false,
Body: true,
})
h.Handle(mockHandler).ServeHTTP(rr, mockRequest)
}
func timedOutRequest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
slowRequest, _ := http.NewRequestWithContext(ctx, "GET", "/slow-op", nil)
slowHandler := http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
res := make(chan struct{})
go func() {
time.Sleep(25 * time.Millisecond)
res <- struct{}{}
}()
select {
case <-rq.Context().Done():
(rw).Header().Set("Content-Type", "text/plain")
(rw).WriteHeader(500)
_, _ = (rw).Write([]byte("timeout"))
case <-res:
(rw).Header().Set("Content-Type", "text/plain")
(rw).WriteHeader(200)
_, _ = (rw).Write([]byte("done"))
}
})
h := NewRequestLogger(RequestLoggerConfig{})
h.Handle(slowHandler).ServeHTTP(rr, slowRequest)
}
func cancelledRequest(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
slowRequest, _ := http.NewRequestWithContext(ctx, "GET", "/slow-op", nil)
slowHandler := http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
res := make(chan struct{})
go func() {
time.Sleep(25 * time.Millisecond)
res <- struct{}{}
}()
select {
case <-rq.Context().Done():
(rw).Header().Set("Content-Type", "text/plain")
(rw).WriteHeader(500)
_, _ = (rw).Write([]byte("canceled"))
case <-res:
(rw).Header().Set("Content-Type", "text/plain")
(rw).WriteHeader(200)
_, _ = (rw).Write([]byte("done"))
}
})
h := NewRequestLogger(RequestLoggerConfig{})
h.Handle(slowHandler).ServeHTTP(rr, slowRequest)
}
func handlerError(t *testing.T) {
ctx, cancel := context.WithCancelCause(context.Background())
defer cancel(errors.New("some other error"))
errorRequest, _ := http.NewRequestWithContext(ctx, "GET", "/error-op", nil)
errorHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Content-Type", "text/plain")
(w).WriteHeader(500)
_, _ = (w).Write([]byte("some error occurred"))
})
h := NewRequestLogger(RequestLoggerConfig{})
h.Handle(errorHandler).ServeHTTP(rr, errorRequest)
}
func TestRequestLogger_Handle(t *testing.T) {
t.Run("String Logger", func(t *testing.T) {
SetupLocalLogger(LogLevelDebug)
t.Run("no extras", noExtras)
t.Run("headers and params", headersAndParams)
t.Run("headers and body", headersAndBody)
t.Run("timed-out rq", timedOutRequest)
t.Run("cancelled rq", cancelledRequest)
t.Run("handler error", handlerError)
})
t.Run("JSON Logger", func(t *testing.T) {
SetupCloudLogger(LogLevelDebug, []string{"logger", "test"})
t.Run("no extras", noExtras)
t.Run("headers and params", headersAndParams)
t.Run("headers and body", headersAndBody)
t.Run("timed-out rq", timedOutRequest)
t.Run("cancelled rq", cancelledRequest)
t.Run("handler error", handlerError)
})
t.Run("Sub-Logger", func(t *testing.T) {
SetupLocalLogger(LogLevelDebug)
sl := Sublogger("sub-logger")
h := NewRequestLogger(RequestLoggerConfig{
Logger: sl,
Tags: []string{"requests"},
})
h.Handle(mockHandler).ServeHTTP(rr, mockRequest)
})
}