76 lines
4.2 KiB
JavaScript
76 lines
4.2 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { copyFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import path from 'node:path'
|
|
import { test } from 'node:test'
|
|
import { pathToFileURL } from 'node:url'
|
|
|
|
const host = '127.0.0.1:5178'
|
|
const destination = 'http://127.0.0.1:5179/admin/#'
|
|
async function request(handler, url, headers = {}, method = 'GET') {
|
|
const res = {
|
|
headers: {},
|
|
setHeader(name, value) { this.headers[name] = value },
|
|
writeHead(status, headers) { this.status = status; Object.assign(this.headers, headers) },
|
|
end(body) { this.body = body }
|
|
}
|
|
await handler({method, url, headers: {host, ...headers}}, res)
|
|
return res
|
|
}
|
|
|
|
test('无构建和模拟数据时默认/显式 5178 仍可导航;5180 才依赖演示文件', async (t) => {
|
|
const root = await mkdtemp(path.join(tmpdir(), 'zyt-preview-entry-'))
|
|
t.after(() => rm(root, {recursive: true, force: true}))
|
|
const dir = path.join(root, 'local-preview')
|
|
await mkdir(dir)
|
|
const file = path.join(dir, 'server.mjs')
|
|
await copyFile(new URL('./server.mjs', import.meta.url), file)
|
|
const {createLocalListener} = await import(pathToFileURL(file).href)
|
|
for (const args of [[], ['5178']]) {
|
|
const {port, handler} = await createLocalListener(args)
|
|
assert.equal(port, 5178)
|
|
for (const url of ['/', '/admin', '/admin/', '/admin/workbench', '/admin/login?redirect=//dummy.invalid', '/admin/members/detail?id=123&token=dummy']) {
|
|
const res = await request(handler, url, {'sec-fetch-mode':'navigate', 'sec-fetch-dest':'document', accept:'text/html,application/xhtml+xml'})
|
|
assert.equal(res.status, 302, url)
|
|
assert.equal(res.headers.Location, destination, url)
|
|
assert.equal(res.headers['Cache-Control'], 'no-store')
|
|
assert.equal(res.headers['Referrer-Policy'], 'no-referrer')
|
|
assert.equal(res.body, undefined)
|
|
}
|
|
}
|
|
await assert.rejects(createLocalListener(['5180']), {code:'ERR_MODULE_NOT_FOUND'})
|
|
for (const args of [['5179'], ['0'], [''], ['5178','5180']]) {
|
|
await assert.rejects(createLocalListener(args), /端口仅支持/)
|
|
}
|
|
// A miniature, local-only demo build verifies that the explicit mode still serves a page and a fixture.
|
|
const dist = path.join(root, 'node_modules/.zyt-ui-preview/dist/local-preview')
|
|
await mkdir(dist, {recursive:true})
|
|
await writeFile(path.join(dist, 'index.html'), '<h1>offline demo</h1>')
|
|
await writeFile(path.join(dir, 'fixtures.mjs'), 'export const resolveFixture = () => ({data:"demo"})')
|
|
const {port, handler} = await createLocalListener(['5180'])
|
|
const headers = {host:'127.0.0.1:5180'}
|
|
assert.equal(port, 5180)
|
|
assert.equal(String((await request(handler, '/admin/workbench', headers)).body), '<h1>offline demo</h1>')
|
|
assert.deepEqual(JSON.parse((await request(handler, '/adminapi/order.order/lists', headers)).body), {data:'demo'})
|
|
})
|
|
|
|
test('5178 拒绝接口、资源、非导航请求和账号提交,不返回 Location 或数据', async () => {
|
|
const {createLocalListener} = await import('./server.mjs')
|
|
const {handler} = await createLocalListener([])
|
|
const cases = [
|
|
...['/adminapi/order.order/lists', '/api-proxy/adminapi/order.order/lists', '/admin/assets/js',
|
|
'/admin/assets/style.css', '/admin/static/logo', '/admin/api/list', '/admin/favicon.ico', '/.env',
|
|
'//dummy.invalid/admin', '/admin/../admin', '/admin/%2f%2fdummy.invalid'].map(url => [url, {}, 'GET', 404]),
|
|
...['POST','PUT','DELETE','OPTIONS','HEAD'].map(method => ['/admin/login', {}, method, 405]),
|
|
...[{host:'dummy.invalid'}, {origin:'https://dummy.invalid'}, {'sec-fetch-site':'cross-site'},
|
|
{'sec-fetch-mode':'cors'}, {'sec-fetch-dest':'script'}, {accept:'application/json'},
|
|
{authorization:'test'}, {token:'test'}].map(headers => ['/admin/', headers, 'GET', 403])
|
|
]
|
|
for (const [url, headers, method, status] of cases) {
|
|
const res = await request(handler, url, headers, method)
|
|
assert.equal(res.status, status, method + ' ' + url)
|
|
assert.equal(res.headers.Location, undefined)
|
|
assert.equal(res.body, undefined)
|
|
}
|
|
})
|