Files
member-console/internal/server/compose_test.go
T
cgalo5758 88db730fcc Add dual licensing and SPDX headers
Introduce a commercial license option alongside AGPL-3.0-only, require a
CLA for contributors, and document the terms in COMMERCIAL.md and
NOTICE. Add a script to stamp SPDX headers on Go files and apply it
across the tree.
2026-09-06 02:29:42 -05:00

53 lines
1.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
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("<p>ok</p>"))
})
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) != "<p>ok</p>" {
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 <plans>")
if !strings.Contains(string(notice), `alert alert-danger`) || !strings.Contains(string(notice), "Could not load your &lt;plans&gt;.") {
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")
}
}