st
Version:
A module for serving static files. Does etags, caching, etc.
70 lines (61 loc) • 1.55 kB
JavaScript
import path from 'node:path'
import http from 'node:http'
import { fileURLToPath } from 'node:url'
import { test, teardown } from './support/tap-shim.js'
import { request } from './support/http-client.js'
import st from '../st.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const port = process.env.PORT || 1337
const opts = Object.assign({
index: false,
path: path.dirname(__dirname),
url: '/test'
}, global.options || {})
const mount = st(opts)
let server
let cacheControl = null
function req (url, headers, cb) {
if (typeof headers === 'function') {
cb = headers
headers = {}
}
request({
encoding: null,
url: 'http://localhost:' + port + url,
headers
}, cb)
}
test('setup', (t) => {
server = http.createServer((req, res) => {
if (cacheControl) {
res.setHeader('cache-control', cacheControl)
}
if (!mount(req, res)) {
res.statusCode = 404
return res.end('Not a match: ' + req.url)
}
}).listen(port, () => {
t.pass('listening')
t.end()
})
})
teardown(() => {
server.close()
})
test('simple request', (t) => {
cacheControl = null
req('/test/st.js', (er, res, body) => {
t.error(er)
t.equal(res.headers['cache-control'], 'public, max-age=600')
t.end()
})
})
test('pre-set cache-control', (t) => {
cacheControl = 'I\'m so excited, and I just can\'t hide it'
req('/test/st.js', (er, res, body) => {
t.error(er)
t.equal(res.headers['cache-control'], cacheControl)
t.end()
})
})