-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathkey_value_test.go
48 lines (36 loc) · 996 Bytes
/
key_value_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
package expmetric
import (
"expvar"
"testing"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/httptest"
)
type testJSONValue struct {
Path string `json:"name"`
}
func TestKeyValueStruct(t *testing.T) {
app := iris.New()
var (
keyFunc = func(ctx iris.Context) string {
return "test-key"
}
valueFunc = func(ctx iris.Context) (interface{}, bool) {
value := testJSONValue{
Path: ctx.Path(),
}
return value, true
}
expectedResponseBody = `{"test-key": {"name":"/test-prefix/path"}}`
metricMiddleware = KeyValue(keyFunc, valueFunc, MetricName("test-metric"))
)
handler := func(ctx iris.Context) {
ctx.ContentType("application/json")
variable := expvar.Get("test-metric").String()
ctx.WriteString(variable)
}
app.Get("/test-prefix/path", metricMiddleware, handler)
e := httptest.New(t, app)
e.GET("/test-prefix/path").Expect().Status(httptest.StatusOK).
HasContentType("application/json").
Body().IsEqual(expectedResponseBody)
}