45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"nex/backend/internal/handler"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestSetupRoutes_VersionDoesNotFallback(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
r := gin.New()
|
|
setupRoutes(r, &handler.ProxyHandler{}, &handler.ProviderHandler{}, &handler.ModelHandler{}, &handler.StatsHandler{}, handler.NewVersionHandler(), handler.NewSettingsHandler(nil, "desktop", true, ""))
|
|
setupStaticFilesWithFS(r, fstest.MapFS{
|
|
"index.html": {Data: []byte("<html>fallback</html>")},
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/version", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
if contentType := w.Header().Get("Content-Type"); contentType == "text/html; charset=utf-8" {
|
|
t.Fatalf("版本接口不应返回 SPA fallback HTML")
|
|
}
|
|
|
|
var result map[string]string
|
|
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
|
t.Fatalf("解析响应失败: %v", err)
|
|
}
|
|
for _, key := range []string{"version", "commit", "build_time"} {
|
|
if result[key] == "" {
|
|
t.Fatalf("响应缺少 %s 字段: %#v", key, result)
|
|
}
|
|
}
|
|
}
|