Skip to content

Commit 364d67a

Browse files
fix(msteams): release all consent upload responses
Cancel unread response bodies after successful uploads as well as failed uploads, and fold the lifecycle assertions into the existing status-path tests. Co-authored-by: Monkey-wusky <[email protected]>
1 parent 892f5a5 commit 364d67a

2 files changed

Lines changed: 14 additions & 23 deletions

File tree

‎extensions/msteams/src/file-consent.test.ts‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ const firstFetchCall = (fetchFn: ReturnType<typeof vi.fn<typeof fetch>>) => {
5151
return call;
5252
};
5353

54+
const responseWithCancel = (status: number, statusText?: string) => {
55+
const cancel = vi.fn();
56+
const body = new ReadableStream<Uint8Array>({ cancel });
57+
return { response: new Response(body, { status, statusText }), cancel };
58+
};
59+
5460
// ─── isPrivateOrReservedIP ───────────────────────────────────────────────────
5561

5662
describe("isPrivateOrReservedIP", () => {
@@ -477,7 +483,8 @@ describe("uploadToConsentUrl", () => {
477483
});
478484

479485
it("allows upload to a valid SharePoint URL and performs PUT", async () => {
480-
const mockFetch = vi.fn<typeof fetch>(async () => new Response(null, { status: 200 }));
486+
const { response, cancel } = responseWithCancel(200);
487+
const mockFetch = vi.fn<typeof fetch>(async () => response);
481488
const buffer = Buffer.from("file content");
482489

483490
await uploadToConsentUrl({
@@ -500,14 +507,12 @@ describe("uploadToConsentUrl", () => {
500507
expect(opts?.body).toEqual(new Uint8Array(buffer));
501508
expect(opts?.signal).toBeInstanceOf(AbortSignal);
502509
expect(opts?.signal?.aborted).toBe(false);
510+
expect(cancel).toHaveBeenCalledOnce();
503511
});
504512

505513
it("throws on non-OK response after passing validation", async () => {
506-
const mockFetch = vi.fn().mockResolvedValue({
507-
ok: false,
508-
status: 403,
509-
statusText: "Forbidden",
510-
});
514+
const { response, cancel } = responseWithCancel(403, "Forbidden");
515+
const mockFetch = vi.fn<typeof fetch>(async () => response);
511516

512517
await expect(
513518
uploadToConsentUrl({
@@ -517,22 +522,6 @@ describe("uploadToConsentUrl", () => {
517522
validationOpts: { resolveFn: publicResolve },
518523
}),
519524
).rejects.toThrow("File upload to consent URL failed: 403 Forbidden");
520-
});
521-
522-
it("cancels a non-OK consent upload response body before throwing", async () => {
523-
const response = new Response("upload rejected", { status: 403, statusText: "Forbidden" });
524-
const cancel = vi.spyOn(response.body!, "cancel").mockResolvedValue(undefined);
525-
const fetchFn = vi.fn<typeof fetch>(async () => response);
526-
527-
await expect(
528-
uploadToConsentUrl({
529-
url: "https://contoso.sharepoint.com/sites/uploads/file.pdf",
530-
buffer: Buffer.from("data"),
531-
fetchFn,
532-
validationOpts: { resolveFn: publicResolve },
533-
}),
534-
).rejects.toThrow("File upload to consent URL failed: 403 Forbidden");
535-
536525
expect(cancel).toHaveBeenCalledOnce();
537526
});
538527

‎extensions/msteams/src/file-consent.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ export async function uploadToConsentUrl(params: {
225225
fetchFn,
226226
);
227227

228+
// Consent uploads never consume the response payload. Cancel it on every
229+
// status so the fetch implementation can release the underlying connection.
230+
await res.body?.cancel().catch(() => undefined);
228231
if (!res.ok) {
229-
await res.body?.cancel().catch(() => undefined);
230232
throw new Error(`File upload to consent URL failed: ${res.status} ${res.statusText}`);
231233
}
232234
}

0 commit comments

Comments
 (0)