XuqmGroup-RNSDK/packages/xwebview/tests/downloadMetadata.test.ts

45 行
1.2 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { fetchDownloadInfo } from '../src/DownloadMetadata'
test('download metadata uses the common RFC filename parser', async () => {
const originalFetch = globalThis.fetch
globalThis.fetch = async () =>
new Response(null, {
headers: {
'content-disposition': "attachment; filename*=UTF-8''%E6%8A%A5%E5%91%8A.pdf",
'content-length': '1024',
'content-type': 'application/pdf; charset=utf-8',
},
status: 200,
})
try {
assert.deepEqual(await fetchDownloadInfo('https://example.test/download'), {
url: 'https://example.test/download',
suggestedFilename: '报告.pdf',
mimeType: 'application/pdf',
fileSize: 1024,
})
} finally {
globalThis.fetch = originalFetch
}
})
test('download metadata falls back to a sanitized URL filename', async () => {
const originalFetch = globalThis.fetch
globalThis.fetch = async () => {
throw new Error('offline')
}
try {
assert.deepEqual(await fetchDownloadInfo('https://example.test/a%20b.txt?token=1'), {
url: 'https://example.test/a%20b.txt?token=1',
suggestedFilename: 'a b.txt',
})
} finally {
globalThis.fetch = originalFetch
}
})