-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstats_test.go
92 lines (73 loc) · 3.67 KB
/
stats_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
"go.uber.org/mock/gomock"
)
func TestStatsResponseWriterWritesResponseMetricOnce(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
statsWriter := newStatsWriter(recorder, "myhandler")
statsWriter.WriteHeader(http.StatusBadRequest)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("expected status code %d, got %d", http.StatusBadRequest, recorder.Code)
}
statsWriter.WriteHeader(http.StatusCreated)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("tried to write to the headers again: Expected status code %d, got %d", http.StatusBadRequest, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "client_failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "client_failure")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "400")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "400")))
}
}
func TestStatsResponseWriterWritesToHeaderOnWrite(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
statsWriter := newStatsWriter(recorder, "myhandler")
statsWriter.Write([]byte("hello"))
if recorder.Code != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "success")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "success")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "200")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "200")))
}
}
func TestWrappingStatsResponseWriteWritesAllMetrics(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
inner := newStatsWriter(recorder, "inner")
wrapper := newStatsWriter(inner, "wrapper")
wrapper.WriteHeader(http.StatusInternalServerError)
if recorder.Code != http.StatusInternalServerError {
t.Fatalf("expected status code %d, got %d", http.StatusInternalServerError, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("wrapper", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "failure")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("wrapper", "500")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "500")))
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("inner", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "failure")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("inner", "500")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "500")))
}
}