package server import ( "net/http" "net/http/httptest" "strings" "testing" ) // TestIncludeComposesInProcess pins page-anatomy "A page arrives complete": // an Include dispatches through the router with the caller's context and // an HX-Request header, returns the fragment on 200, and errors otherwise; // IncludeOr turns the error into an inline notice, never a blank region. func TestIncludeComposesInProcess(t *testing.T) { mux := http.NewServeMux() var seenHX, seenMethod string mux.HandleFunc("GET /partials/ok", func(w http.ResponseWriter, r *http.Request) { seenHX = r.Header.Get("HX-Request") seenMethod = r.Method _, _ = w.Write([]byte("

ok

")) }) mux.HandleFunc("GET /partials/bad", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "boom", http.StatusInternalServerError) }) inc := NewInclude(mux) r := httptest.NewRequest(http.MethodPost, "/products", strings.NewReader("x=1")) out, err := inc(r, "/partials/ok") if err != nil || string(out) != "

ok

" { t.Fatalf("include ok = %q, %v", out, err) } if seenHX != "true" || seenMethod != http.MethodGet { t.Errorf("include must GET with HX-Request: true, got %s %q", seenMethod, seenHX) } if _, err := inc(r, "/partials/bad"); err == nil { t.Error("a non-200 include must error") } if _, err := inc(r, "/partials/missing"); err == nil { t.Error("an unregistered include must error") } notice := IncludeOr(inc, nil, r, "/partials/bad", "your ") if !strings.Contains(string(notice), `alert alert-danger`) || !strings.Contains(string(notice), "Could not load your <plans>.") { t.Errorf("IncludeOr must render an escaped inline notice, got %q", notice) } if IncludeOr(nil, nil, r, "/partials/ok", "x") != "" { t.Error("a nil Include renders nothing") } }