



























@@ -62,6 +62,14 @@ function createGuardedResult(response: Response, finalUrl: string) {
6262};
6363}
646465+function guardedFetchCall(index: number): Parameters<typeof fetchWithSsrFGuard>[0] {
66+const call = mockGuardedFetch.mock.calls.at(index)?.at(0);
67+if (call === undefined) {
68+throw new Error(`expected guarded fetch call ${index}`);
69+}
70+return call;
71+}
72+6573describe("uploadFile memex upload hardening", () => {
6674beforeEach(() => {
6775vi.clearAllMocks();
@@ -123,7 +131,7 @@ describe("uploadFile memex upload hardening", () => {
123131expect(result).toEqual({ url: "https://memex.tlon.network/files/uploaded.png" });
124132expect(vi.mocked(globalThis.fetch)).not.toHaveBeenCalled();
125133expect(mockGuardedFetch).toHaveBeenCalledTimes(2);
126-const firstCall = mockGuardedFetch.mock.calls[0]?.[0];
134+const firstCall = guardedFetchCall(0);
127135expect(firstCall?.url).toBe("https://memex.tlon.network/v1/zod/upload");
128136expect(firstCall?.init?.method).toBe("PUT");
129137expect(firstCall?.init?.headers).toEqual({ "Content-Type": "application/json" });
@@ -137,7 +145,7 @@ describe("uploadFile memex upload hardening", () => {
137145expect(firstBody.contentLength).toBe(11);
138146expect(firstBody.contentType).toBe("image/png");
139147expect(typeof firstBody.fileName).toBe("string");
140-const secondCall = mockGuardedFetch.mock.calls[1]?.[0];
148+const secondCall = guardedFetchCall(1);
141149expect(secondCall?.url).toBe("https://uploads.tlon.network/put");
142150expect(secondCall?.init?.method).toBe("PUT");
143151expect(secondCall?.init?.headers).toEqual({
@@ -171,7 +179,7 @@ describe("uploadFile memex upload hardening", () => {
171179172180expect(vi.mocked(globalThis.fetch)).not.toHaveBeenCalled();
173181expect(mockGuardedFetch).toHaveBeenCalledTimes(2);
174-const uploadCall = mockGuardedFetch.mock.calls[1]?.[0];
182+const uploadCall = guardedFetchCall(1);
175183expect(uploadCall?.url).toBe("https://uploads.tlon.network/put");
176184expect(uploadCall?.auditContext).toBe("tlon-memex-upload");
177185expect(uploadCall?.capture).toBe(false);
@@ -272,7 +280,7 @@ describe("uploadFile memex upload hardening", () => {
272280273281expect(vi.mocked(globalThis.fetch)).not.toHaveBeenCalled();
274282expect(mockGuardedFetch).toHaveBeenCalledTimes(2);
275-const uploadCall = mockGuardedFetch.mock.calls[1]?.[0];
283+const uploadCall = guardedFetchCall(1);
276284expect(uploadCall?.url).toBe("https://uploads.tlon.network/put");
277285expect(uploadCall?.auditContext).toBe("tlon-memex-upload");
278286expect(uploadCall?.capture).toBe(false);
@@ -387,7 +395,7 @@ describe("uploadFile memex upload hardening", () => {
387395388396expect(vi.mocked(globalThis.fetch)).not.toHaveBeenCalled();
389397expect(mockGuardedFetch).toHaveBeenCalledTimes(1);
390-const lookupCall = mockGuardedFetch.mock.calls[0]?.[0];
398+const lookupCall = guardedFetchCall(0);
391399expect(lookupCall?.url).toBe("https://memex.tlon.network/v1/zod/upload");
392400expect(lookupCall?.auditContext).toBe("tlon-memex-upload-url");
393401expect(lookupCall?.capture).toBe(false);
@@ -455,7 +463,7 @@ describe("uploadFile custom S3 upload hardening", () => {
455463456464expect(result.url.startsWith("https://files.example.com/")).toBe(true);
457465expect(mockGuardedFetch).toHaveBeenCalledTimes(1);
458-const uploadCall = mockGuardedFetch.mock.calls[0]?.[0];
466+const uploadCall = guardedFetchCall(0);
459467expect(uploadCall?.url).toBe("https://s3.example.com/uploads/file?sig=abc");
460468expect(uploadCall?.init?.method).toBe("PUT");
461469expect(uploadCall?.init?.headers).toBeUndefined();
@@ -508,7 +516,7 @@ describe("uploadFile custom S3 upload hardening", () => {
508516509517expect(result.url.startsWith("https://files.example.com/")).toBe(true);
510518expect(mockGuardedFetch).toHaveBeenCalledTimes(1);
511-const uploadCall = mockGuardedFetch.mock.calls[0]?.[0];
519+const uploadCall = guardedFetchCall(0);
512520expect(uploadCall?.url).toBe("https://10.0.0.15/uploads/file?sig=abc");
513521expect(uploadCall?.auditContext).toBe("tlon-custom-s3-upload");
514522expect(uploadCall?.capture).toBe(false);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。