-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmetrics_test.go
70 lines (65 loc) · 2.51 KB
/
metrics_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
package tigertonic
import (
"bytes"
"net/http"
"net/url"
"testing"
)
func TestCounter(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`{"foo":"bar"}`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
counter := Counted(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}), "counted", nil)
counter.ServeHTTP(w, r)
if 1 != counter.Count() {
t.Fatal(counter.Count())
}
}
func TestCounterByStatus(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`{"foo":"bar"}`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
counterByStatus := CountedByStatus(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}), "counted", nil)
counterByStatus.ServeHTTP(w, r)
if 1 != counterByStatus.counters[200].Count() {
t.Fatal(counterByStatus.counters[200].Count())
}
if 0 != counterByStatus.counters[500].Count() {
t.Fatal(counterByStatus.counters[500].Count())
}
}
func TestCounterByStatusXX(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`{"foo":"bar"}`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
counterByStatusXX := CountedByStatusXX(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}), "counted", nil)
counterByStatusXX.ServeHTTP(w, r)
if 1 != counterByStatusXX.counter2xx.Count() {
t.Fatal(counterByStatusXX.counter2xx.Count())
}
if 0 != counterByStatusXX.counter5xx.Count() {
t.Fatal(counterByStatusXX.counter5xx.Count())
}
}
func TestTimer(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`{"foo":"bar"}`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
timer := Timed(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}), "timed", nil)
timer.ServeHTTP(w, r)
if 1 != timer.Count() {
t.Fatal(timer.Count())
}
}