Skip to content

Commit 4be1dce

Browse files
committed
address PR comments
1 parent fac9de0 commit 4be1dce

File tree

2 files changed

+84
-77
lines changed

2 files changed

+84
-77
lines changed

packages/event-handler/tests/unit/rest/BaseRouter.test.ts

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import context from '@aws-lambda-powertools/testing-utils/context';
2-
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
2+
import type { Context } from 'aws-lambda';
33
import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { BaseRouter } from '../../../src/rest/BaseRouter.js';
55
import { HttpErrorCodes, HttpVerbs } from '../../../src/rest/constants.js';
@@ -17,72 +17,13 @@ import type {
1717
RouteHandler,
1818
RouterOptions,
1919
} from '../../../src/types/rest.js';
20-
21-
const createTestEvent = (
22-
path: string,
23-
httpMethod: string
24-
): APIGatewayProxyEvent => ({
25-
path,
26-
httpMethod,
27-
headers: {},
28-
body: null,
29-
multiValueHeaders: {},
30-
isBase64Encoded: false,
31-
pathParameters: null,
32-
queryStringParameters: null,
33-
multiValueQueryStringParameters: null,
34-
stageVariables: null,
35-
requestContext: {
36-
httpMethod,
37-
path,
38-
domainName: 'localhost',
39-
} as any,
40-
resource: '',
41-
});
42-
43-
// Middleware factory functions
44-
const createTrackingMiddleware = (
45-
name: string,
46-
executionOrder: string[]
47-
): Middleware => {
48-
return async (_params, _options, next) => {
49-
executionOrder.push(`${name}-start`);
50-
await next();
51-
executionOrder.push(`${name}-end`);
52-
};
53-
};
54-
55-
const createThrowingMiddleware = (
56-
name: string,
57-
executionOrder: string[],
58-
errorMessage: string
59-
): Middleware => {
60-
return async (_params, _options, _next) => {
61-
executionOrder.push(name);
62-
throw new Error(errorMessage);
63-
};
64-
};
65-
66-
const createReturningMiddleware = (
67-
name: string,
68-
executionOrder: string[],
69-
response: any
70-
): Middleware => {
71-
return async (_params, _options, _next) => {
72-
executionOrder.push(name);
73-
return response;
74-
};
75-
};
76-
77-
const createNoNextMiddleware = (
78-
name: string,
79-
executionOrder: string[]
80-
): Middleware => {
81-
return async (_params, _options, _next) => {
82-
executionOrder.push(name);
83-
// Intentionally doesn't call next()
84-
};
85-
};
20+
import {
21+
createNoNextMiddleware,
22+
createReturningMiddleware,
23+
createTestEvent,
24+
createThrowingMiddleware,
25+
createTrackingMiddleware,
26+
} from './helpers.js';
8627

8728
describe('Class: BaseRouter', () => {
8829
class TestResolver extends BaseRouter {
@@ -493,7 +434,6 @@ describe('Class: BaseRouter', () => {
493434
// Prepare
494435
const app = new TestResolver();
495436
const executionOrder: string[] = [];
496-
let middlewareScope: any;
497437

498438
app.use(async (params, options, next) => {
499439
executionOrder.push('middleware-start');
@@ -507,7 +447,6 @@ describe('Class: BaseRouter', () => {
507447
@app.get('/test')
508448
public async getTest() {
509449
executionOrder.push('handler');
510-
middlewareScope = this.scope;
511450
return { message: `${this.scope}: success` };
512451
}
513452

@@ -529,7 +468,6 @@ describe('Class: BaseRouter', () => {
529468
'handler',
530469
'middleware-end',
531470
]);
532-
expect(middlewareScope).toBe('class-scope');
533471
expect(result).toEqual({
534472
statusCode: 200,
535473
body: JSON.stringify({ message: 'class-scope: success' }),
@@ -1032,24 +970,24 @@ describe('Class: BaseRouter', () => {
1032970
);
1033971

1034972
class Lambda {
973+
public scope = 'class-scope';
974+
1035975
@app.get('/test', [middleware])
1036976
public async getTest() {
1037977
executionOrder.push('handler');
1038-
return { result: 'decorator-with-middleware' };
978+
return { result: `${this.scope}: decorator-with-middleware` };
1039979
}
1040980

1041981
public async handler(event: unknown, context: Context) {
1042-
return app.resolve(event, context);
982+
return app.resolve(event, context, { scope: this });
1043983
}
1044984
}
1045985

1046986
const lambda = new Lambda();
987+
const handler = lambda.handler.bind(lambda);
1047988

1048989
// Act
1049-
const result = await lambda.handler(
1050-
createTestEvent('/test', 'GET'),
1051-
context
1052-
);
990+
const result = await handler(createTestEvent('/test', 'GET'), context);
1053991

1054992
// Assess
1055993
expect(executionOrder).toEqual([
@@ -1059,7 +997,9 @@ describe('Class: BaseRouter', () => {
1059997
]);
1060998
expect(result).toEqual({
1061999
statusCode: 200,
1062-
body: JSON.stringify({ result: 'decorator-with-middleware' }),
1000+
body: JSON.stringify({
1001+
result: 'class-scope: decorator-with-middleware',
1002+
}),
10631003
headers: { 'Content-Type': 'application/json' },
10641004
isBase64Encoded: false,
10651005
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { APIGatewayProxyEvent } from 'aws-lambda';
2+
import type { Middleware } from '../../../src/types/rest.js';
3+
4+
export const createTestEvent = (
5+
path: string,
6+
httpMethod: string
7+
): APIGatewayProxyEvent => ({
8+
path,
9+
httpMethod,
10+
headers: {},
11+
body: null,
12+
multiValueHeaders: {},
13+
isBase64Encoded: false,
14+
pathParameters: null,
15+
queryStringParameters: null,
16+
multiValueQueryStringParameters: null,
17+
stageVariables: null,
18+
requestContext: {
19+
httpMethod,
20+
path,
21+
domainName: 'localhost',
22+
} as any,
23+
resource: '',
24+
});
25+
26+
export const createTrackingMiddleware = (
27+
name: string,
28+
executionOrder: string[]
29+
): Middleware => {
30+
return async (_params, _options, next) => {
31+
executionOrder.push(`${name}-start`);
32+
await next();
33+
executionOrder.push(`${name}-end`);
34+
};
35+
};
36+
37+
export const createThrowingMiddleware = (
38+
name: string,
39+
executionOrder: string[],
40+
errorMessage: string
41+
): Middleware => {
42+
return async (_params, _options, _next) => {
43+
executionOrder.push(name);
44+
throw new Error(errorMessage);
45+
};
46+
};
47+
48+
export const createReturningMiddleware = (
49+
name: string,
50+
executionOrder: string[],
51+
response: any
52+
): Middleware => {
53+
return async (_params, _options, _next) => {
54+
executionOrder.push(name);
55+
return response;
56+
};
57+
};
58+
59+
export const createNoNextMiddleware = (
60+
name: string,
61+
executionOrder: string[]
62+
): Middleware => {
63+
return async (_params, _options, _next) => {
64+
executionOrder.push(name);
65+
// Intentionally doesn't call next()
66+
};
67+
};

0 commit comments

Comments
 (0)