-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounters_test.go
224 lines (195 loc) · 6.61 KB
/
counters_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
214
215
216
217
218
219
220
221
222
223
224
package gohm_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/karrick/gohm"
)
func testCounter(t *testing.T, status int) gohm.Counters {
var counters gohm.Counters
response := "some response"
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if status != http.StatusOK {
// To ensure status code set even when next handler does not
// explicitly set it, we omit setting header when StatusOK is
// provided, and below we invoke this test function for both
// http.StatusOK and another 2xx status code.
w.WriteHeader(status)
}
w.Write([]byte(response))
}), gohm.Config{Counters: &counters})
handler.ServeHTTP(recorder, request)
resp := recorder.Result()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if got, want := resp.StatusCode, status; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
if got, want := string(body), response; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.GetAll(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
return counters
}
func TestStatusCounters1xx(t *testing.T) {
counters := testCounter(t, http.StatusContinue) // 100
if got, want := counters.Get1xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
// get and reset counter
if got, want := counters.GetAndReset1xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestStatusCounters2xxWithoutHandlerWritingStatus(t *testing.T) {
counters := testCounter(t, http.StatusOK) // 200
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestStatusCounters2xx(t *testing.T) {
counters := testCounter(t, http.StatusCreated) // 201
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
// get and reset counter
if got, want := counters.GetAndReset2xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestStatusCounters3xx(t *testing.T) {
counters := testCounter(t, http.StatusFound) // 302
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
// get and reset counter
if got, want := counters.GetAndReset3xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestStatusCounters4xx(t *testing.T) {
counters := testCounter(t, http.StatusForbidden) // 403
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
// get and reset counter
if got, want := counters.GetAndReset4xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestStatusCounters5xx(t *testing.T) {
counters := testCounter(t, http.StatusGatewayTimeout) // 504
if got, want := counters.Get1xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get2xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get3xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get4xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
// get and reset counter
if got, want := counters.GetAndReset5xx(), uint64(1); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := counters.Get5xx(), uint64(0); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func BenchmarkWithCounters(b *testing.B) {
var counters gohm.Counters
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
}), gohm.Config{Counters: &counters})
b.ResetTimer()
for i := 0; i < b.N; i++ {
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler.ServeHTTP(recorder, request)
}
}