-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b47982c
commit dd8b55a
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { Request, RunModes, Version } from '@jitar/execution'; | ||
|
||
import { MIDDLEWARE_MANAGERS } from './fixtures'; | ||
|
||
describe('MiddlewareManager', () => | ||
{ | ||
describe('.handle(fqn, version, args, headers', () => | ||
{ | ||
it('should execute the middleware in the correct order', async () => | ||
{ | ||
const args = new Map(); | ||
const headers = new Map(); | ||
|
||
const request = new Request('test', new Version(1, 0, 0), args, headers, RunModes.NORMAL); | ||
const response = await MIDDLEWARE_MANAGERS.DEFAULT.handle(request); | ||
|
||
expect(response.result).toBe('123'); | ||
expect(headers.get('first')).toBe('yes'); | ||
expect(headers.get('second')).toBe('yes'); | ||
expect(headers.get('third')).toBe('yes'); | ||
expect(headers.get('last')).toBe('3'); // The last middleware to be called is the last one added | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
export * from './middlewares.fixture'; | ||
export * from './middlewareManagers.fixture'; |
14 changes: 14 additions & 0 deletions
14
packages/middleware/test/fixtures/middlewareManagers.fixture.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
import MiddlewareManager from '../../src/middlewareManager'; | ||
|
||
import { MIDDLEWARES } from './middlewares.fixture'; | ||
|
||
const manager = new MiddlewareManager(); | ||
manager.addMiddleware(MIDDLEWARES.FIRST); | ||
manager.addMiddleware(MIDDLEWARES.SECOND); | ||
manager.addMiddleware(MIDDLEWARES.THIRD); | ||
|
||
export const MIDDLEWARE_MANAGERS = | ||
{ | ||
DEFAULT: manager | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
import { Request, Response, StatusCodes } from '@jitar/execution'; | ||
|
||
import Middleware from '../../src/interfaces/Middleware'; | ||
|
||
class FirstMiddleware implements Middleware | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async handle(request: Request, next: () => Promise<Response>): Promise<Response> | ||
{ | ||
request.setHeader('first', 'yes'); | ||
request.setHeader('last', '1'); | ||
|
||
const response = await next(); | ||
response.result = '1' + response.result; | ||
|
||
return response; | ||
} | ||
} | ||
|
||
class SecondMiddleware implements Middleware | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async handle(request: Request, next: () => Promise<Response>): Promise<Response> | ||
{ | ||
request.setHeader('second', 'yes'); | ||
request.setHeader('last', '2'); | ||
|
||
const response = await next(); | ||
response.result = '2' + response.result; | ||
|
||
return response; | ||
} | ||
} | ||
|
||
class ThirdMiddleware implements Middleware | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async handle(request: Request, next: () => Promise<Response>): Promise<Response> | ||
{ | ||
request.setHeader('third', 'yes'); | ||
request.setHeader('last', '3'); | ||
|
||
return new Response(StatusCodes.OK, '3'); | ||
} | ||
} | ||
|
||
export const MIDDLEWARES = | ||
{ | ||
FIRST: new FirstMiddleware(), | ||
SECOND: new SecondMiddleware(), | ||
THIRD: new ThirdMiddleware() | ||
}; |