|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { middleware } from '../../middleware' |
| 3 | +import type { NextRequest } from 'next/server' |
| 4 | + |
| 5 | +// Helper to create a minimal NextRequest-like object for testing |
| 6 | +function mockRequest(pathname: string) { |
| 7 | + return { nextUrl: { pathname } } as unknown as NextRequest |
| 8 | +} |
| 9 | + |
| 10 | +describe('middleware /news/:year validation', () => { |
| 11 | + it('allows valid year at lower bound (2001)', () => { |
| 12 | + const res = middleware(mockRequest('/news/2001')) |
| 13 | + expect(res.status).toBe(200) |
| 14 | + }) |
| 15 | + |
| 16 | + it('allows valid year at upper bound (2025)', () => { |
| 17 | + const res = middleware(mockRequest('/news/2025')) |
| 18 | + expect(res.status).toBe(200) |
| 19 | + }) |
| 20 | + |
| 21 | + it('returns 404 for year below range (1999)', () => { |
| 22 | + const res = middleware(mockRequest('/news/1999')) |
| 23 | + expect(res.status).toBe(404) |
| 24 | + }) |
| 25 | + |
| 26 | + it('returns 404 for year above range (2026)', () => { |
| 27 | + const res = middleware(mockRequest('/news/2026')) |
| 28 | + expect(res.status).toBe(404) |
| 29 | + }) |
| 30 | + |
| 31 | + it('returns 404 for non-numeric year (abcd)', () => { |
| 32 | + const res = middleware(mockRequest('/news/abcd')) |
| 33 | + expect(res.status).toBe(404) |
| 34 | + }) |
| 35 | + |
| 36 | + it('passes through for paths with extra segments (not single-segment match)', () => { |
| 37 | + const res = middleware(mockRequest('/news/2025/extra')) |
| 38 | + expect(res.status).toBe(200) |
| 39 | + }) |
| 40 | + |
| 41 | + it('passes through for non-news paths', () => { |
| 42 | + const res = middleware(mockRequest('/features')) |
| 43 | + expect(res.status).toBe(200) |
| 44 | + }) |
| 45 | +}) |
| 46 | + |
0 commit comments