1b7b70426c
- config: koanf-based loading (defaults → YAML → KINDEXR_ env vars) - db: embedded SQLite migrations with BEGIN/END-aware statement splitter - server: chi router, GET /health returns JSON stats - cmd/kindexr: graceful SIGTERM shutdown - cmd/kindexr-cli: stub - deploy: systemd unit, example config, nginx snippet - all packages covered by race-clean tests
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package server_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.utn.lol/enki/kindexr/internal/config"
|
|
"git.utn.lol/enki/kindexr/internal/db"
|
|
"git.utn.lol/enki/kindexr/internal/server"
|
|
)
|
|
|
|
func newTestServer(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
database, err := db.Open(filepath.Join(dir, "test.db"))
|
|
if err != nil {
|
|
t.Fatalf("open test db: %v", err)
|
|
}
|
|
t.Cleanup(func() { database.Close() })
|
|
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("load config: %v", err)
|
|
}
|
|
|
|
srv := server.New(cfg, database, "0.1.0-test")
|
|
return httptest.NewServer(srv.Handler())
|
|
}
|
|
|
|
func TestHealthEndpoint(t *testing.T) {
|
|
ts := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/health")
|
|
if err != nil {
|
|
t.Fatalf("GET /health: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var body map[string]interface{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode JSON: %v", err)
|
|
}
|
|
|
|
if body["status"] != "ok" {
|
|
t.Errorf("expected status=ok, got %v", body["status"])
|
|
}
|
|
if _, ok := body["version"]; !ok {
|
|
t.Error("expected 'version' field in response")
|
|
}
|
|
if rc, ok := body["relays_connected"]; !ok {
|
|
t.Error("expected 'relays_connected' field in response")
|
|
} else if rc.(float64) != 0 {
|
|
t.Errorf("expected relays_connected=0, got %v", rc)
|
|
}
|
|
if et, ok := body["events_total"]; !ok {
|
|
t.Error("expected 'events_total' field in response")
|
|
} else if et.(float64) != 0 {
|
|
t.Errorf("expected events_total=0, got %v", et)
|
|
}
|
|
// last_event_at should be present (null is fine for empty DB).
|
|
if _, ok := body["last_event_at"]; !ok {
|
|
t.Error("expected 'last_event_at' field in response")
|
|
}
|
|
}
|
|
|
|
func TestHealthContentType(t *testing.T) {
|
|
ts := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/health")
|
|
if err != nil {
|
|
t.Fatalf("GET /health: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
ct := resp.Header.Get("Content-Type")
|
|
if !strings.HasPrefix(ct, "application/json") {
|
|
t.Errorf("expected Content-Type application/json, got %q", ct)
|
|
}
|
|
}
|
|
|
|
func TestHealthMethod(t *testing.T) {
|
|
ts := newTestServer(t)
|
|
defer ts.Close()
|
|
|
|
// GET should return 200.
|
|
resp, err := http.Get(ts.URL + "/health")
|
|
if err != nil {
|
|
t.Fatalf("GET /health: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("GET /health: expected 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
// POST should return 405.
|
|
resp, err = http.Post(ts.URL+"/health", "application/json", nil)
|
|
if err != nil {
|
|
t.Fatalf("POST /health: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Errorf("POST /health: expected 405, got %d", resp.StatusCode)
|
|
}
|
|
}
|