-
Notifications
You must be signed in to change notification settings - Fork 31
/
middleware_test.go
213 lines (182 loc) · 6.75 KB
/
middleware_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package chiprometheus
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/go-chi/chi"
"github.com/prometheus/client_golang/prometheus"
)
func Test_Logger(t *testing.T) {
recorder := httptest.NewRecorder()
n := chi.NewRouter()
m := NewMiddleware("test")
n.Use(m)
n.Handle("/metrics", prometheus.Handler())
n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
n.Get(`/users/{firstName}`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
req1, err := http.NewRequest("GET", "http://localhost:3000/ok", nil)
if err != nil {
t.Error(err)
}
req2, err := http.NewRequest("GET", "http://localhost:3000/users/JoeBob", nil)
if err != nil {
t.Error(err)
}
req3, err := http.NewRequest("GET", "http://localhost:3000/users/Misty", nil)
if err != nil {
t.Error(err)
}
req4, err := http.NewRequest("GET", "http://localhost:3000/metrics", nil)
if err != nil {
t.Error(err)
}
n.ServeHTTP(recorder, req1)
n.ServeHTTP(recorder, req2)
n.ServeHTTP(recorder, req3)
n.ServeHTTP(recorder, req4)
body := recorder.Body.String()
if !strings.Contains(body, reqsName) {
t.Errorf("body does not contain request total entry '%s'", reqsName)
}
if !strings.Contains(body, latencyName) {
t.Errorf("body does not contain request duration entry '%s'", latencyName)
}
req1Count := `chi_request_duration_milliseconds_count{code="OK",method="GET",path="/ok",service="test"} 1`
req2Count := `chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/JoeBob",service="test"} 1`
req3Count := `chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/Misty",service="test"} 1`
if !strings.Contains(body, req1Count) {
t.Errorf("body does not contain req1 count summary '%s'", req1Count)
}
if !strings.Contains(body, req2Count) {
t.Errorf("body does not contain req1 count summary '%s'", req2Count)
}
if !strings.Contains(body, req3Count) {
t.Errorf("body does not contain req1 count summary '%s'", req3Count)
}
}
func Test_PatternLogger(t *testing.T) {
recorder := httptest.NewRecorder()
n := chi.NewRouter()
m := NewPatternMiddleware("patternOnlyTest")
n.Use(m)
n.Handle("/metrics", prometheus.Handler())
n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
n.Get(`/users/{firstName}`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
req1, err := http.NewRequest("GET", "http://localhost:3000/ok", nil)
if err != nil {
t.Error(err)
}
req2, err := http.NewRequest("GET", "http://localhost:3000/users/JoeBob", nil)
if err != nil {
t.Error(err)
}
req3, err := http.NewRequest("GET", "http://localhost:3000/users/Misty", nil)
if err != nil {
t.Error(err)
}
req4, err := http.NewRequest("GET", "http://localhost:3000/metrics", nil)
if err != nil {
t.Error(err)
}
n.ServeHTTP(recorder, req1)
n.ServeHTTP(recorder, req2)
n.ServeHTTP(recorder, req3)
n.ServeHTTP(recorder, req4)
body := recorder.Body.String()
if !strings.Contains(body, patternReqsName) {
t.Errorf("body does not contain request total entry '%s'", patternReqsName)
}
if !strings.Contains(body, patternLatencyName) {
t.Errorf("body does not contain request duration entry '%s'", patternLatencyName)
}
req1Count := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/ok",service="patternOnlyTest"} 1`
joeBobCount := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/users/JoeBob",service="patternOnlyTest"} 1`
mistyCount := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/users/Misty",service="patternOnlyTest"} 1`
firstNamePatternCount := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/users/{firstName}",service="patternOnlyTest"} 2`
if !strings.Contains(body, req1Count) {
t.Errorf("body does not contain req1 count summary '%s'", req1Count)
}
if strings.Contains(body, joeBobCount) {
t.Errorf("body should not contain Joe Bob count summary '%s'", joeBobCount)
}
if strings.Contains(body, mistyCount) {
t.Errorf("body should not contain Misty count summary '%s'", mistyCount)
}
if !strings.Contains(body, firstNamePatternCount) {
t.Errorf("body does not contain first name pattern count summary '%s'", firstNamePatternCount)
}
}
func Test_MultipleLoggers(t *testing.T) {
recorder := httptest.NewRecorder()
n := chi.NewRouter()
mid := NewMiddleware("pathTest")
m := NewPatternMiddleware("patternTest")
n.Use(mid)
n.Use(m)
n.Handle("/metrics", prometheus.Handler())
n.Get(`/ok`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
n.Get(`/users/{firstName}`, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
req1, err := http.NewRequest("GET", "http://localhost:3000/ok", nil)
if err != nil {
t.Error(err)
}
req2, err := http.NewRequest("GET", "http://localhost:3000/users/JoeBob", nil)
if err != nil {
t.Error(err)
}
req3, err := http.NewRequest("GET", "http://localhost:3000/users/Misty", nil)
if err != nil {
t.Error(err)
}
req4, err := http.NewRequest("GET", "http://localhost:3000/metrics", nil)
if err != nil {
t.Error(err)
}
n.ServeHTTP(recorder, req1)
n.ServeHTTP(recorder, req2)
n.ServeHTTP(recorder, req3)
n.ServeHTTP(recorder, req4)
body := recorder.Body.String()
if !strings.Contains(body, patternReqsName) {
t.Errorf("body does not contain request total entry '%s'", patternReqsName)
}
if !strings.Contains(body, patternLatencyName) {
t.Errorf("body does not contain request duration entry '%s'", patternLatencyName)
}
req1Count := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/ok",service="patternTest"} 1`
joeBobCount := `chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/JoeBob",service="pathTest"} 1`
mistyCount := `chi_request_duration_milliseconds_count{code="OK",method="GET",path="/users/Misty",service="pathTest"} 1`
firstNamePatternCount := `chi_pattern_request_duration_milliseconds_count{code="OK",method="GET",path="/users/{firstName}",service="patternTest"} 2`
if !strings.Contains(body, req1Count) {
t.Errorf("body does not contain req1 count summary '%s'", req1Count)
}
if !strings.Contains(body, joeBobCount) {
t.Errorf("body does not contain Joe Bob count summary '%s'", joeBobCount)
}
if !strings.Contains(body, mistyCount) {
t.Errorf("body does not contain Misty count summary '%s'", mistyCount)
}
if !strings.Contains(body, firstNamePatternCount) {
t.Errorf("body does not contain first name pattern count summary '%s'", firstNamePatternCount)
}
}