Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestTrackerPageFromPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
key string
|
||||
name string
|
||||
}{
|
||||
{"/", "home", "首页"},
|
||||
{"/list/ai-short-play/2/page/3/", "list", "短剧列表"},
|
||||
{"/series/hot/page/2/", "series", "AI成人剧集"},
|
||||
{"/tag/family/page/2/", "tag_list", "标签详情"},
|
||||
{"/tag-list/family/", "tag_list", "标签详情"},
|
||||
{"/rank/2/page/3/", "rank", "排行榜"},
|
||||
{"/today-update/page/2/", "today_update", "今日更新"},
|
||||
{"/category/drama/page/2/", "category_detail", "分类详情"},
|
||||
{"/topic/page/2/", "theme_index", "专题列表"},
|
||||
{"/topic/sweet-love/page/2/", "topic", "专题详情"},
|
||||
{"/more/legacy-topic/", "topic", "专题详情"},
|
||||
{"/detail/my-playlet/", "detail", "短剧详情"},
|
||||
{"/play/my-playlet/42/", "play", "播放页"},
|
||||
{"/login/return/detail/foo/", "login", "登录"},
|
||||
{"/register/", "register", "注册"},
|
||||
{"/collect/chapter/page/2/", "collect", "我的收藏"},
|
||||
{"/search/测试/page/2/", "search", "搜索结果页"},
|
||||
{"/about-us/", "about_us", "关于我们"},
|
||||
{"/user-agreement/", "user_agreement", "用户协议"},
|
||||
{"/privacy-policy/", "privacy_policy", "隐私政策"},
|
||||
{"/help-center/", "help_center", "帮助中心"},
|
||||
{"/richtext/自定义内容/", "rich_text", "富文本页"},
|
||||
{"/dh/", "nav", "官方导航"},
|
||||
{"/unregistered-path/", "internal_other", "站内其他页面"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.path, func(t *testing.T) {
|
||||
key, name := trackerPageFromPath(tt.path)
|
||||
if key != tt.key || name != tt.name {
|
||||
t.Fatalf("trackerPageFromPath(%q) = (%q, %q), want (%q, %q)", tt.path, key, name, tt.key, tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackerPageFromRequestUsesResponseStatus(t *testing.T) {
|
||||
tests := []struct {
|
||||
status int
|
||||
key string
|
||||
name string
|
||||
}{
|
||||
{http.StatusNotFound, "not_found", "404页面"},
|
||||
{http.StatusInternalServerError, "server_error", "服务异常页面"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
key, name := trackerPageFromRequest("/missing/", tt.status)
|
||||
if key != tt.key || name != tt.name {
|
||||
t.Fatalf("trackerPageFromRequest(status=%d) = (%q, %q), want (%q, %q)", tt.status, key, name, tt.key, tt.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachTrackerPagePreservesExplicitPage(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/topic/custom/", nil)
|
||||
data := gin.H{"pageKey": "topic", "pageName": "自定义专题"}
|
||||
|
||||
attachTrackerPage(c, http.StatusOK, data)
|
||||
|
||||
if data["pageKey"] != "topic" || data["pageName"] != "自定义专题" {
|
||||
t.Fatalf("explicit tracker page was overwritten: %#v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachTrackerPageReplacesUnknownValues(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/play/demo/1/", nil)
|
||||
data := gin.H{"pageKey": "unknown", "pageName": "未知页面"}
|
||||
|
||||
attachTrackerPage(c, http.StatusOK, data)
|
||||
|
||||
if data["pageKey"] != "play" || data["pageName"] != "播放页" {
|
||||
t.Fatalf("unknown tracker page was not replaced: %#v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachTrackerPageMarksErrorResponse(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/topic/deleted/", nil)
|
||||
data := gin.H{"pageKey": "topic", "pageName": "已删除专题"}
|
||||
|
||||
attachTrackerPage(c, http.StatusNotFound, data)
|
||||
|
||||
if data["pageKey"] != "not_found" || data["pageName"] != "404页面" {
|
||||
t.Fatalf("error response retained business page mapping: %#v", data)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user