Files
tamabox/route/question/admin_backquery_test.go
2026-10-22 17:49:25 +08:00

57 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package question
import (
"net/http/httptest"
"testing"
"github.com/flamego/flamego"
"github.com/flamego/template"
"github.com/tamakyi/TamaBox/internal/context"
)
// 从后台进入单题页时的筛选参数会写进表单 action 的 query,POST 回来要靠
// from=admin 重新认出来源。丢了它,「回答并下一条」/「下一条未读」跳转后
// 页脚「返回」会掉回箱主页(实测过)。
func TestAdminBackQueryKeepsFrom(t *testing.T) {
cases := []struct {
name string
url string
want string
}{
{"非后台来源", "/answer?sort=new", ""},
{"后台来源无筛选", "/answer?from=admin", "from=admin"},
{"后台来源带筛选", "/answer?from=admin&sort=new&read=2", "from=admin&read=2&sort=new"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var got string
f := flamego.New()
f.Post("/answer", func(fc flamego.Context) {
ctx := context.Context{Context: fc, Data: template.Data{}}
got = adminBackQuery(ctx)
})
f.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", c.url, nil))
if got != c.want {
t.Fatalf("adminBackQuery(%q) = %q, want %q", c.url, got, c.want)
}
})
}
}
// 跳转地址必须带回完整的后台上下文,否则下一条页面认不出来源。
func TestQuestionPathKeepsAdminContext(t *testing.T) {
var got string
f := flamego.New()
f.Post("/next-unread", func(fc flamego.Context) {
ctx := context.Context{Context: fc, Data: template.Data{}}
got = questionPath(ctx, "box", 7)
})
f.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/next-unread?from=admin&sort=new", nil))
want := "/_/box/7?from=admin&sort=new"
if got != want {
t.Fatalf("questionPath() = %q, want %q", got, want)
}
}