|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { ConfigService } from '@nestjs/config'; |
| 3 | +import { ApiVersionMiddleware } from './api-version.middleware'; |
| 4 | +import { Request, Response } from 'express'; |
| 5 | + |
| 6 | +function buildConfigService(overrides: Record<string, string> = {}): jest.Mocked<ConfigService> { |
| 7 | + const defaults: Record<string, string> = { |
| 8 | + SUNSET_VERSIONS: 'v1:2024-01-01', |
| 9 | + DEPRECATED_VERSIONS: 'v2:2025-06-01', |
| 10 | + API_MIGRATION_DOCS_URL: 'https://docs.example.com/migration', |
| 11 | + ...overrides, |
| 12 | + }; |
| 13 | + return { |
| 14 | + get: jest.fn((key: string, fallback?: string) => defaults[key] ?? fallback ?? ''), |
| 15 | + } as unknown as jest.Mocked<ConfigService>; |
| 16 | +} |
| 17 | + |
| 18 | +function buildRes(): jest.Mocked<Response> { |
| 19 | + const res: Partial<jest.Mocked<Response>> = { |
| 20 | + setHeader: jest.fn().mockReturnThis(), |
| 21 | + status: jest.fn().mockReturnThis(), |
| 22 | + json: jest.fn().mockReturnThis(), |
| 23 | + }; |
| 24 | + return res as jest.Mocked<Response>; |
| 25 | +} |
| 26 | + |
| 27 | +describe('ApiVersionMiddleware', () => { |
| 28 | + let middleware: ApiVersionMiddleware; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + const module: TestingModule = await Test.createTestingModule({ |
| 32 | + providers: [ApiVersionMiddleware, { provide: ConfigService, useValue: buildConfigService() }], |
| 33 | + }).compile(); |
| 34 | + |
| 35 | + middleware = module.get(ApiVersionMiddleware); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('extractVersion', () => { |
| 39 | + it('returns null for non-versioned paths', () => { |
| 40 | + expect(middleware.extractVersion('/users')).toBeNull(); |
| 41 | + expect(middleware.extractVersion('/')).toBeNull(); |
| 42 | + expect(middleware.extractVersion('/health')).toBeNull(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('extracts version from path prefix', () => { |
| 46 | + expect(middleware.extractVersion('/v1/users')).toBe('v1'); |
| 47 | + expect(middleware.extractVersion('/v2/courses')).toBe('v2'); |
| 48 | + expect(middleware.extractVersion('/V3/items')).toBe('v3'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('extracts version from bare version path', () => { |
| 52 | + expect(middleware.extractVersion('/v1')).toBe('v1'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('sunset versions', () => { |
| 57 | + it('returns 410 Gone for a sunset version', () => { |
| 58 | + const req = { path: '/v1/users', method: 'GET' } as Request; |
| 59 | + const res = buildRes(); |
| 60 | + const next = jest.fn(); |
| 61 | + |
| 62 | + middleware.use(req, res, next); |
| 63 | + |
| 64 | + expect(res.status).toHaveBeenCalledWith(410); |
| 65 | + expect(res.json).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ |
| 67 | + statusCode: 410, |
| 68 | + error: 'Gone', |
| 69 | + }), |
| 70 | + ); |
| 71 | + expect(next).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('sets Sunset and Link response headers', () => { |
| 75 | + const req = { path: '/v1/courses', method: 'GET' } as Request; |
| 76 | + const res = buildRes(); |
| 77 | + |
| 78 | + middleware.use(req, res, jest.fn()); |
| 79 | + |
| 80 | + expect(res.setHeader).toHaveBeenCalledWith('Sunset', expect.any(String)); |
| 81 | + expect(res.setHeader).toHaveBeenCalledWith( |
| 82 | + 'Link', |
| 83 | + expect.stringContaining('successor-version'), |
| 84 | + ); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('deprecated versions', () => { |
| 89 | + it('calls next() for deprecated (grace-period) versions', () => { |
| 90 | + const req = { path: '/v2/users', method: 'GET' } as Request; |
| 91 | + const res = buildRes(); |
| 92 | + const next = jest.fn(); |
| 93 | + |
| 94 | + middleware.use(req, res, next); |
| 95 | + |
| 96 | + expect(next).toHaveBeenCalled(); |
| 97 | + expect(res.status).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('sets Deprecation and Sunset headers for deprecated versions', () => { |
| 101 | + const req = { path: '/v2/courses', method: 'GET' } as Request; |
| 102 | + const res = buildRes(); |
| 103 | + |
| 104 | + middleware.use(req, res, jest.fn()); |
| 105 | + |
| 106 | + expect(res.setHeader).toHaveBeenCalledWith('Deprecation', 'true'); |
| 107 | + expect(res.setHeader).toHaveBeenCalledWith('Sunset', expect.any(String)); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('non-versioned paths', () => { |
| 112 | + it('passes through without touching response headers', () => { |
| 113 | + const req = { path: '/health', method: 'GET' } as Request; |
| 114 | + const res = buildRes(); |
| 115 | + const next = jest.fn(); |
| 116 | + |
| 117 | + middleware.use(req, res, next); |
| 118 | + |
| 119 | + expect(next).toHaveBeenCalled(); |
| 120 | + expect(res.setHeader).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('empty configuration', () => { |
| 125 | + it('passes all requests when no versions are configured', async () => { |
| 126 | + const module: TestingModule = await Test.createTestingModule({ |
| 127 | + providers: [ |
| 128 | + ApiVersionMiddleware, |
| 129 | + { |
| 130 | + provide: ConfigService, |
| 131 | + useValue: buildConfigService({ SUNSET_VERSIONS: '', DEPRECATED_VERSIONS: '' }), |
| 132 | + }, |
| 133 | + ], |
| 134 | + }).compile(); |
| 135 | + |
| 136 | + const mw = module.get(ApiVersionMiddleware); |
| 137 | + const req = { path: '/v1/users', method: 'GET' } as Request; |
| 138 | + const res = buildRes(); |
| 139 | + const next = jest.fn(); |
| 140 | + |
| 141 | + mw.use(req, res, next); |
| 142 | + |
| 143 | + expect(next).toHaveBeenCalled(); |
| 144 | + expect(res.status).not.toHaveBeenCalled(); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments