This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package networkapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/network"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
)
|
||||
|
||||
type Handler struct{ Provider network.Provider }
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/v1/network" {
|
||||
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 network health.", nil)
|
||||
return
|
||||
}
|
||||
if err := r.Context().Err(); err != nil {
|
||||
return
|
||||
}
|
||||
if h.Provider == nil {
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "NETWORK_UNAVAILABLE", "Network health not available", "De netwerkgezondheid is niet beschikbaar.", nil)
|
||||
return
|
||||
}
|
||||
snapshot, err := h.Provider.Snapshot(r.Context())
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return
|
||||
}
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "NETWORK_UNAVAILABLE", "Network health not available", "De netwerkgezondheid kon niet worden gelezen.", nil)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=5")
|
||||
_ = json.NewEncoder(w).Encode(snapshot)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package networkapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/network"
|
||||
)
|
||||
|
||||
type staticProvider struct {
|
||||
snapshot network.Snapshot
|
||||
err error
|
||||
}
|
||||
|
||||
func (p staticProvider) Snapshot(context.Context) (network.Snapshot, error) { return p.snapshot, p.err }
|
||||
|
||||
func authenticatedNetworkRequest() *http.Request {
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/network", nil)
|
||||
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
}
|
||||
|
||||
func TestNetworkHandlerAuthAndSafeOutput(t *testing.T) {
|
||||
handler := Handler{Provider: staticProvider{snapshot: network.Snapshot{ContractVersion: network.ContractVersion, ObservedAt: time.Now().UTC(), Health: []network.Health{{Scope: network.ScopeInternet, State: network.StateDown, Reason: "target_failed"}}}}}
|
||||
unauthorized := httptest.NewRecorder()
|
||||
handler.ServeHTTP(unauthorized, httptest.NewRequest(http.MethodGet, "/api/v1/network", nil))
|
||||
if unauthorized.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unauthorized status=%d", unauthorized.Code)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, authenticatedNetworkRequest())
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"scope":"internet"`) || !strings.Contains(response.Body.String(), `"state":"down"`) {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworkHandlerMapsProviderFailure(t *testing.T) {
|
||||
handler := Handler{Provider: staticProvider{err: errors.New("source down")}}
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, authenticatedNetworkRequest())
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status=%d", response.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user