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.
59 lines
2.2 KiB
59 lines
2.2 KiB
import { describe, expect, test } from "bun:test";
|
|
import { POST_MEDIA_PUBLIC_PREFIX } from "#server/constants/media";
|
|
import { extractMediaUrlsFromMarkdown, publicAssetUrlToStorageKey } from "./post-media-urls";
|
|
|
|
describe("extractMediaUrlsFromMarkdown", () => {
|
|
test("accepts site-relative asset URL without allowed origins", () => {
|
|
expect(extractMediaUrlsFromMarkdown(``)).toEqual([
|
|
`${POST_MEDIA_PUBLIC_PREFIX}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([`${POST_MEDIA_PUBLIC_PREFIX}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([
|
|
`${POST_MEDIA_PUBLIC_PREFIX}c.webp`,
|
|
]);
|
|
});
|
|
|
|
test("strips query on absolute asset URL via pathname", () => {
|
|
expect(
|
|
extractMediaUrlsFromMarkdown(``, {
|
|
allowedAssetOrigins: ["https://x.example"],
|
|
}),
|
|
).toEqual([`${POST_MEDIA_PUBLIC_PREFIX}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(`${POST_MEDIA_PUBLIC_PREFIX}z.webp`)).toBe("z.webp");
|
|
});
|
|
});
|
|
|