- Add .golangci.yml with v2 config (errcheck, govet, staticcheck, misspell, etc.) - Fix 32 errcheck issues across config, discord, ntfy, server packages - Fix misspelling: cancelled → canceled - Fix staticcheck: use append(slice...) instead of loop - Fix staticcheck: remove empty error branch - Use t.Setenv instead of os.Setenv/Unsetenv in tests - Update CI workflow: add lint job, release tagging, ntfy notifications
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestServer_HealthEndpoint_StatusCodes(t *testing.T) {
|
|
// Test health endpoint returns JSON
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"status":"ok","healthy":true}`))
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
|
|
if w.Header().Get("Content-Type") != "application/json" {
|
|
t.Error("expected Content-Type application/json")
|
|
}
|
|
}
|
|
|
|
func TestServer_ReadyEndpoint_StatusCodes(t *testing.T) {
|
|
// Test ready endpoint returns JSON
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"status":"ready","ready":true}`))
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func TestServer_Shutdown(t *testing.T) {
|
|
// Create a minimal server for shutdown testing
|
|
srv := &http.Server{
|
|
Addr: ":0",
|
|
Handler: http.NewServeMux(),
|
|
}
|
|
|
|
// Start in background
|
|
go func() { _ = srv.ListenAndServe() }()
|
|
|
|
// Give it a moment to start
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
// Shutdown should complete without error
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
err := srv.Shutdown(ctx)
|
|
if err != nil {
|
|
t.Errorf("Shutdown() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestServer_MetricsEndpoint(t *testing.T) {
|
|
// Verify /metrics endpoint can be created
|
|
// The actual promhttp.Handler() is tested by Prometheus library
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte("# metrics here"))
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
}
|