4.1 KB
controller_test.go
package application
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestQueryParam(t *testing.T) {
tests := []struct {
name string
url string
param string
defaultValue string
expected string
}{
{"existing param", "/?foo=bar", "foo", "default", "bar"},
{"missing param", "/?foo=bar", "baz", "default", "default"},
{"empty param", "/?foo=", "foo", "default", "default"},
{"no query string", "/", "foo", "default", "default"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.url, nil)
c := &BaseController{Request: req}
result := c.QueryParam(tt.param, tt.defaultValue)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestQueryParamNilRequest(t *testing.T) {
c := &BaseController{}
result := c.QueryParam("foo", "default")
if result != "default" {
t.Errorf("expected 'default', got %q", result)
}
}
func TestIntParam(t *testing.T) {
tests := []struct {
name string
url string
param string
defaultValue int
expected int
}{
{"valid int", "/?page=5", "page", 1, 5},
{"missing param", "/?foo=bar", "page", 1, 1},
{"invalid int", "/?page=abc", "page", 1, 1},
{"negative int", "/?page=-3", "page", 1, -3},
{"zero", "/?page=0", "page", 1, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.url, nil)
c := &BaseController{Request: req}
result := c.IntParam(tt.param, tt.defaultValue)
if result != tt.expected {
t.Errorf("expected %d, got %d", tt.expected, result)
}
})
}
}
func TestIsHTMX(t *testing.T) {
c := &BaseController{}
// Non-HTMX request
req := httptest.NewRequest(http.MethodGet, "/", nil)
if c.IsHTMX(req) {
t.Error("expected false for non-HTMX request")
}
// HTMX request
req = httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("HX-Request", "true")
if !c.IsHTMX(req) {
t.Error("expected true for HTMX request")
}
}
func TestRedirect(t *testing.T) {
c := &BaseController{}
// Non-HTMX redirect
t.Run("non-HTMX", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c.Redirect(rec, req, "/dashboard")
if rec.Code != http.StatusSeeOther {
t.Errorf("expected status %d, got %d", http.StatusSeeOther, rec.Code)
}
if loc := rec.Header().Get("Location"); loc != "/dashboard" {
t.Errorf("expected Location '/dashboard', got %q", loc)
}
})
// HTMX redirect
t.Run("HTMX", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("HX-Request", "true")
rec := httptest.NewRecorder()
c.Redirect(rec, req, "/dashboard")
if rec.Code != http.StatusOK {
t.Errorf("expected status %d, got %d", http.StatusOK, rec.Code)
}
if loc := rec.Header().Get("HX-Location"); loc != "/dashboard" {
t.Errorf("expected HX-Location '/dashboard', got %q", loc)
}
})
}
func TestRefresh(t *testing.T) {
c := &BaseController{}
// Non-HTMX refresh
t.Run("non-HTMX", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/current", nil)
rec := httptest.NewRecorder()
c.Refresh(rec, req)
if rec.Code != http.StatusSeeOther {
t.Errorf("expected status %d, got %d", http.StatusSeeOther, rec.Code)
}
if loc := rec.Header().Get("Location"); loc != "/current" {
t.Errorf("expected Location '/current', got %q", loc)
}
})
// HTMX refresh
t.Run("HTMX", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/current", nil)
req.Header.Set("HX-Request", "true")
rec := httptest.NewRecorder()
c.Refresh(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected status %d, got %d", http.StatusOK, rec.Code)
}
if refresh := rec.Header().Get("HX-Refresh"); refresh != "true" {
t.Errorf("expected HX-Refresh 'true', got %q", refresh)
}
})
}
func TestSetup(t *testing.T) {
app := &App{
controllers: make(map[string]Controller),
}
c := &BaseController{}
c.Setup(app)
if c.App != app {
t.Error("expected App to be set")
}
}