test(media): dedupe server fixture helpers and cover 404/id validation
This commit is contained in:
@@ -35,6 +35,16 @@ describe("media server", () => {
|
||||
let server: Awaited<ReturnType<typeof startMediaServer>>;
|
||||
let port = 0;
|
||||
|
||||
function mediaUrl(id: string) {
|
||||
return `http://127.0.0.1:${port}/media/${id}`;
|
||||
}
|
||||
|
||||
async function writeMediaFile(id: string, contents: string) {
|
||||
const filePath = path.join(MEDIA_DIR, id);
|
||||
await fs.writeFile(filePath, contents);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
MEDIA_DIR = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
|
||||
server = await startMediaServer(0, 1_000);
|
||||
@@ -48,20 +58,18 @@ describe("media server", () => {
|
||||
});
|
||||
|
||||
it("serves media and cleans up after send", async () => {
|
||||
const file = path.join(MEDIA_DIR, "file1");
|
||||
await fs.writeFile(file, "hello");
|
||||
const res = await fetch(`http://127.0.0.1:${port}/media/file1`);
|
||||
const file = await writeMediaFile("file1", "hello");
|
||||
const res = await fetch(mediaUrl("file1"));
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("hello");
|
||||
await waitForFileRemoval(file);
|
||||
});
|
||||
|
||||
it("expires old media", async () => {
|
||||
const file = path.join(MEDIA_DIR, "old");
|
||||
await fs.writeFile(file, "stale");
|
||||
const file = await writeMediaFile("old", "stale");
|
||||
const past = Date.now() - 10_000;
|
||||
await fs.utimes(file, past / 1000, past / 1000);
|
||||
const res = await fetch(`http://127.0.0.1:${port}/media/old`);
|
||||
const res = await fetch(mediaUrl("old"));
|
||||
expect(res.status).toBe(410);
|
||||
await expect(fs.stat(file)).rejects.toThrow();
|
||||
});
|
||||
@@ -75,8 +83,7 @@ describe("media server", () => {
|
||||
testName: "rejects invalid media ids",
|
||||
mediaPath: "invalid%20id",
|
||||
setup: async () => {
|
||||
const file = path.join(MEDIA_DIR, "file2");
|
||||
await fs.writeFile(file, "hello");
|
||||
await writeMediaFile("file2", "hello");
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -90,17 +97,33 @@ describe("media server", () => {
|
||||
},
|
||||
] as const)("$testName", async (testCase) => {
|
||||
await testCase.setup?.();
|
||||
const res = await fetch(`http://127.0.0.1:${port}/media/${testCase.mediaPath}`);
|
||||
const res = await fetch(mediaUrl(testCase.mediaPath));
|
||||
expect(res.status).toBe(400);
|
||||
expect(await res.text()).toBe("invalid path");
|
||||
});
|
||||
|
||||
it("rejects oversized media files", async () => {
|
||||
const file = path.join(MEDIA_DIR, "big");
|
||||
await fs.writeFile(file, "");
|
||||
const file = await writeMediaFile("big", "");
|
||||
await fs.truncate(file, MEDIA_MAX_BYTES + 1);
|
||||
const res = await fetch(`http://127.0.0.1:${port}/media/big`);
|
||||
const res = await fetch(mediaUrl("big"));
|
||||
expect(res.status).toBe(413);
|
||||
expect(await res.text()).toBe("too large");
|
||||
});
|
||||
|
||||
it("returns not found for missing media IDs", async () => {
|
||||
const res = await fetch(mediaUrl("missing-file"));
|
||||
expect(res.status).toBe(404);
|
||||
expect(await res.text()).toBe("not found");
|
||||
});
|
||||
|
||||
it("returns 404 when route param is missing (dot path)", async () => {
|
||||
const res = await fetch(mediaUrl("."));
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it("rejects overlong media id", async () => {
|
||||
const res = await fetch(mediaUrl(`${"a".repeat(201)}.txt`));
|
||||
expect(res.status).toBe(400);
|
||||
expect(await res.text()).toBe("invalid path");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user