|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type { Request, Response } from 'express'; |
| 3 | +import { deprecationMiddleware } from '../deprecation.js'; |
| 4 | + |
| 5 | +// ─── Helpers ────────────────────────────────────────────────────────────────── |
| 6 | + |
| 7 | +function makeReq(overrides: Partial<Request> = {}): Request { |
| 8 | + return { |
| 9 | + method: 'GET', |
| 10 | + originalUrl: '/api/v1/test', |
| 11 | + ip: '127.0.0.1', |
| 12 | + headers: {}, |
| 13 | + ...overrides, |
| 14 | + } as unknown as Request; |
| 15 | +} |
| 16 | + |
| 17 | +function makeRes(): { |
| 18 | + res: Response; |
| 19 | + headers: Record<string, string | string[] | number>; |
| 20 | +} { |
| 21 | + const headers: Record<string, string | string[] | number> = {}; |
| 22 | + |
| 23 | + const res = { |
| 24 | + setHeader: vi.fn((name: string, value: string | string[] | number) => { |
| 25 | + headers[name] = value; |
| 26 | + }), |
| 27 | + getHeader: vi.fn((name: string) => { |
| 28 | + return headers[name]; |
| 29 | + }), |
| 30 | + } as unknown as Response; |
| 31 | + |
| 32 | + return { res, headers }; |
| 33 | +} |
| 34 | + |
| 35 | +// ─── Tests ──────────────────────────────────────────────────────────────────── |
| 36 | + |
| 37 | +describe('deprecationMiddleware()', () => { |
| 38 | + let next: ReturnType<typeof vi.fn>; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + next = vi.fn(); |
| 42 | + // Mock console.warn to avoid cluttering test output |
| 43 | + vi.spyOn(console, 'warn').mockImplementation(() => { }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('sets Deprecation header', () => { |
| 47 | + const req = makeReq(); |
| 48 | + const { res, headers } = makeRes(); |
| 49 | + const deprecationDate = '2023-12-31'; |
| 50 | + const mw = deprecationMiddleware({ deprecationDate }); |
| 51 | + |
| 52 | + mw(req, res, next); |
| 53 | + |
| 54 | + expect(headers['Deprecation']).toBe(new Date(deprecationDate).toUTCString()); |
| 55 | + expect(next).toHaveBeenCalledOnce(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('sets Sunset header when provided', () => { |
| 59 | + const req = makeReq(); |
| 60 | + const { res, headers } = makeRes(); |
| 61 | + const deprecationDate = '2023-10-01'; |
| 62 | + const sunsetDate = '2024-03-31'; |
| 63 | + const mw = deprecationMiddleware({ deprecationDate, sunsetDate }); |
| 64 | + |
| 65 | + mw(req, res, next); |
| 66 | + |
| 67 | + expect(headers['Deprecation']).toBe(new Date(deprecationDate).toUTCString()); |
| 68 | + expect(headers['Sunset']).toBe(new Date(sunsetDate).toUTCString()); |
| 69 | + }); |
| 70 | + |
| 71 | + it('sets Link header for successor-version', () => { |
| 72 | + const req = makeReq(); |
| 73 | + const { res, headers } = makeRes(); |
| 74 | + const alternativeUrl = 'https://api.example.com/v2'; |
| 75 | + const mw = deprecationMiddleware({ |
| 76 | + deprecationDate: '2023-01-01', |
| 77 | + alternativeUrl |
| 78 | + }); |
| 79 | + |
| 80 | + mw(req, res, next); |
| 81 | + |
| 82 | + expect(headers['Link']).toBe(`<${alternativeUrl}>; rel="successor-version"`); |
| 83 | + }); |
| 84 | + |
| 85 | + it('appends to existing Link header', () => { |
| 86 | + const req = makeReq(); |
| 87 | + const { res, headers } = makeRes(); |
| 88 | + |
| 89 | + // Set existing Link header |
| 90 | + const existingLink = '<https://docs.example.com>; rel="help"'; |
| 91 | + res.setHeader('Link', existingLink); |
| 92 | + |
| 93 | + const alternativeUrl = 'https://api.example.com/v2'; |
| 94 | + const mw = deprecationMiddleware({ |
| 95 | + deprecationDate: '2023-01-01', |
| 96 | + alternativeUrl |
| 97 | + }); |
| 98 | + |
| 99 | + mw(req, res, next); |
| 100 | + |
| 101 | + const linkHeader = headers['Link']; |
| 102 | + expect(Array.isArray(linkHeader)).toBe(true); |
| 103 | + expect(linkHeader).toContain(existingLink); |
| 104 | + expect(linkHeader).toContain(`<${alternativeUrl}>; rel="successor-version"`); |
| 105 | + }); |
| 106 | + |
| 107 | + it('logs a warning with request details', () => { |
| 108 | + const req = makeReq({ method: 'POST', originalUrl: '/api/v1/old-endpoint' }); |
| 109 | + const { res } = makeRes(); |
| 110 | + const deprecationDate = '2023-06-01'; |
| 111 | + const mw = deprecationMiddleware({ deprecationDate }); |
| 112 | + |
| 113 | + mw(req, res, next); |
| 114 | + |
| 115 | + expect(console.warn).toHaveBeenCalledWith( |
| 116 | + expect.stringContaining('[DEPRECATION WARNING]') |
| 117 | + ); |
| 118 | + expect(console.warn).toHaveBeenCalledWith( |
| 119 | + expect.stringContaining('POST') |
| 120 | + ); |
| 121 | + expect(console.warn).toHaveBeenCalledWith( |
| 122 | + expect.stringContaining('/api/v1/old-endpoint') |
| 123 | + ); |
| 124 | + expect(console.warn).toHaveBeenCalledWith( |
| 125 | + expect.stringContaining(deprecationDate) |
| 126 | + ); |
| 127 | + }); |
| 128 | +}); |
0 commit comments