-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware_test.go
62 lines (49 loc) · 1.55 KB
/
middleware_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
package fiberprometheus
import (
"github.com/gofiber/fiber"
"io/ioutil"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMiddleware_Register(t *testing.T) {
app := fiber.New()
m := NewMiddleware("test_namespace", "test_http_subsystem", "/test-metrics")
m.Register(app)
app.Get("/testurl", func(c *fiber.Ctx) {
c.Send("Hello test")
})
req := httptest.NewRequest(
"GET",
"/testurl",
nil,
)
resp, _ := app.Test(req, -1)
assert.NotNil(t, resp)
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
// Get metrics
req = httptest.NewRequest(
"GET",
"/test-metrics",
nil,
)
resp, _ = app.Test(req, -1)
responseBody, _ := ioutil.ReadAll(resp.Body)
reqDurationText := "test_namespace_test_http_subsystem_request_duration_seconds_bucket{handler=\"/testurl\",method=\"GET\",le=\"5\"} 1"
reqCountText := "test_namespace_test_http_subsystem_requests_total{method=\"GET\",path=\"/testurl\",status_code=\"200\"} 1"
assert.Contains(t, string(responseBody), reqDurationText)
assert.Contains(t, string(responseBody), reqCountText)
}
func TestMiddleware_SetupPath(t *testing.T) {
app := fiber.New()
m := NewMiddleware("test_namespace", "test_http_subsystem", "/test-metrics")
m.SetupPath(app)
assert.Equal(t, "/test-metrics", m.MetricPath)
}
func TestNewMiddleware(t *testing.T) {
m := NewMiddleware("test_namespace", "test_http_subsystem", "/test-metrics")
assert.NotNil(t, m)
assert.Equal(t, "test_namespace", m.Namespace)
assert.Equal(t, "test_http_subsystem", m.Subsystem)
assert.Equal(t, "/test-metrics", m.MetricPath)
}