| 12345678910111213141516171819202122232425262728293031323334 |
- import assert from 'node:assert/strict'
- import fs from 'node:fs'
- import os from 'node:os'
- import path from 'node:path'
- import test from 'node:test'
- import { Writable } from 'node:stream'
- import localGisStatic from '../src/common/localGisStaticPlugin.js'
- test('dev /gis 瓦片必须可缓存,否则预热请求进不了浏览器缓存', async () => {
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cockpit-gis-'))
- fs.writeFileSync(path.join(dir, 'tile.png'), Buffer.from([0, 1, 2, 3]))
- let handler
- const server = { middlewares: { use: (_prefix, fn) => { handler = fn } } }
- try {
- localGisStatic({ mounts: JSON.stringify([{ prefix: '/gis/pic-ring0', dir }]), log: () => {} }).configureServer(server)
- assert.equal(typeof handler, 'function')
- const headers = {}
- let body = ''
- const res = new Writable({
- write(chunk, _encoding, callback) { body += chunk.toString(); callback() },
- })
- res.setHeader = (name, value) => { headers[name] = value }
- const finished = new Promise((resolve) => res.on('finish', resolve))
- handler({ url: '/tile.png' }, res)
- await finished
- assert.equal(headers['Content-Type'], 'image/png')
- assert.equal(headers['Cache-Control'], 'public, max-age=86400')
- assert.equal(body, Buffer.from([0, 1, 2, 3]).toString())
- } finally {
- fs.rmSync(dir, { recursive: true, force: true })
- }
- })
|