mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 07:19:33 +08:00
新增单元测试验证图片重定向逻辑,优化前端预览链接样式,调整依赖配置
This commit is contained in:
@@ -172,9 +172,11 @@ func handleImageResponse(c *gin.Context, img *model.Image) {
|
||||
if mode == "redirect" {
|
||||
if selected.PublicURL != "" {
|
||||
c.Redirect(http.StatusFound, selected.PublicURL)
|
||||
} else if img.URLBase != "" {
|
||||
// 兜底重定向到原始 Bing
|
||||
bingURL := fmt.Sprintf("https://www.bing.com%s_%s.jpg", img.URLBase, selected.Variant)
|
||||
c.Redirect(http.StatusFound, bingURL)
|
||||
} else {
|
||||
// 兜底重定向到原始 Bing (如果可能,但由于 URLBase 只有一部分,这里可能不工作)
|
||||
// 这里我们更倾向于 local 转发,如果 PublicURL 为空
|
||||
serveLocal(c, selected.StorageKey)
|
||||
}
|
||||
} else {
|
||||
@@ -201,7 +203,9 @@ func formatMeta(img *model.Image) gin.H {
|
||||
variants := []gin.H{}
|
||||
for _, v := range img.Variants {
|
||||
url := v.PublicURL
|
||||
if cfg.API.Mode == "local" || url == "" {
|
||||
if url == "" && cfg.API.Mode == "redirect" && img.URLBase != "" {
|
||||
url = fmt.Sprintf("https://www.bing.com%s_%s.jpg", img.URLBase, v.Variant)
|
||||
} else if cfg.API.Mode == "local" || url == "" {
|
||||
url = fmt.Sprintf("%s/api/v1/image/date/%s?variant=%s&format=%s", cfg.Server.BaseURL, img.Date, v.Variant, v.Format)
|
||||
}
|
||||
variants = append(variants, gin.H{
|
||||
|
||||
68
internal/http/handlers/image_test.go
Normal file
68
internal/http/handlers/image_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"BingPaper/internal/config"
|
||||
"BingPaper/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestHandleImageResponseRedirect(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Setup config
|
||||
err := config.Init("")
|
||||
assert.NoError(t, err)
|
||||
config.GetConfig().API.Mode = "redirect"
|
||||
|
||||
// Mock Image and Variant
|
||||
img := &model.Image{
|
||||
Date: "2026-01-26",
|
||||
URLBase: "/th?id=OHR.TestImage",
|
||||
Variants: []model.ImageVariant{
|
||||
{
|
||||
Variant: "UHD",
|
||||
Format: "jpg",
|
||||
PublicURL: "", // Empty for local storage simulation
|
||||
StorageKey: "2026-01-26/2026-01-26_UHD.jpg",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("Redirect mode with empty PublicURL should redirect to Bing", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request, _ = http.NewRequest("GET", "/api/v1/image/today?variant=UHD", nil)
|
||||
|
||||
handleImageResponse(c, img)
|
||||
|
||||
assert.Equal(t, http.StatusFound, w.Code)
|
||||
assert.Contains(t, w.Header().Get("Location"), "bing.com")
|
||||
assert.Contains(t, w.Header().Get("Location"), "UHD")
|
||||
})
|
||||
|
||||
t.Run("FormatMeta in redirect mode should return Bing URL if PublicURL is empty", func(t *testing.T) {
|
||||
config.GetConfig().API.Mode = "redirect"
|
||||
meta := formatMeta(img)
|
||||
|
||||
variants := meta["variants"].([]gin.H)
|
||||
assert.Equal(t, 1, len(variants))
|
||||
assert.Contains(t, variants[0]["url"].(string), "bing.com")
|
||||
assert.Contains(t, variants[0]["url"].(string), "UHD")
|
||||
})
|
||||
|
||||
t.Run("FormatMeta in local mode should return API URL", func(t *testing.T) {
|
||||
config.GetConfig().API.Mode = "local"
|
||||
config.GetConfig().Server.BaseURL = "http://myserver.com"
|
||||
meta := formatMeta(img)
|
||||
|
||||
variants := meta["variants"].([]gin.H)
|
||||
assert.Equal(t, 1, len(variants))
|
||||
assert.Contains(t, variants[0]["url"].(string), "myserver.com")
|
||||
assert.Contains(t, variants[0]["url"].(string), "/api/v1/image/date/")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user