UNPKG

als-send-file

Version:

file serving with advanced options for caching, headers, and error handling, compatible with Express middleware.

52 lines (42 loc) 1.89 kB
const http = require('http'); const { describe, it, beforeEach } = require('node:test'); const assert = require('node:assert'); const cacheControl = require('../lib/cache-control'); describe('Cache-Control Tests', () => { let response; beforeEach(() => { response = new http.ServerResponse({}); response.setHeader = function (key, value) { this.headers = this.headers || {}; this.headers[key] = value; }; }); it('should set the correct Cache-Control header for maxAge', () => { cacheControl(response, { maxAge: 3600 }); assert.strictEqual(response.headers['Cache-Control'], 'max-age=3600'); }); it('should set multiple directives correctly', () => { cacheControl(response, { maxAge: 3600, noCache: true, public: 'public' }); assert.strictEqual(response.headers['Cache-Control'], 'public, max-age=3600, no-cache'); }); it('should handle no directives set', () => { cacheControl(response, {}); assert.strictEqual(response.headers, undefined); }); it('should set the correct Cache-Control header for no-store', () => { cacheControl(response, { noStore: true }); assert.strictEqual(response.headers['Cache-Control'], 'no-store'); }); it('should set the correct Cache-Control header for private', () => { cacheControl(response, { public: 'private' }); assert.strictEqual(response.headers['Cache-Control'], 'private'); }); it('should ignore invalid maxAge', () => { cacheControl(response, { maxAge: 'invalid' }); assert.strictEqual(response.headers, undefined); }); it('should set all directives correctly', () => { cacheControl(response, { maxAge: 3600, noCache: true, noStore: true, public: 'private' }); assert.strictEqual(response.headers['Cache-Control'], 'private, max-age=3600, no-cache, no-store'); }); });