package site
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func newTestRouter(t *testing.T) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
r := gin.New()
New().Register(r)
return r
}
func TestSitePageRender(t *testing.T) {
r := newTestRouter(t)
for _, path := range []string{"/", "/home", "/index.html"} {
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
if w.Code != http.StatusOK {
t.Fatalf("%s: expect 200, got %d", path, w.Code)
}
body := w.Body.String()
for _, want := range []string{
"
OpsxAPI", // 标题
`id="features"`, // 功能区
`id="pricing"`, // 定价区
"¥99", // 入门价
"¥8,000", // 企业档标价
"¥30,000", // 买断标价
"¥5,000–8,000", // 成交区间
"永久买断", // 买断 FAQ
"早鸟创始价", // 让利策略
"Socket.IO", // 协议覆盖
"hello@opsxapi.dev", // 联系方式
`href="/docs"`, // 文档互链
`href="/site/assets/site.css"`, // 样式资源
} {
if !strings.Contains(body, want) {
t.Fatalf("%s: missing %q", path, want)
}
}
}
}
func TestSiteAsset(t *testing.T) {
r := newTestRouter(t)
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/site/assets/site.css", nil))
if w.Code != http.StatusOK || !strings.Contains(w.Header().Get("Content-Type"), "text/css") {
t.Fatalf("css: %d %s", w.Code, w.Header().Get("Content-Type"))
}
w = httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/site/assets/../template.html", nil))
if w.Code != http.StatusBadRequest && w.Code != http.StatusNotFound {
t.Fatalf("path traversal should be rejected, got %d", w.Code)
}
}