|
| 1 | +import { describe, test, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { validateHeaders } from './validate-headers.js'; |
| 3 | + |
| 4 | +describe('validateHeaders', () => { |
| 5 | + const console_warn_spy = vi.spyOn(console, 'warn'); |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + vi.resetAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('cache-control header', () => { |
| 12 | + test('accepts valid directives', () => { |
| 13 | + validateHeaders({ 'cache-control': 'public, max-age=3600' }); |
| 14 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('rejects invalid directives', () => { |
| 18 | + validateHeaders({ 'cache-control': 'public, maxage=3600' }); |
| 19 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 20 | + expect.stringContaining('Invalid cache-control directive "maxage"') |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + test('rejects empty directives', () => { |
| 25 | + validateHeaders({ 'cache-control': 'public,, max-age=3600' }); |
| 26 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 27 | + expect.stringContaining('`cache-control` header contains empty directives') |
| 28 | + ); |
| 29 | + |
| 30 | + validateHeaders({ 'cache-control': 'public, , max-age=3600' }); |
| 31 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 32 | + expect.stringContaining('`cache-control` header contains empty directives') |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + test('accepts multiple cache-control values', () => { |
| 37 | + validateHeaders({ 'cache-control': 'max-age=3600, s-maxage=7200' }); |
| 38 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('content-type header', () => { |
| 43 | + test('accepts standard content types', () => { |
| 44 | + validateHeaders({ 'content-type': 'application/json' }); |
| 45 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('accepts content types with parameters', () => { |
| 49 | + validateHeaders({ 'content-type': 'text/html; charset=utf-8' }); |
| 50 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 51 | + |
| 52 | + validateHeaders({ 'content-type': 'application/javascript; charset=utf-8' }); |
| 53 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('accepts vendor-specific content types', () => { |
| 57 | + validateHeaders({ 'content-type': 'x-custom/whatever' }); |
| 58 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + test('rejects malformed content types', () => { |
| 62 | + validateHeaders({ 'content-type': 'invalid-content-type' }); |
| 63 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 64 | + expect.stringContaining('Invalid content-type value "invalid-content-type"') |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + test('rejects invalid content type categories', () => { |
| 69 | + validateHeaders({ 'content-type': 'invalid/type; invalid=param' }); |
| 70 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 71 | + expect.stringContaining('Invalid content-type value "invalid/type"') |
| 72 | + ); |
| 73 | + |
| 74 | + validateHeaders({ 'content-type': 'bad/type; charset=utf-8' }); |
| 75 | + expect(console_warn_spy).toHaveBeenCalledWith( |
| 76 | + expect.stringContaining('Invalid content-type value "bad/type"') |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + test('handles case-insensitive content-types', () => { |
| 81 | + validateHeaders({ 'content-type': 'TEXT/HTML; charset=utf-8' }); |
| 82 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test('allows unknown headers', () => { |
| 87 | + validateHeaders({ 'x-custom-header': 'some-value' }); |
| 88 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + test('handles multiple headers simultaneously', () => { |
| 92 | + validateHeaders({ |
| 93 | + 'cache-control': 'max-age=3600', |
| 94 | + 'content-type': 'text/html', |
| 95 | + 'x-custom': 'value' |
| 96 | + }); |
| 97 | + expect(console_warn_spy).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments