|
1 | 1 | // Msteams tests cover file consent plugin behavior. |
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
3 | 3 | import { uploadToConsentUrl } from "./file-consent.js"; |
| 4 | +import { resolveMSTeamsSharePointUploadTimeoutMs } from "./request-timeout.js"; |
4 | 5 | import { buildUserAgent } from "./user-agent.js"; |
5 | 6 |
|
6 | 7 | // Helper: a resolveFn that returns a public IP by default |
@@ -321,6 +322,132 @@ describe("uploadToConsentUrl", () => { |
321 | 322 | expect(opts?.body).toEqual(new Uint8Array(Buffer.from("hello"))); |
322 | 323 | }); |
323 | 324 |
|
| 325 | + it("aborts consent uploads that do not finish before the request timeout", async () => { |
| 326 | + vi.useFakeTimers(); |
| 327 | + try { |
| 328 | + let observedSignal: AbortSignal | undefined; |
| 329 | + const fetchFn = vi.fn<typeof fetch>( |
| 330 | + async (_url, init) => |
| 331 | + await new Promise<Response>((_resolve, reject) => { |
| 332 | + observedSignal = init?.signal ?? undefined; |
| 333 | + observedSignal?.addEventListener( |
| 334 | + "abort", |
| 335 | + () => { |
| 336 | + const reason = observedSignal?.reason; |
| 337 | + reject(reason instanceof Error ? reason : new Error("request aborted")); |
| 338 | + }, |
| 339 | + { once: true }, |
| 340 | + ); |
| 341 | + }), |
| 342 | + ); |
| 343 | + |
| 344 | + const uploadPromise = uploadToConsentUrl({ |
| 345 | + url: "https://contoso.sharepoint.com/upload", |
| 346 | + buffer: Buffer.from("hello"), |
| 347 | + fetchFn, |
| 348 | + timeoutMs: 25, |
| 349 | + validationOpts: { resolveFn: publicResolve }, |
| 350 | + }); |
| 351 | + |
| 352 | + await vi.advanceTimersByTimeAsync(0); |
| 353 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 354 | + expect(observedSignal?.aborted).toBe(false); |
| 355 | + const uploadRejection = expect(uploadPromise).rejects.toMatchObject({ |
| 356 | + name: "TimeoutError", |
| 357 | + message: "request timed out", |
| 358 | + }); |
| 359 | + |
| 360 | + await vi.advanceTimersByTimeAsync(25); |
| 361 | + await uploadRejection; |
| 362 | + expect(observedSignal?.aborted).toBe(true); |
| 363 | + } finally { |
| 364 | + vi.useRealTimers(); |
| 365 | + } |
| 366 | + }); |
| 367 | + |
| 368 | + it("allows consent uploads that complete before the request timeout", async () => { |
| 369 | + vi.useFakeTimers(); |
| 370 | + try { |
| 371 | + let observedSignal: AbortSignal | undefined; |
| 372 | + const fetchFn = vi.fn<typeof fetch>( |
| 373 | + async (_url, init) => |
| 374 | + await new Promise<Response>((resolve, reject) => { |
| 375 | + observedSignal = init?.signal ?? undefined; |
| 376 | + observedSignal?.addEventListener( |
| 377 | + "abort", |
| 378 | + () => reject(new DOMException("consent upload timed out", "AbortError")), |
| 379 | + { once: true }, |
| 380 | + ); |
| 381 | + setTimeout(() => resolve(new Response(null, { status: 200 })), 25); |
| 382 | + }), |
| 383 | + ); |
| 384 | + |
| 385 | + const uploadPromise = uploadToConsentUrl({ |
| 386 | + url: "https://contoso.sharepoint.com/upload", |
| 387 | + buffer: Buffer.from("hello"), |
| 388 | + fetchFn, |
| 389 | + timeoutMs: 50, |
| 390 | + validationOpts: { resolveFn: publicResolve }, |
| 391 | + }); |
| 392 | + |
| 393 | + await vi.advanceTimersByTimeAsync(0); |
| 394 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 395 | + expect(observedSignal?.aborted).toBe(false); |
| 396 | + |
| 397 | + await vi.advanceTimersByTimeAsync(25); |
| 398 | + await expect(uploadPromise).resolves.toBeUndefined(); |
| 399 | + expect(observedSignal?.aborted).toBe(false); |
| 400 | + |
| 401 | + await vi.advanceTimersByTimeAsync(25); |
| 402 | + expect(observedSignal?.aborted).toBe(false); |
| 403 | + } finally { |
| 404 | + vi.useRealTimers(); |
| 405 | + } |
| 406 | + }); |
| 407 | + |
| 408 | + it("allows size-budgeted consent uploads that complete after the base upload deadline", async () => { |
| 409 | + vi.useFakeTimers(); |
| 410 | + try { |
| 411 | + const buffer = Buffer.alloc(512 * 1024); |
| 412 | + const resolvedTimeoutMs = resolveMSTeamsSharePointUploadTimeoutMs(buffer.length); |
| 413 | + const baseTimeoutMs = resolveMSTeamsSharePointUploadTimeoutMs(0); |
| 414 | + const completionMs = baseTimeoutMs + 1_000; |
| 415 | + let observedSignal: AbortSignal | undefined; |
| 416 | + const fetchFn = vi.fn<typeof fetch>( |
| 417 | + async (_url, init) => |
| 418 | + await new Promise<Response>((resolve, reject) => { |
| 419 | + observedSignal = init?.signal ?? undefined; |
| 420 | + observedSignal?.addEventListener( |
| 421 | + "abort", |
| 422 | + () => reject(new DOMException("consent upload timed out", "AbortError")), |
| 423 | + { once: true }, |
| 424 | + ); |
| 425 | + setTimeout(() => resolve(new Response(null, { status: 200 })), completionMs); |
| 426 | + }), |
| 427 | + ); |
| 428 | + |
| 429 | + expect(completionMs).toBeGreaterThan(baseTimeoutMs); |
| 430 | + expect(completionMs).toBeLessThan(resolvedTimeoutMs); |
| 431 | + |
| 432 | + const uploadPromise = uploadToConsentUrl({ |
| 433 | + url: "https://contoso.sharepoint.com/upload", |
| 434 | + buffer, |
| 435 | + fetchFn, |
| 436 | + validationOpts: { resolveFn: publicResolve }, |
| 437 | + }); |
| 438 | + |
| 439 | + await vi.advanceTimersByTimeAsync(0); |
| 440 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 441 | + expect(observedSignal?.aborted).toBe(false); |
| 442 | + |
| 443 | + await vi.advanceTimersByTimeAsync(completionMs); |
| 444 | + await expect(uploadPromise).resolves.toBeUndefined(); |
| 445 | + expect(observedSignal?.aborted).toBe(false); |
| 446 | + } finally { |
| 447 | + vi.useRealTimers(); |
| 448 | + } |
| 449 | + }); |
| 450 | + |
324 | 451 | it("blocks upload to a disallowed host", async () => { |
325 | 452 | const mockFetch = vi.fn(); |
326 | 453 | await expect( |
@@ -364,15 +491,15 @@ describe("uploadToConsentUrl", () => { |
364 | 491 | expect(mockFetch).toHaveBeenCalledOnce(); |
365 | 492 | const [url, opts] = firstFetchCall(mockFetch); |
366 | 493 | expect(url).toBe("https://contoso.sharepoint.com/sites/uploads/file.pdf"); |
367 | | - expect(opts).toEqual({ |
368 | | - method: "PUT", |
369 | | - headers: { |
370 | | - "User-Agent": buildUserAgent(), |
371 | | - "Content-Type": "application/pdf", |
372 | | - "Content-Range": "bytes 0-11/12", |
373 | | - }, |
374 | | - body: new Uint8Array(buffer), |
| 494 | + expect(opts?.method).toBe("PUT"); |
| 495 | + expect(opts?.headers).toEqual({ |
| 496 | + "User-Agent": buildUserAgent(), |
| 497 | + "Content-Type": "application/pdf", |
| 498 | + "Content-Range": "bytes 0-11/12", |
375 | 499 | }); |
| 500 | + expect(opts?.body).toEqual(new Uint8Array(buffer)); |
| 501 | + expect(opts?.signal).toBeInstanceOf(AbortSignal); |
| 502 | + expect(opts?.signal?.aborted).toBe(false); |
376 | 503 | }); |
377 | 504 |
|
378 | 505 | it("throws on non-OK response after passing validation", async () => { |
|
0 commit comments