|
| 1 | +import context from '@aws-lambda-powertools/testing-utils/context'; |
| 2 | +import type { Context } from 'aws-lambda'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { BaseRouter } from '../../../src/rest/BaseRouter.js'; |
| 5 | +import type { ResolveOptions } from '../../../src/types/index.js'; |
| 6 | +import type { |
| 7 | + RouteHandler, |
| 8 | + RouteOptions, |
| 9 | + RouterOptions, |
| 10 | +} from '../../../src/types/rest.js'; |
| 11 | + |
| 12 | +describe('BaseRouter', () => { |
| 13 | + class TestResolver extends BaseRouter { |
| 14 | + public readonly handlers: Map<string, RouteHandler> = new Map(); |
| 15 | + |
| 16 | + constructor(options?: RouterOptions) { |
| 17 | + super(options); |
| 18 | + this.logger.debug('test debug'); |
| 19 | + this.logger.warn('test warn'); |
| 20 | + this.logger.error('test error'); |
| 21 | + } |
| 22 | + |
| 23 | + #isEvent(obj: unknown): asserts obj is { path: string; method: string } { |
| 24 | + if ( |
| 25 | + typeof obj !== 'object' || |
| 26 | + obj === null || |
| 27 | + !('path' in obj) || |
| 28 | + !('method' in obj) |
| 29 | + ) { |
| 30 | + throw new Error('Invalid event object'); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public route(handler: RouteHandler, options: RouteOptions) { |
| 35 | + if (options.path == null || options.method == null) |
| 36 | + throw new Error('path or method cannot be null'); |
| 37 | + this.handlers.set(options.path + options.method, handler); |
| 38 | + } |
| 39 | + |
| 40 | + public resolve( |
| 41 | + event: unknown, |
| 42 | + context: Context, |
| 43 | + options?: ResolveOptions |
| 44 | + ): Promise<unknown> { |
| 45 | + this.#isEvent(event); |
| 46 | + const { method, path } = event; |
| 47 | + const handler = this.handlers.get(path + method); |
| 48 | + if (handler == null) throw new Error('404'); |
| 49 | + return handler(event, context); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + vi.unstubAllEnvs(); |
| 55 | + }); |
| 56 | + |
| 57 | + it.each([ |
| 58 | + ['GET', 'get'], |
| 59 | + ['POST', 'post'], |
| 60 | + ['PUT', 'put'], |
| 61 | + ['PATCH', 'patch'], |
| 62 | + ['DELETE', 'delete'], |
| 63 | + ['HEAD', 'head'], |
| 64 | + ])('should route %s requests', async (method, verb) => { |
| 65 | + const app = new TestResolver(); |
| 66 | + ( |
| 67 | + app[ |
| 68 | + verb as 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head' |
| 69 | + ] as Function |
| 70 | + )('test', () => `${verb}-test`); |
| 71 | + const actual = await app.resolve({ path: 'test', method }, context); |
| 72 | + expect(actual).toEqual(`${verb}-test`); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should use console.warn and console,error when logger is not provided', () => { |
| 76 | + const app = new TestResolver(); |
| 77 | + expect(console.debug).not.toHaveBeenCalled(); |
| 78 | + expect(console.error).toHaveBeenCalledWith('test error'); |
| 79 | + expect(console.warn).toHaveBeenCalledWith('test warn'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should use console.debug in DEBUG mode when logger is not provided', () => { |
| 83 | + vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG'); |
| 84 | + const app = new TestResolver(); |
| 85 | + expect(console.debug).toHaveBeenCalledWith('test debug'); |
| 86 | + expect(console.error).toHaveBeenCalledWith('test error'); |
| 87 | + expect(console.warn).toHaveBeenCalledWith('test warn'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should use custom logger when provided', () => { |
| 91 | + vi.stubEnv('AWS_LAMBDA_LOG_LEVEL', 'DEBUG'); |
| 92 | + |
| 93 | + const logger = { |
| 94 | + debug: vi.fn(), |
| 95 | + info: vi.fn(), |
| 96 | + warn: vi.fn(), |
| 97 | + error: vi.fn(), |
| 98 | + }; |
| 99 | + |
| 100 | + const app = new TestResolver({ logger }); |
| 101 | + expect(logger.error).toHaveBeenCalledWith('test error'); |
| 102 | + expect(logger.warn).toHaveBeenCalledWith('test warn'); |
| 103 | + expect(logger.debug).toHaveBeenCalledWith('test debug'); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('decorators', () => { |
| 107 | + const app = new TestResolver(); |
| 108 | + |
| 109 | + class Lambda { |
| 110 | + @app.get('test', {}) |
| 111 | + public async getTest() { |
| 112 | + return 'get-test'; |
| 113 | + } |
| 114 | + |
| 115 | + @app.post('test') |
| 116 | + public async postTest() { |
| 117 | + return 'post-test'; |
| 118 | + } |
| 119 | + |
| 120 | + @app.put('test') |
| 121 | + public async putTest() { |
| 122 | + return 'put-test'; |
| 123 | + } |
| 124 | + |
| 125 | + @app.patch('test') |
| 126 | + public async patchTest() { |
| 127 | + return 'patch-test'; |
| 128 | + } |
| 129 | + |
| 130 | + @app.delete('test') |
| 131 | + public async deleteTest() { |
| 132 | + return 'delete-test'; |
| 133 | + } |
| 134 | + |
| 135 | + @app.head('test') |
| 136 | + public async headTest() { |
| 137 | + return 'head-test'; |
| 138 | + } |
| 139 | + |
| 140 | + public async handler(event: unknown, context: Context) { |
| 141 | + return app.resolve(event, context, {}); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + it.each([ |
| 146 | + ['GET', 'get-test'], |
| 147 | + ['POST', 'post-test'], |
| 148 | + ['PUT', 'put-test'], |
| 149 | + ['PATCH', 'patch-test'], |
| 150 | + ['DELETE', 'delete-test'], |
| 151 | + ['HEAD', 'head-test'], |
| 152 | + ])('should route %s requests with decorators', async (method, expected) => { |
| 153 | + const lambda = new Lambda(); |
| 154 | + const actual = await lambda.handler({ path: 'test', method }, context); |
| 155 | + expect(actual).toEqual(expected); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments