Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestBuildPageConsoleDataFiltersCommonSessionFields(t *testing.T) {
|
||||
payload := buildPageConsoleData("/detail/example", http.StatusOK, "detail.html", gin.H{
|
||||
"title": "示例短剧",
|
||||
"detail": gin.H{"id": 12, "slug": "example"},
|
||||
"appConfig": "public config",
|
||||
"headerNav": "navigation",
|
||||
"footer": "footer data",
|
||||
"isLoggedIn": true,
|
||||
"email": "viewer@example.com",
|
||||
"pageConsoleData": "old payload",
|
||||
})
|
||||
|
||||
if payload["path"] != "/detail/example" || payload["status"] != http.StatusOK || payload["template"] != "detail.html" {
|
||||
t.Fatalf("unexpected page console metadata: %#v", payload)
|
||||
}
|
||||
pageData, ok := payload["data"].(gin.H)
|
||||
if !ok {
|
||||
t.Fatalf("payload data type = %T, want gin.H", payload["data"])
|
||||
}
|
||||
if _, ok := pageData["detail"]; !ok {
|
||||
t.Fatalf("page response data was removed: %#v", pageData)
|
||||
}
|
||||
if payload["headerNav"] != "navigation" || payload["footer"] != "footer data" {
|
||||
t.Fatalf("common navigation/footer data missing: %#v", payload)
|
||||
}
|
||||
for _, key := range []string{"appConfig", "headerNav", "isLoggedIn", "email", "pageConsoleData"} {
|
||||
if _, ok := pageData[key]; ok {
|
||||
t.Fatalf("excluded key %q remains in console data: %#v", key, pageData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetNoIndexAndPageNoIndex(t *testing.T) {
|
||||
data := gin.H{}
|
||||
setNoIndex(data, false)
|
||||
if data["robots"] != "noindex,nofollow" || !pageNoIndex(data) {
|
||||
t.Fatalf("noindex policy was not applied: %#v", data)
|
||||
}
|
||||
|
||||
if pageNoIndex(gin.H{"robots": "index,follow"}) {
|
||||
t.Fatal("indexable robots value was treated as noindex")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRichtextIndexPolicy(t *testing.T) {
|
||||
publicData := gin.H{}
|
||||
applyRichtextIndexPolicy(publicData, "关于我们")
|
||||
if pageNoIndex(publicData) {
|
||||
t.Fatal("about page should remain indexable")
|
||||
}
|
||||
|
||||
legalData := gin.H{}
|
||||
applyRichtextIndexPolicy(legalData, "隐私政策")
|
||||
if !pageNoIndex(legalData) || legalData["robots"] != "noindex,follow" {
|
||||
t.Fatalf("privacy page policy = %#v, want noindex,follow", legalData)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user