From 0e16f4b8254e4f10e661adbef64fc9c438f11a26 Mon Sep 17 00:00:00 2001 From: Xinhao Chen <123888078+blablabiu@users.noreply.github.com> Date: Mon, 9 Mar 2026 14:05:11 +0800 Subject: [PATCH] Codex/fix flaky anti hotlink test (#550) * test: remove network dependency from anti-hotlink test * test: add optional external anti-hotlink integration test --------- Co-authored-by: cxh --- pkg/downloader/images_test.go | 65 ++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/pkg/downloader/images_test.go b/pkg/downloader/images_test.go index 4c95cb4..b263d0a 100644 --- a/pkg/downloader/images_test.go +++ b/pkg/downloader/images_test.go @@ -1,6 +1,10 @@ package downloader import ( + "encoding/base64" + "fmt" + "net/http" + "net/http/httptest" "os" "path/filepath" "strings" @@ -102,9 +106,57 @@ func TestImageDownloader_generateFileName(t *testing.T) { } // TestDownloadImage_AntiHotlink 测试下载防盗链图片 -// 验证 PR #412 的修改:添加 User-Agent 和 Referer 解决 403 问题 +// 验证添加 User-Agent 和 Referer 解决 403 问题 func TestDownloadImage_AntiHotlink(t *testing.T) { - // 快科技的图片,需要 User-Agent 才能下载 + // 1x1 透明 PNG,避免依赖外部网络资源导致测试不稳定 + const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO7+2X8AAAAASUVORK5CYII=" + pngData, err := base64.StdEncoding.DecodeString(pngBase64) + if err != nil { + t.Fatalf("解析测试图片失败: %v", err) + } + + var server *httptest.Server + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if got := r.Header.Get("User-Agent"); got == "" { + http.Error(w, "missing user-agent", http.StatusForbidden) + return + } + + expectedReferer := fmt.Sprintf("%s/", server.URL) + if got := r.Header.Get("Referer"); got != expectedReferer { + http.Error(w, "invalid referer", http.StatusForbidden) + return + } + + w.Header().Set("Content-Type", "image/png") + _, _ = w.Write(pngData) + })) + defer server.Close() + + tempDir := t.TempDir() + downloader := NewImageDownloader(tempDir) + + filePath, err := downloader.DownloadImage(server.URL + "/image.png") + if err != nil { + t.Fatalf("下载失败: %v", err) + } + + info, err := os.Stat(filePath) + if err != nil { + t.Fatalf("文件不存在: %v", err) + } + if info.Size() == 0 { + t.Fatalf("下载文件为空") + } +} + +// TestDownloadImage_AntiHotlink_External 集成测试:真实外网防盗链场景 +// 默认跳过,设置 XHS_RUN_NETWORK_TESTS=1 后执行。 +func TestDownloadImage_AntiHotlink_External(t *testing.T) { + if os.Getenv("XHS_RUN_NETWORK_TESTS") != "1" { + t.Skip("skip external network test; set XHS_RUN_NETWORK_TESTS=1 to enable") + } + testURL := "https://img1.mydrivers.com/img/20260213/s_fdac2d21214147019e629fa7f2c8802e.png" tempDir := t.TempDir() @@ -115,16 +167,11 @@ func TestDownloadImage_AntiHotlink(t *testing.T) { t.Fatalf("下载失败: %v", err) } - // 验证文件存在 info, err := os.Stat(filePath) if err != nil { t.Fatalf("文件不存在: %v", err) } - - // 验证文件大小合理(大于 1KB) - if info.Size() < 1024 { - t.Errorf("文件太小,可能下载失败: %d bytes", info.Size()) + if info.Size() == 0 { + t.Fatalf("下载文件为空") } - - t.Logf("下载成功: %s (%d bytes)", filePath, info.Size()) }