|
| 1 | +package httpmw |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/go-chi/chi/v5" |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/coder/coder/v2/coderd/tracing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPrometheus(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + t.Run("All", func(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + req := httptest.NewRequest("GET", "/", nil) |
| 21 | + req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chi.NewRouteContext())) |
| 22 | + res := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()} |
| 23 | + reg := prometheus.NewRegistry() |
| 24 | + Prometheus(reg)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | + w.WriteHeader(http.StatusOK) |
| 26 | + })).ServeHTTP(res, req) |
| 27 | + metrics, err := reg.Gather() |
| 28 | + require.NoError(t, err) |
| 29 | + require.Greater(t, len(metrics), 0) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestGetRoutePattern(t *testing.T) { |
| 34 | + t.Parallel() |
| 35 | + reg := prometheus.NewRegistry() |
| 36 | + promMW := Prometheus(reg) |
| 37 | + // Create a test router with some routes |
| 38 | + r := chi.NewRouter() |
| 39 | + r.With(promMW).Get("/api/v2/workspaces/{workspace}", func(w http.ResponseWriter, r *http.Request) {}) |
| 40 | + r.With(promMW).Get("/api/v2/users/{user}", func(w http.ResponseWriter, r *http.Request) {}) |
| 41 | + r.With(promMW).Get("/static/*", func(w http.ResponseWriter, r *http.Request) {}) |
| 42 | + |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + method string |
| 46 | + path string |
| 47 | + expected string |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "PatternAlreadyAvailable", |
| 51 | + method: "GET", |
| 52 | + path: "/api/v2/workspaces/test", |
| 53 | + expected: "/api/v2/workspaces/{workspace}", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "UserRoute", |
| 57 | + method: "GET", |
| 58 | + path: "/api/v2/users/john", |
| 59 | + expected: "/api/v2/users/{user}", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "StaticRoute", |
| 63 | + method: "GET", |
| 64 | + path: "/static/css/style.css", |
| 65 | + expected: "/static/*", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "NoMatchingRoute", |
| 69 | + method: "GET", |
| 70 | + path: "/nonexistent", |
| 71 | + expected: "", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "FrontendRoute", |
| 75 | + method: "GET", |
| 76 | + path: "/", |
| 77 | + expected: "", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range tests { |
| 82 | + tt := tt |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + req := httptest.NewRequest(tt.method, tt.path, nil) |
| 87 | + |
| 88 | + sw := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()} |
| 89 | + |
| 90 | + r.ServeHTTP(sw, req) |
| 91 | + |
| 92 | + metrics, err := reg.Gather() |
| 93 | + require.NoError(t, err) |
| 94 | + require.Greater(t, len(metrics), 0) |
| 95 | + |
| 96 | + // Verify the result |
| 97 | + // require.Equal(t, tt.expected, pattern, "unexpected route pattern") |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments