From c6d6411378836865508ae8c69ddbe56066e2df72 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 18 Feb 2026 12:58:35 +0000 Subject: [PATCH] test(media): dedupe redirect request fixtures --- src/media/store.redirect.test.ts | 66 ++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/media/store.redirect.test.ts b/src/media/store.redirect.test.ts index e236c4f903c..f007199b97c 100644 --- a/src/media/store.redirect.test.ts +++ b/src/media/store.redirect.test.ts @@ -11,6 +11,24 @@ import { saveMediaSource, setMediaStoreNetworkDepsForTest } from "./store.js"; const HOME = path.join(os.tmpdir(), "openclaw-home-redirect"); const mockRequest = vi.fn(); +function createMockHttpExchange() { + const res = Object.assign(new PassThrough(), { + statusCode: 0, + headers: {} as Record, + }); + const req = { + on: (event: string, handler: (...args: unknown[]) => void) => { + if (event === "error") { + res.on("error", handler); + } + return req; + }, + end: () => undefined, + destroy: () => res.destroy(), + } as const; + return { req, res }; +} + describe("media store redirects", () => { let envSnapshot: ReturnType; @@ -44,20 +62,7 @@ describe("media store redirects", () => { let call = 0; mockRequest.mockImplementation((_url, _opts, cb) => { call += 1; - const res = Object.assign(new PassThrough(), { - statusCode: 0, - headers: {} as Record, - }); - const req = { - on: (event: string, handler: (...args: unknown[]) => void) => { - if (event === "error") { - res.on("error", handler); - } - return req; - }, - end: () => undefined, - destroy: () => res.destroy(), - } as const; + const { req, res } = createMockHttpExchange(); if (call === 1) { res.statusCode = 302; @@ -89,20 +94,7 @@ describe("media store redirects", () => { it("sniffs xlsx from zip content when headers and url extension are missing", async () => { mockRequest.mockImplementationOnce((_url, _opts, cb) => { - const res = Object.assign(new PassThrough(), { - statusCode: 0, - headers: {} as Record, - }); - const req = { - on: (event: string, handler: (...args: unknown[]) => void) => { - if (event === "error") { - res.on("error", handler); - } - return req; - }, - end: () => undefined, - destroy: () => res.destroy(), - } as const; + const { req, res } = createMockHttpExchange(); res.statusCode = 200; res.headers = {}; @@ -134,4 +126,22 @@ describe("media store redirects", () => { ); expect(path.extname(saved.path)).toBe(".xlsx"); }); + + it("fails when redirect response omits location header", async () => { + mockRequest.mockImplementationOnce((_url, _opts, cb) => { + const { req, res } = createMockHttpExchange(); + res.statusCode = 302; + res.headers = {}; + setImmediate(() => { + cb(res as unknown); + res.end(); + }); + return req; + }); + + await expect(saveMediaSource("https://example.com/start")).rejects.toThrow( + "Redirect loop or missing Location header", + ); + expect(mockRequest).toHaveBeenCalledTimes(1); + }); });