|
1 | | -package common_test |
| 1 | +package common |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "net/http" |
5 | 5 | "net/http/httptest" |
6 | 6 | "testing" |
7 | 7 | "time" |
8 | 8 |
|
9 | | - "code.gitea.io/gitea/routers/common" |
10 | | - |
11 | 9 | "github.com/go-chi/chi/v5" |
| 10 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 11 | + "github.com/stretchr/testify/assert" |
12 | 12 | "github.com/stretchr/testify/require" |
13 | 13 | ) |
14 | 14 |
|
15 | 15 | func TestMetricsMiddlewere(t *testing.T) { |
16 | | - |
17 | | - middleware := common.RouteMetrics() |
| 16 | + middleware := RouteMetrics() |
18 | 17 | r := chi.NewRouter() |
19 | 18 | r.Use(middleware) |
20 | 19 | r.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
21 | 20 | w.Write([]byte("test")) |
22 | 21 | time.Sleep(5 * time.Millisecond) |
23 | 22 | })) |
24 | | - |
25 | 23 | testServer := httptest.NewServer(r) |
26 | | - |
| 24 | + // Check all defined metrics |
| 25 | + verify := func(i int) { |
| 26 | + assert.Equal(t, testutil.CollectAndCount(reqDurationHistogram, "http_server_request_duration"), i) |
| 27 | + assert.Equal(t, testutil.CollectAndCount(reqSizeHistogram, "http_server_request_body_size"), i) |
| 28 | + assert.Equal(t, testutil.CollectAndCount(respSizeHistogram, "http_server_response_body_size"), i) |
| 29 | + assert.Equal(t, testutil.CollectAndCount(reqInflightGauge, "http_server_active_requests"), i) |
| 30 | + } |
| 31 | + |
| 32 | + // Check they don't exist before making a request |
| 33 | + verify(0) |
27 | 34 | _, err := http.Get(testServer.URL) |
28 | 35 | require.NoError(t, err) |
29 | | - |
| 36 | + // Check they do exist after making the request |
| 37 | + verify(1) |
30 | 38 | } |
0 commit comments