Skip to content

Commit 017c0dc

Browse files
committed
test: dedupe msteams attachment redirects
1 parent 0229246 commit 017c0dc

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

extensions/msteams/src/attachments.test.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,17 @@ function isUrlAllowedBySsrfPolicy(url: string, policy?: SsrFPolicy): boolean {
8888
);
8989
}
9090

91-
const fetchRemoteMediaMock = vi.fn(async (params: RemoteMediaFetchParams) => {
91+
async function fetchRemoteMediaWithRedirects(
92+
params: RemoteMediaFetchParams,
93+
requestInit?: RequestInit,
94+
) {
9295
const fetchFn = params.fetchImpl ?? fetch;
9396
let currentUrl = params.url;
9497
for (let i = 0; i <= MAX_REDIRECT_HOPS; i += 1) {
9598
if (!isUrlAllowedBySsrfPolicy(currentUrl, params.ssrfPolicy)) {
9699
throw new Error(`Blocked hostname (not in allowlist): ${currentUrl}`);
97100
}
98-
const res = await fetchFn(currentUrl, { redirect: "manual" });
101+
const res = await fetchFn(currentUrl, { redirect: "manual", ...requestInit });
99102
if (REDIRECT_STATUS_CODES.includes(res.status)) {
100103
const location = res.headers.get("location");
101104
if (!location) {
@@ -107,6 +110,10 @@ const fetchRemoteMediaMock = vi.fn(async (params: RemoteMediaFetchParams) => {
107110
return readRemoteMediaResponse(res, params);
108111
}
109112
throw new Error("too many redirects");
113+
}
114+
115+
const fetchRemoteMediaMock = vi.fn(async (params: RemoteMediaFetchParams) => {
116+
return await fetchRemoteMediaWithRedirects(params);
110117
});
111118

112119
const runtimeStub: PluginRuntime = createPluginRuntimeMock({
@@ -720,24 +727,9 @@ describe("msteams attachments", () => {
720727
});
721728

722729
fetchRemoteMediaMock.mockImplementationOnce(async (params) => {
723-
const fetchFn = params.fetchImpl ?? fetch;
724-
let currentUrl = params.url;
725-
for (let i = 0; i < MAX_REDIRECT_HOPS; i += 1) {
726-
const res = await fetchFn(currentUrl, {
727-
redirect: "manual",
728-
dispatcher: {},
729-
} as RequestInit);
730-
if (REDIRECT_STATUS_CODES.includes(res.status)) {
731-
const location = res.headers.get("location");
732-
if (!location) {
733-
throw new Error("redirect missing location");
734-
}
735-
currentUrl = new URL(location, currentUrl).toString();
736-
continue;
737-
}
738-
return readRemoteMediaResponse(res, params);
739-
}
740-
throw new Error("too many redirects");
730+
return await fetchRemoteMediaWithRedirects(params, {
731+
dispatcher: {},
732+
} as RequestInit);
741733
});
742734

743735
const media = await downloadAttachmentsWithFetch(

0 commit comments

Comments
 (0)