mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-14 19:40:40 +00:00
test: share msteams safe fetch assertions
This commit is contained in:
@@ -31,6 +31,23 @@ function mockFetchWithRedirect(redirectMap: Record<string, string>, finalBody =
|
||||
});
|
||||
}
|
||||
|
||||
async function expectSafeFetchStatus(params: {
|
||||
fetchMock: ReturnType<typeof vi.fn>;
|
||||
url: string;
|
||||
allowHosts: string[];
|
||||
expectedStatus: number;
|
||||
resolveFn?: typeof publicResolve;
|
||||
}) {
|
||||
const res = await safeFetch({
|
||||
url: params.url,
|
||||
allowHosts: params.allowHosts,
|
||||
fetchFn: params.fetchMock as unknown as typeof fetch,
|
||||
resolveFn: params.resolveFn ?? publicResolve,
|
||||
});
|
||||
expect(res.status).toBe(params.expectedStatus);
|
||||
return res;
|
||||
}
|
||||
|
||||
describe("msteams attachment allowlists", () => {
|
||||
it("normalizes wildcard host lists", () => {
|
||||
expect(resolveAllowedHosts(["*", "graph.microsoft.com"])).toEqual(["*"]);
|
||||
@@ -121,13 +138,12 @@ describe("safeFetch", () => {
|
||||
const fetchMock = vi.fn(async (_url: string, _init?: RequestInit) => {
|
||||
return new Response("ok", { status: 200 });
|
||||
});
|
||||
const res = await safeFetch({
|
||||
await expectSafeFetchStatus({
|
||||
fetchMock,
|
||||
url: "https://teams.sharepoint.com/file.pdf",
|
||||
allowHosts: ["sharepoint.com"],
|
||||
fetchFn: fetchMock as unknown as typeof fetch,
|
||||
resolveFn: publicResolve,
|
||||
expectedStatus: 200,
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(fetchMock).toHaveBeenCalledOnce();
|
||||
// Should have used redirect: "manual"
|
||||
expect(fetchMock.mock.calls[0][1]).toHaveProperty("redirect", "manual");
|
||||
@@ -137,13 +153,12 @@ describe("safeFetch", () => {
|
||||
const fetchMock = mockFetchWithRedirect({
|
||||
"https://teams.sharepoint.com/file.pdf": "https://cdn.sharepoint.com/storage/file.pdf",
|
||||
});
|
||||
const res = await safeFetch({
|
||||
await expectSafeFetchStatus({
|
||||
fetchMock,
|
||||
url: "https://teams.sharepoint.com/file.pdf",
|
||||
allowHosts: ["sharepoint.com"],
|
||||
fetchFn: fetchMock as unknown as typeof fetch,
|
||||
resolveFn: publicResolve,
|
||||
expectedStatus: 200,
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user