-
Notifications
You must be signed in to change notification settings - Fork 176
fix(event-handler): allow event handler response to return array #4725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1c1b1f1
e1d5afc
4517ec3
b50a14d
5d2d7b3
829e20a
ef33697
6c2ad1f
cbd789d
a1ca85b
4605774
1f262af
d2d98f6
849dab1
f94addd
fb1ea55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,74 +67,111 @@ const createStreamHandler = | |
| (event: unknown, _context: Context, responseStream: MockResponseStream) => | ||
| app.resolveStream(event, _context, { scope, responseStream }); | ||
|
|
||
| const createTestLambdaClass = (app: Router, expectedResponse: unknown) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this function to the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we’ll need to move Should we also move the other helper handlers from the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think that's fine. I had been meaning to tidy up the decorator tests anyway so now is a good a time as any. |
||
| class Lambda { | ||
| @app.get('/test') | ||
| public getTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.post('/test') | ||
| public postTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.put('/test') | ||
| public putTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.patch('/test') | ||
| public patchTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.delete('/test') | ||
| public deleteTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.head('/test') | ||
| public headTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| @app.options('/test') | ||
| public optionsTest() { | ||
| return expectedResponse; | ||
| } | ||
|
|
||
| public handler = createHandler(app); | ||
| } | ||
|
|
||
| return Lambda; | ||
| }; | ||
|
|
||
| describe.each([ | ||
| { version: 'V1', createEvent: createTestEvent }, | ||
| { version: 'V2', createEvent: createTestEventV2 }, | ||
| ])('Class: Router - Decorators ($version)', ({ createEvent }) => { | ||
| describe('decorators', () => { | ||
| const app = new Router(); | ||
|
|
||
| class Lambda { | ||
| @app.get('/test') | ||
| public getTest() { | ||
| return { result: 'get-test' }; | ||
| } | ||
|
|
||
| @app.post('/test') | ||
| public postTest() { | ||
| return { result: 'post-test' }; | ||
| } | ||
| const httpMethods = [ | ||
| ['GET', 'get'], | ||
| ['POST', 'post'], | ||
| ['PUT', 'put'], | ||
| ['PATCH', 'patch'], | ||
| ['DELETE', 'delete'], | ||
| ['HEAD', 'head'], | ||
| ['OPTIONS', 'options'], | ||
| ]; | ||
| it.each(httpMethods)( | ||
| 'routes %s requests with object response', | ||
| async (method, verb) => { | ||
| // Prepare | ||
| const app = new Router(); | ||
| const expected = { result: `${verb}-test` }; | ||
| const Lambda = createTestLambdaClass(app, expected); | ||
| const lambda = new Lambda(); | ||
|
|
||
| @app.put('/test') | ||
| public putTest() { | ||
| return { result: 'put-test' }; | ||
| } | ||
| // Act | ||
| const actual = await lambda.handler( | ||
| createTestEvent('/test', method), | ||
| context | ||
| ); | ||
|
|
||
| @app.patch('/test') | ||
| public patchTest() { | ||
| return { result: 'patch-test' }; | ||
| // Assess | ||
| expect(actual.statusCode).toBe(200); | ||
| expect(actual.body).toBe(JSON.stringify(expected)); | ||
| expect(actual.headers?.['content-type']).toBe('application/json'); | ||
| expect(actual.isBase64Encoded).toBe(false); | ||
| } | ||
| ); | ||
|
|
||
| @app.delete('/test') | ||
| public deleteTest() { | ||
| return { result: 'delete-test' }; | ||
| } | ||
| it.each(httpMethods)( | ||
| 'routes %s requests with array response', | ||
| async (method, verb) => { | ||
| // Prepare | ||
| const app = new Router(); | ||
| const expected = [ | ||
| { id: 1, result: `${verb}-test-1` }, | ||
| { id: 2, result: `${verb}-test-2` }, | ||
| ]; | ||
| const Lambda = createTestLambdaClass(app, expected); | ||
| const lambda = new Lambda(); | ||
|
|
||
| @app.head('/test') | ||
| public headTest() { | ||
| return { result: 'head-test' }; | ||
| } | ||
| // Act | ||
| const actual = await lambda.handler( | ||
| createTestEvent('/test', method), | ||
| context | ||
| ); | ||
|
|
||
| @app.options('/test') | ||
| public optionsTest() { | ||
| return { result: 'options-test' }; | ||
| // Assess | ||
| expect(actual.statusCode).toBe(200); | ||
| expect(actual.body).toBe(JSON.stringify(expected)); | ||
| expect(actual.headers?.['content-type']).toBe('application/json'); | ||
| expect(actual.isBase64Encoded).toBe(false); | ||
| } | ||
|
|
||
| public handler = createHandler(app); | ||
| } | ||
|
|
||
| it.each([ | ||
| ['GET', { result: 'get-test' }], | ||
| ['POST', { result: 'post-test' }], | ||
| ['PUT', { result: 'put-test' }], | ||
| ['PATCH', { result: 'patch-test' }], | ||
| ['DELETE', { result: 'delete-test' }], | ||
| ['HEAD', { result: 'head-test' }], | ||
| ['OPTIONS', { result: 'options-test' }], | ||
| ])('routes %s requests with decorators', async (method, expected) => { | ||
| // Prepare | ||
| const lambda = new Lambda(); | ||
| // Act | ||
| const actual = await lambda.handler( | ||
| createEvent('/test', method), | ||
| context | ||
| ); | ||
| // Assess | ||
| expect(actual.statusCode).toBe(200); | ||
| expect(actual.body).toBe(JSON.stringify(expected)); | ||
| expect(actual.headers?.['content-type']).toBe('application/json'); | ||
| expect(actual.isBase64Encoded).toBe(false); | ||
| }); | ||
| ); | ||
| }); | ||
|
|
||
| describe('decorators with middleware', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this
errorBodyToWebResponse.