70 lines
2 KiB
TypeScript
70 lines
2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import {
|
|
isPrivateOrLocalHost,
|
|
normalizeManualAssetValue,
|
|
} from "@/lib/manuals-asset-paths"
|
|
|
|
test("normalizeManualAssetValue keeps relative paths relative", () => {
|
|
assert.equal(
|
|
normalizeManualAssetValue("Royal-Vendors/foo-manual.pdf", "manual"),
|
|
"Royal-Vendors/foo-manual.pdf"
|
|
)
|
|
assert.equal(
|
|
normalizeManualAssetValue("Royal-Vendors/foo-thumb.jpg", "thumbnail"),
|
|
"Royal-Vendors/foo-thumb.jpg"
|
|
)
|
|
})
|
|
|
|
test("normalizeManualAssetValue normalizes site-proxy absolute URLs to relative paths", () => {
|
|
assert.equal(
|
|
normalizeManualAssetValue(
|
|
"https://cdn.example.com/manuals/vendor/foo-manual.pdf",
|
|
"manual"
|
|
),
|
|
"vendor/foo-manual.pdf"
|
|
)
|
|
|
|
assert.equal(
|
|
normalizeManualAssetValue(
|
|
"https://files.example.com/vendor/foo-manual.pdf",
|
|
"manual"
|
|
),
|
|
"https://files.example.com/vendor/foo-manual.pdf"
|
|
)
|
|
})
|
|
|
|
test("normalizeManualAssetValue rewrites localhost and private hosts to relative paths", () => {
|
|
assert.equal(
|
|
normalizeManualAssetValue(
|
|
"http://localhost:3000/api/thumbnails/Royal-Vendors/foo-thumb.jpg",
|
|
"thumbnail"
|
|
),
|
|
"Royal-Vendors/foo-thumb.jpg"
|
|
)
|
|
|
|
assert.equal(
|
|
normalizeManualAssetValue(
|
|
"http://127.0.0.1:3000/api/manuals/Royal-Vendors/foo-manual.pdf",
|
|
"manual"
|
|
),
|
|
"Royal-Vendors/foo-manual.pdf"
|
|
)
|
|
|
|
assert.equal(
|
|
normalizeManualAssetValue(
|
|
"http://10.1.2.3:3000/api/thumbnails/Royal-Vendors/foo-thumb.jpg",
|
|
"thumbnail"
|
|
),
|
|
"Royal-Vendors/foo-thumb.jpg"
|
|
)
|
|
})
|
|
|
|
test("isPrivateOrLocalHost identifies local/private hosts", () => {
|
|
assert.equal(isPrivateOrLocalHost("localhost"), true)
|
|
assert.equal(isPrivateOrLocalHost("127.0.0.1"), true)
|
|
assert.equal(isPrivateOrLocalHost("10.0.0.9"), true)
|
|
assert.equal(isPrivateOrLocalHost("192.168.1.20"), true)
|
|
assert.equal(isPrivateOrLocalHost("172.18.5.4"), true)
|
|
assert.equal(isPrivateOrLocalHost("cdn.example.com"), false)
|
|
})
|