73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestHealthHandler(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected status %d, got %d", http.StatusOK, recorder.Code)
|
|
}
|
|
|
|
var payload healthResponse
|
|
if err := json.NewDecoder(recorder.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if payload.Status != "ok" {
|
|
t.Fatalf("expected status %q, got %q", "ok", payload.Status)
|
|
}
|
|
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/json" {
|
|
t.Fatalf("expected Content-Type application/json, got %q", contentType)
|
|
}
|
|
}
|
|
|
|
func TestStatusHandler(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/api/v1/status", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected status %d, got %d", http.StatusOK, recorder.Code)
|
|
}
|
|
|
|
var payload statusResponse
|
|
if err := json.NewDecoder(recorder.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if payload.Service != "clustercanvas" {
|
|
t.Fatalf("expected service %q, got %q", "clustercanvas", payload.Service)
|
|
}
|
|
if payload.Version != Version {
|
|
t.Fatalf("expected version %q, got %q", Version, payload.Version)
|
|
}
|
|
}
|
|
|
|
func TestCORSPreflight(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
request := httptest.NewRequest(http.MethodOptions, "/health", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
NewRouter().ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusNoContent {
|
|
t.Fatalf("expected status %d, got %d", http.StatusNoContent, recorder.Code)
|
|
}
|
|
if origin := recorder.Header().Get("Access-Control-Allow-Origin"); origin != defaultAllowedOrigin {
|
|
t.Fatalf("expected Allow-Origin %q, got %q", defaultAllowedOrigin, origin)
|
|
}
|
|
}
|