This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package metricsapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/metriccatalog"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
)
|
||||
|
||||
type Handler struct{ Registry metriccatalog.Registry }
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/metrics/catalog" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
|
||||
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read the metric catalog.", nil)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=60")
|
||||
w.Header().Set("ETag", `"`+h.Registry.Version()+`"`)
|
||||
if r.Header.Get("If-None-Match") == `"`+h.Registry.Version()+`"` {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(h.Registry.Response()); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package metricsapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/metriccatalog"
|
||||
)
|
||||
|
||||
func TestCatalogRequiresAuthentication(t *testing.T) {
|
||||
registry, err := metriccatalog.DefaultRegistry()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/metrics/catalog", nil)
|
||||
Handler{Registry: registry}.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogReturnsVersionedBindingsAndSupportsETag(t *testing.T) {
|
||||
registry, err := metriccatalog.DefaultRegistry()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := Handler{Registry: registry}
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/metrics/catalog", nil).WithContext(auth.WithPrincipal(context.Background(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
if response.Header().Get("ETag") == "" {
|
||||
t.Fatal("missing ETag")
|
||||
}
|
||||
if len(response.Body.Bytes()) == 0 {
|
||||
t.Fatal("empty catalog")
|
||||
}
|
||||
etag := response.Header().Get("ETag")
|
||||
request = httptest.NewRequest(http.MethodGet, "/api/v1/metrics/catalog", nil).WithContext(auth.WithPrincipal(context.Background(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
request.Header.Set("If-None-Match", etag)
|
||||
response = httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusNotModified {
|
||||
t.Fatalf("etag status=%d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user