11import context from '@aws-lambda-powertools/testing-utils/context' ;
2- import type {
3- APIGatewayProxyEvent ,
4- APIGatewayProxyEventV2 ,
5- APIGatewayProxyResult ,
6- APIGatewayProxyStructuredResultV2 ,
7- Context ,
8- } from 'aws-lambda' ;
92import { describe , expect , it } from 'vitest' ;
103import {
114 BadRequestError ,
@@ -17,124 +10,79 @@ import {
1710} from '../../../../src/rest/index.js' ;
1811import type { RequestContext } from '../../../../src/types/rest.js' ;
1912import {
13+ createHandler ,
14+ createHandlerWithScope ,
15+ createStreamHandler ,
2016 createTestEvent ,
2117 createTestEventV2 ,
18+ createTestLambdaClass ,
2219 createTrackingMiddleware ,
2320 MockResponseStream ,
2421 parseStreamOutput ,
2522} from '../helpers.js' ;
2623
27- const createHandler = ( app : Router ) => {
28- function handler (
29- event : APIGatewayProxyEvent ,
30- context : Context
31- ) : Promise < APIGatewayProxyResult > ;
32- function handler (
33- event : APIGatewayProxyEventV2 ,
34- context : Context
35- ) : Promise < APIGatewayProxyStructuredResultV2 > ;
36- function handler (
37- event : unknown ,
38- context : Context
39- ) : Promise < APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 > ;
40- function handler ( event : unknown , context : Context ) {
41- return app . resolve ( event , context ) ;
42- }
43- return handler ;
44- } ;
45-
46- const createHandlerWithScope = ( app : Router , scope : unknown ) => {
47- function handler (
48- event : APIGatewayProxyEvent ,
49- context : Context
50- ) : Promise < APIGatewayProxyResult > ;
51- function handler (
52- event : APIGatewayProxyEventV2 ,
53- context : Context
54- ) : Promise < APIGatewayProxyStructuredResultV2 > ;
55- function handler (
56- event : unknown ,
57- context : Context
58- ) : Promise < APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 > ;
59- function handler ( event : unknown , context : Context ) {
60- return app . resolve ( event , context , { scope } ) ;
61- }
62- return handler ;
63- } ;
64-
65- const createStreamHandler =
66- ( app : Router , scope : unknown ) =>
67- ( event : unknown , _context : Context , responseStream : MockResponseStream ) =>
68- app . resolveStream ( event , _context , { scope, responseStream } ) ;
69-
7024describe . each ( [
7125 { version : 'V1' , createEvent : createTestEvent } ,
7226 { version : 'V2' , createEvent : createTestEventV2 } ,
7327] ) ( 'Class: Router - Decorators ($version)' , ( { createEvent } ) => {
7428 describe ( 'decorators' , ( ) => {
75- const app = new Router ( ) ;
76-
77- class Lambda {
78- @app . get ( '/test' )
79- public getTest ( ) {
80- return { result : 'get-test' } ;
81- }
82-
83- @app . post ( '/test' )
84- public postTest ( ) {
85- return { result : 'post-test' } ;
86- }
29+ const httpMethods = [
30+ [ 'GET' , 'get' ] ,
31+ [ 'POST' , 'post' ] ,
32+ [ 'PUT' , 'put' ] ,
33+ [ 'PATCH' , 'patch' ] ,
34+ [ 'DELETE' , 'delete' ] ,
35+ [ 'HEAD' , 'head' ] ,
36+ [ 'OPTIONS' , 'options' ] ,
37+ ] ;
38+ it . each ( httpMethods ) (
39+ 'routes %s requests with object response' ,
40+ async ( method , verb ) => {
41+ // Prepare
42+ const app = new Router ( ) ;
43+ const expected = { result : `${ verb } -test` } ;
44+ const Lambda = createTestLambdaClass ( app , expected ) ;
45+ const lambda = new Lambda ( ) ;
8746
88- @app . put ( '/test' )
89- public putTest ( ) {
90- return { result : 'put-test' } ;
91- }
47+ // Act
48+ const actual = await lambda . handler (
49+ createTestEvent ( '/test' , method ) ,
50+ context
51+ ) ;
9252
93- @app . patch ( '/test' )
94- public patchTest ( ) {
95- return { result : 'patch-test' } ;
53+ // Assess
54+ expect ( actual . statusCode ) . toBe ( 200 ) ;
55+ expect ( actual . body ) . toBe ( JSON . stringify ( expected ) ) ;
56+ expect ( actual . headers ?. [ 'content-type' ] ) . toBe ( 'application/json' ) ;
57+ expect ( actual . isBase64Encoded ) . toBe ( false ) ;
9658 }
59+ ) ;
9760
98- @app . delete ( '/test' )
99- public deleteTest ( ) {
100- return { result : 'delete-test' } ;
101- }
61+ it . each ( httpMethods ) (
62+ 'routes %s requests with array response' ,
63+ async ( method , verb ) => {
64+ // Prepare
65+ const app = new Router ( ) ;
66+ const expected = [
67+ { id : 1 , result : `${ verb } -test-1` } ,
68+ { id : 2 , result : `${ verb } -test-2` } ,
69+ ] ;
70+ const Lambda = createTestLambdaClass ( app , expected ) ;
71+ const lambda = new Lambda ( ) ;
10272
103- @app . head ( '/test' )
104- public headTest ( ) {
105- return { result : 'head-test' } ;
106- }
73+ // Act
74+ const actual = await lambda . handler (
75+ createTestEvent ( '/test' , method ) ,
76+ context
77+ ) ;
10778
108- @app . options ( '/test' )
109- public optionsTest ( ) {
110- return { result : 'options-test' } ;
79+ // Assess
80+ expect ( actual . statusCode ) . toBe ( 200 ) ;
81+ expect ( actual . body ) . toBe ( JSON . stringify ( expected ) ) ;
82+ expect ( actual . headers ?. [ 'content-type' ] ) . toBe ( 'application/json' ) ;
83+ expect ( actual . isBase64Encoded ) . toBe ( false ) ;
11184 }
112-
113- public handler = createHandler ( app ) ;
114- }
115-
116- it . each ( [
117- [ 'GET' , { result : 'get-test' } ] ,
118- [ 'POST' , { result : 'post-test' } ] ,
119- [ 'PUT' , { result : 'put-test' } ] ,
120- [ 'PATCH' , { result : 'patch-test' } ] ,
121- [ 'DELETE' , { result : 'delete-test' } ] ,
122- [ 'HEAD' , { result : 'head-test' } ] ,
123- [ 'OPTIONS' , { result : 'options-test' } ] ,
124- ] ) ( 'routes %s requests with decorators' , async ( method , expected ) => {
125- // Prepare
126- const lambda = new Lambda ( ) ;
127- // Act
128- const actual = await lambda . handler (
129- createEvent ( '/test' , method ) ,
130- context
131- ) ;
132- // Assess
133- expect ( actual . statusCode ) . toBe ( 200 ) ;
134- expect ( actual . body ) . toBe ( JSON . stringify ( expected ) ) ;
135- expect ( actual . headers ?. [ 'content-type' ] ) . toBe ( 'application/json' ) ;
136- expect ( actual . isBase64Encoded ) . toBe ( false ) ;
137- } ) ;
85+ ) ;
13886 } ) ;
13987
14088 describe ( 'decorators with middleware' , ( ) => {
0 commit comments