-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmiddleware_test.go
58 lines (48 loc) · 1.5 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
package echoprometheus
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
)
func TestMetric(t *testing.T) {
//arrange
e := echo.New()
e.Use(MetricsMiddleware())
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
request := httptest.NewRequest(echo.GET, "/metrics", nil)
recorder := httptest.NewRecorder()
//action
e.ServeHTTP(recorder, request)
//assert
assert.Equal(t, http.StatusOK, recorder.Code)
assert.NotEmpty(t, recorder.Body.String())
assert.True(t, strings.Contains(recorder.Body.String(), "echo"))
assert.True(t, strings.Contains(recorder.Body.String(), "http"))
}
func TestMetricWithCustomConfig(t *testing.T) {
//arrange
e := echo.New()
var configMetrics = NewConfig()
configMetrics.Namespace = "namespace"
configMetrics.Subsystem = "test_subsystem"
configMetrics.NormalizeHTTPStatus = false
configMetrics.Buckets = []float64{
1, // 1s
2, // 2s
}
e.Use(MetricsMiddlewareWithConfig(configMetrics))
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
request := httptest.NewRequest(echo.GET, "/metrics", nil)
recorder := httptest.NewRecorder()
//action
e.ServeHTTP(recorder, request)
//assert
assert.Equal(t, http.StatusOK, recorder.Code)
assert.NotEmpty(t, recorder.Body.String())
assert.True(t, strings.Contains(recorder.Body.String(), "namespace"))
assert.True(t, strings.Contains(recorder.Body.String(), "test_subsystem"))
}