You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
import { describe, expect, test } from "bun:test";
|
|
import { extractMediaUrlsFromMarkdown, publicAssetUrlToStorageKey } from "./post-media-urls";
|
|
|
|
describe("extractMediaUrlsFromMarkdown", () => {
|
|
test("accepts site-relative /public/upload/ URL without allowed origins", () => {
|
|
expect(extractMediaUrlsFromMarkdown("")).toEqual(["/public/upload/a.webp"]);
|
|
});
|
|
|
|
test("rejects absolute URL when no allowed origins", () => {
|
|
expect(extractMediaUrlsFromMarkdown("")).toEqual([]);
|
|
});
|
|
|
|
test("accepts absolute URL when origin matches allowed list", () => {
|
|
expect(
|
|
extractMediaUrlsFromMarkdown("", {
|
|
allowedAssetOrigins: ["https://blog.example.com"],
|
|
}),
|
|
).toEqual(["/public/upload/b.webp"]);
|
|
});
|
|
|
|
test("rejects absolute URL when origin does not match", () => {
|
|
expect(
|
|
extractMediaUrlsFromMarkdown("", {
|
|
allowedAssetOrigins: ["https://blog.example.com"],
|
|
}),
|
|
).toEqual([]);
|
|
});
|
|
|
|
test("strips query on relative asset URL", () => {
|
|
expect(extractMediaUrlsFromMarkdown("")).toEqual(["/public/upload/c.webp"]);
|
|
});
|
|
|
|
test("strips query on absolute asset URL via pathname", () => {
|
|
expect(
|
|
extractMediaUrlsFromMarkdown("", {
|
|
allowedAssetOrigins: ["https://x.example"],
|
|
}),
|
|
).toEqual(["/public/upload/d.webp"]);
|
|
});
|
|
|
|
test("ignores non-asset absolute URLs", () => {
|
|
expect(
|
|
extractMediaUrlsFromMarkdown("", {
|
|
allowedAssetOrigins: ["https://evil.com"],
|
|
}),
|
|
).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("publicAssetUrlToStorageKey", () => {
|
|
test("maps normalized path to storage key", () => {
|
|
expect(publicAssetUrlToStorageKey("/public/upload/z.webp")).toBe("z.webp");
|
|
});
|
|
});
|
|
|