package server import ( "net/http" "net/http/httptest" "testing" ) func TestCORSMiddleware(t *testing.T) { handler := CORSMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) // Test regular request req := httptest.NewRequest("GET", "/test", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, req) if w.Header().Get("Access-Control-Allow-Origin") != "*" { t.Error("CORS origin header missing") } if w.Header().Get("Access-Control-Allow-Methods") != "*" { t.Error("CORS methods header missing") } if w.Header().Get("Access-Control-Allow-Headers") != "*" { t.Error("CORS headers header missing") } if w.Code != http.StatusOK { t.Errorf("expected 200, got %d", w.Code) } } func TestCORSPreflightOptions(t *testing.T) { called := false handler := CORSMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true })) req := httptest.NewRequest("OPTIONS", "/test", nil) w := httptest.NewRecorder() handler.ServeHTTP(w, req) if called { t.Error("OPTIONS request should not reach inner handler") } if w.Code != http.StatusOK { t.Errorf("expected 200 for OPTIONS, got %d", w.Code) } } func TestNewRouter(t *testing.T) { r := NewRouter() if r == nil { t.Fatal("NewRouter returned nil") } // Add a test route and verify it works r.Get("/test", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) }) req := httptest.NewRequest("GET", "/test", nil) w := httptest.NewRecorder() r.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("expected 200, got %d", w.Code) } if w.Body.String() != "ok" { t.Errorf("expected 'ok', got %q", w.Body.String()) } }