diff --git a/src/backend/drivers/github_releases/util.test.ts b/src/backend/drivers/github_releases/util.test.ts new file mode 100644 index 00000000..0fba3d72 --- /dev/null +++ b/src/backend/drivers/github_releases/util.test.ts @@ -0,0 +1,37 @@ +import test from "node:test" +import assert from "node:assert/strict" +import { ClientGithubReleases } from "./util" + +function clientWith(ghProxy?: string): ClientGithubReleases { + return new ClientGithubReleases({ + repo_structure: "example/repo", + ...(ghProxy !== undefined ? { gh_proxy: ghProxy } : {}), + }) +} + +function proxyOf(additionGhProxy: string | undefined, url: string): string { + const client = clientWith(additionGhProxy) + // proxy() 为私有方法,测试中通过索引访问 + return (client as unknown as { proxy: (u: string) => string }).proxy(url) +} + +test("gh_proxy 已配置时为 http(s) 下载链接加代理前缀", () => { + const url = "https://github.com/example/repo/releases/download/v1.0.0/app.zip" + assert.equal( + proxyOf("https://mirror.example.com", url), + "https://mirror.example.com" + url, + ) +}) + +test("gh_proxy 未配置时保持原链接不变", () => { + const url = "https://github.com/example/repo/releases/download/v1.0.0/app.zip" + assert.equal(proxyOf(undefined, url), url) + assert.equal(proxyOf("", url), url) +}) + +test("gh_proxy 已配置但链接非 http 开头时不加前缀(对齐上游 Go 版语义)", () => { + assert.equal( + proxyOf("https://mirror.example.com", "/relative/path"), + "/relative/path", + ) +}) diff --git a/src/backend/drivers/github_releases/util.ts b/src/backend/drivers/github_releases/util.ts index d3c9ade6..2945ff50 100644 --- a/src/backend/drivers/github_releases/util.ts +++ b/src/backend/drivers/github_releases/util.ts @@ -61,8 +61,8 @@ export class ClientGithubReleases { private proxy(url: string): string { const p = this.addition.gh_proxy || "" - if (!p || url.startsWith("http")) return url - return p + url + if (p && url.startsWith("http")) return p + url + return url } /** 解析挂载点对应的 org/repo */