-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
metrics_test.go
45 lines (38 loc) · 1.09 KB
/
metrics_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
package rest
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMetrics(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(Metrics("127.0.0.1")(handler))
defer ts.Close()
resp, err := http.Get(ts.URL + "/metrics")
require.Nil(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)
b, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.True(t, strings.Contains(string(b), "cmdline"))
assert.True(t, strings.Contains(string(b), "memstats"))
}
func TestMetricsRejected(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := w.Write([]byte("blah blah"))
require.NoError(t, err)
})
ts := httptest.NewServer(Metrics("1.1.1.1")(handler))
defer ts.Close()
resp, err := http.Get(ts.URL + "/metrics")
require.Nil(t, err)
defer resp.Body.Close()
assert.Equal(t, 403, resp.StatusCode)
}