fix(qqbot): cancel non-OK direct-upload response body before throwing (#110008)

This commit is contained in:
Monkey-wusky
2026-07-20 18:54:12 -07:00
committed by GitHub
parent a510a8e3a5
commit 17643f3850
2 changed files with 9 additions and 3 deletions
@@ -39,14 +39,16 @@ function mockGuardedResponse(
body: BodyInit = MEDIA_BYTES,
init?: ResponseInit,
): {
response: Response;
release: ReturnType<typeof vi.fn>;
} {
const release = vi.fn(async () => {});
const response = new Response(body, init);
fetchWithSsrFGuardMock.mockResolvedValueOnce({
response: new Response(body, init),
response,
release,
});
return { release };
return { response, release };
}
function mockApiClient(): ApiClient {
@@ -428,7 +430,8 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
it("rejects HTTP errors from guarded direct-upload downloads before calling the QQ API", async () => {
fetchWithSsrFGuardMock.mockReset();
mockGuardedResponse("not found", { status: 404 });
const { response, release } = mockGuardedResponse("not found", { status: 404 });
const cancelSpy = vi.spyOn(response.body!, "cancel").mockResolvedValue(undefined);
const client = mockApiClient();
const tokenManager = mockTokenManager();
const api = new MediaApi(client, tokenManager);
@@ -443,6 +446,8 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
),
).rejects.toThrow("Direct-upload media URL returned HTTP 404");
expect(cancelSpy).toHaveBeenCalledOnce();
expect(release).toHaveBeenCalledOnce();
expect(tokenManager["getAccessToken"]).not.toHaveBeenCalled();
expect(client["request"]).not.toHaveBeenCalled();
});
+1
View File
@@ -161,6 +161,7 @@ export async function downloadDirectUploadUrl(
const { response, release } = await fetchDirectUploadDownload(parsed.toString());
try {
if (!response.ok) {
await response.body?.cancel().catch(() => undefined);
throw new Error(`Direct-upload media URL returned HTTP ${response.status}`);
}
return await readDirectUploadResponse(response, opts.maxBytes ?? MAX_UPLOAD_SIZE);