|
| 1 | +import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test'; |
| 2 | +import type { Server } from 'bun'; |
| 3 | +import 'reflect-metadata'; |
| 4 | + |
| 5 | +import { |
| 6 | + App, |
| 7 | + Module, |
| 8 | + Controller, |
| 9 | + Get, |
| 10 | + Injectable, |
| 11 | + type Middleware, |
| 12 | + type NextFunction, |
| 13 | + UseMiddleware, |
| 14 | +} from '../../src'; |
| 15 | + |
| 16 | +const executionOrder: string[] = []; |
| 17 | + |
| 18 | +@Injectable() |
| 19 | +class GlobalMiddleware1 implements Middleware { |
| 20 | + async use(req: Request, next: NextFunction) { |
| 21 | + executionOrder.push('GlobalMiddleware1'); |
| 22 | + return await next(); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +@Injectable() |
| 27 | +class GlobalMiddleware2 implements Middleware { |
| 28 | + async use(req: Request, next: NextFunction) { |
| 29 | + executionOrder.push('GlobalMiddleware2'); |
| 30 | + return await next(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +@Injectable() |
| 35 | +class ControllerMiddleware implements Middleware { |
| 36 | + async use(req: Request, next: NextFunction) { |
| 37 | + executionOrder.push('ControllerMiddleware'); |
| 38 | + return await next(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +@Injectable() |
| 43 | +class RouteMiddleware implements Middleware { |
| 44 | + async use(req: Request, next: NextFunction) { |
| 45 | + executionOrder.push('RouteMiddleware'); |
| 46 | + return await next(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +@Injectable() |
| 51 | +class ShortCircuitMiddleware implements Middleware { |
| 52 | + async use(req: Request, next: NextFunction) { |
| 53 | + return new Response('Request was short-circuited by middleware', { status: 200 }); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +@Controller('/middleware-test') |
| 58 | +@UseMiddleware(ControllerMiddleware) |
| 59 | +class TestController { |
| 60 | + @Get('/all') |
| 61 | + @UseMiddleware(RouteMiddleware) |
| 62 | + getWithAllMiddlewares() { |
| 63 | + executionOrder.push('Handler'); |
| 64 | + return { success: true }; |
| 65 | + } |
| 66 | + |
| 67 | + @Get('/no-route-mw') |
| 68 | + getWithNoRouteMiddleware() { |
| 69 | + executionOrder.push('Handler'); |
| 70 | + return { success: true }; |
| 71 | + } |
| 72 | + |
| 73 | + @Get('/short-circuit') |
| 74 | + @UseMiddleware(ShortCircuitMiddleware) |
| 75 | + getShortCircuited() { |
| 76 | + executionOrder.push('Handler'); |
| 77 | + return { message: 'This should not be returned' }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +@Module({ |
| 82 | + controllers: [TestController], |
| 83 | + providers: [ |
| 84 | + GlobalMiddleware1, |
| 85 | + GlobalMiddleware2, |
| 86 | + ControllerMiddleware, |
| 87 | + RouteMiddleware, |
| 88 | + ShortCircuitMiddleware, |
| 89 | + ], |
| 90 | +}) |
| 91 | +class TestAppModule {} |
| 92 | + |
| 93 | +describe('Middlewares (E2E)', () => { |
| 94 | + let app: App; |
| 95 | + let server: Server; |
| 96 | + |
| 97 | + beforeAll(() => { |
| 98 | + app = new App(TestAppModule); |
| 99 | + |
| 100 | + app.use(GlobalMiddleware1, GlobalMiddleware2); |
| 101 | + |
| 102 | + server = app.listen(0); |
| 103 | + }); |
| 104 | + |
| 105 | + afterAll(() => { |
| 106 | + server.stop(true); |
| 107 | + }); |
| 108 | + |
| 109 | + beforeEach(() => { |
| 110 | + executionOrder.length = 0; |
| 111 | + }); |
| 112 | + |
| 113 | + test('should execute global, controller, and route middlewares in correct order', async () => { |
| 114 | + const response = await fetch(new URL('/middleware-test/all', server.url)); |
| 115 | + |
| 116 | + expect(response.status).toBe(200); |
| 117 | + |
| 118 | + expect(executionOrder).toEqual([ |
| 119 | + 'GlobalMiddleware1', |
| 120 | + 'GlobalMiddleware2', |
| 121 | + 'ControllerMiddleware', |
| 122 | + 'RouteMiddleware', |
| 123 | + 'Handler', |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should execute only global and controller middlewares when no route middleware is present', async () => { |
| 128 | + const response = await fetch(new URL('/middleware-test/no-route-mw', server.url)); |
| 129 | + |
| 130 | + expect(response.status).toBe(200); |
| 131 | + |
| 132 | + expect(executionOrder).toEqual([ |
| 133 | + 'GlobalMiddleware1', |
| 134 | + 'GlobalMiddleware2', |
| 135 | + 'ControllerMiddleware', |
| 136 | + 'Handler', |
| 137 | + ]); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should allow a middleware to short-circuit the request chain', async () => { |
| 141 | + const response = await fetch(new URL('/middleware-test/short-circuit', server.url)); |
| 142 | + |
| 143 | + expect(response.status).toBe(200); |
| 144 | + expect(await response.text()).toBe('Request was short-circuited by middleware'); |
| 145 | + |
| 146 | + expect(executionOrder).not.toContain('Handler'); |
| 147 | + expect(executionOrder).toEqual([ |
| 148 | + 'GlobalMiddleware1', |
| 149 | + 'GlobalMiddleware2', |
| 150 | + 'ControllerMiddleware', |
| 151 | + ]); |
| 152 | + }); |
| 153 | + |
| 154 | + test('should not apply middlewares to a non-existent route', async () => { |
| 155 | + const response = await fetch(new URL('/not-found', server.url)); |
| 156 | + |
| 157 | + expect(response.status).toBe(404); |
| 158 | + |
| 159 | + expect(executionOrder).toEqual([]); |
| 160 | + }); |
| 161 | +}); |
0 commit comments