-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerotest.go
186 lines (160 loc) 路 3.88 KB
/
kerotest.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
package kerotest
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/josip/kero"
)
const DashPath = "/_kero_test"
const HelloPath = "/hello/:id"
const WaitPath = "/wait"
const WaitDuration = time.Duration(123 * time.Millisecond)
const DashUsername = "admin"
const DashPass = "pass"
const PixelPath = "/px.gif"
const pixelReferrerPath = "/blog/hello-mars"
const pixelReferrer = "http://localhost:1234" + pixelReferrerPath
const PrefixToIgnore = "/hello"
var WaitRequest = httptest.NewRequest("GET", WaitPath, nil)
type DashboardTest struct {
Description string
Path string
Authed bool
ExpectError bool
}
func (t *DashboardTest) Request() *http.Request {
req := httptest.NewRequest("GET", t.Path, nil)
if t.Authed {
req.SetBasicAuth(DashUsername, DashPass)
}
return req
}
func (t *DashboardTest) HasFailed(statusCode int) bool {
reqFailed := statusCode >= 400
return (t.ExpectError && !reqFailed) || (!t.ExpectError && reqFailed)
}
var DashboardTests = []DashboardTest{
{
Description: "prevent unauthorized access",
Path: DashPath,
Authed: false,
// gin returns 404, fiber 401 on invalid auth
ExpectError: true,
},
{
Description: "load CSS assets",
Path: DashPath + "/assets/css/app.css",
Authed: true,
ExpectError: false,
},
{
Description: "load dashboard with some content",
Path: DashPath,
Authed: true,
ExpectError: false,
},
}
type TrackingTest struct {
Path string
ExpectToBeTracked bool
}
func (t *TrackingTest) Request() *http.Request {
return httptest.NewRequest("GET", t.Path, nil)
}
var TrackingTests = []TrackingTest{
{
Path: "/favicon.ico",
ExpectToBeTracked: false,
},
{
Path: DashPath,
ExpectToBeTracked: false,
},
{
Path: "/hello/world",
ExpectToBeTracked: true,
},
{
Path: "/hello/earth",
ExpectToBeTracked: true,
},
{
Path: "/hello/go",
ExpectToBeTracked: true,
},
{
Path: "/",
ExpectToBeTracked: true,
},
}
func ExpectRequestsTracked(t *testing.T, k *kero.Kero) {
expected := 0
for _, req := range TrackingTests {
if req.ExpectToBeTracked {
expected += 1
}
}
tracked := k.Count(kero.HttpReqMetricName, 0, time.Now().Unix())
if tracked != expected {
t.Error("expected", expected, "requests to be tracked, got", tracked)
}
}
func ExpectDurationTracked(t *testing.T, k *kero.Kero) {
wants := 1
records, err := k.Query(kero.HttpReqDurationMetricName, kero.MetricLabels{}, 0, time.Now().Unix())
if err != nil {
t.Fatal("failed to query kero:", err)
}
if len(records) != wants {
t.Fatal("expected", wants, "got", len(records), "tracked events")
}
if records[0].Value < float64(WaitDuration.Milliseconds()) {
t.Error("expected request to take at least 100ms, got", records[0].Value)
}
}
func PixelRequest() *http.Request {
req := httptest.NewRequest("GET", PixelPath, nil)
req.Header.Add("Referer", pixelReferrer)
return req
}
func ExpectPixelToTrack(t *testing.T, k *kero.Kero) {
res, err := k.Query(
kero.HttpReqMetricName,
kero.MetricLabels{
kero.HttpPathLabel: pixelReferrerPath,
},
0,
time.Now().Unix(),
)
if err != nil {
t.Fatal("failed to query db", err)
}
if len(res) != 1 {
t.Fatal("expected pixel to track paths but it did not")
}
res, err = k.Query(
kero.HttpReqMetricName,
kero.MetricLabels{
kero.HttpPathLabel: PixelPath,
},
0,
time.Now().Unix(),
)
if err != nil {
t.Fatal("failed to query db", err)
}
if len(res) != 0 {
t.Fatal("expected request to pixel path not to be tracked")
}
}
func IgnoredHelloRequest() *http.Request {
req := httptest.NewRequest("GET", "/hello/mars", nil)
return req
}
func ExpectHelloIgnored(t *testing.T, k *kero.Kero) {
tracked := k.Count(kero.HttpReqMetricName, 0, time.Now().Unix())
if tracked != 0 {
t.Fatal("expected request to ignore prefix not to be tracked")
}
}