Skip to content

Commit 180ae68

Browse files
committed
added the headers check middleware
1 parent dde1477 commit 180ae68

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

packages/event-handler/src/rest/middleware/cors.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ export const cors = (options?: CorsOptions): Middleware => {
9393
headers: reqCtx.res.headers,
9494
});
9595
}
96-
97-
// Continue to next middleware/handler
9896
await next();
9997
};
10098
};

packages/event-handler/tests/unit/rest/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,14 @@ export const createSettingHeadersMiddleware = (headers: {
7777
});
7878
};
7979
};
80+
81+
export const createHeaderCheckMiddleware = (headers: {
82+
[key: string]: string;
83+
}): Middleware => {
84+
return async (_params, options, next) => {
85+
options.res.headers.forEach((value, key) => {
86+
headers[key] = value;
87+
});
88+
await next();
89+
};
90+
};

packages/event-handler/tests/unit/rest/middleware/cors.test.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { beforeEach, describe, expect, it } from 'vitest';
22
import context from '@aws-lambda-powertools/testing-utils/context';
33
import { cors } from '../../../../src/rest/middleware/cors.js';
4-
import { createTestEvent, createTrackingMiddleware } from '../helpers.js';
4+
import { createTestEvent, createHeaderCheckMiddleware } from '../helpers.js';
55
import { Router } from '../../../../src/rest/Router.js';
66
import { DEFAULT_CORS_OPTIONS } from 'src/rest/constants.js';
77

@@ -17,12 +17,11 @@ describe('CORS Middleware', () => {
1717

1818
it('uses default configuration when no options are provided', async () => {
1919
// Prepare
20-
const executionOrder: string[] = [];
20+
const corsHeaders: { [key: string]: string; } = {};
2121
app.get(
2222
'/test',
23-
[createTrackingMiddleware('middleware1', executionOrder)],
23+
[createHeaderCheckMiddleware(corsHeaders)],
2424
async () => {
25-
executionOrder.push('handler');
2625
return { success: true };
2726
});
2827

@@ -40,18 +39,19 @@ describe('CORS Middleware', () => {
4039
expect(result.headers?.['access-control-allow-credentials']).toEqual(
4140
DEFAULT_CORS_OPTIONS.credentials.toString()
4241
);
43-
expect(executionOrder).toEqual([
44-
'middleware1-start',
45-
'handler',
46-
'middleware1-end',
47-
]);
42+
expect(corsHeaders).toMatchObject({
43+
"access-control-allow-credentials": "false",
44+
"access-control-allow-headers": "Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token",
45+
"access-control-allow-methods": "DELETE, GET, HEAD, PATCH, POST, PUT",
46+
"access-control-allow-origin": "*",
47+
});
4848
});
4949

5050
it('merges user options with defaults', async () => {
5151
// Prepare
52-
const executionOrder: string[] = [];
53-
const application = new Router();
54-
application.get(
52+
const corsHeaders: { [key: string]: string; } = {};
53+
const app = new Router();
54+
app.get(
5555
'/test',
5656
[
5757
cors({
@@ -62,15 +62,14 @@ describe('CORS Middleware', () => {
6262
exposeHeaders: ['Authorization', 'X-Custom-Header'],
6363
maxAge: 86400,
6464
}),
65-
createTrackingMiddleware('middleware1', executionOrder)
65+
createHeaderCheckMiddleware(corsHeaders)
6666
],
6767
async () => {
68-
executionOrder.push('handler');
6968
return { success: true };
7069
});
7170

7271
// Act
73-
const result = await application.resolve(getRequestEvent, context);
72+
const result = await app.resolve(getRequestEvent, context);
7473

7574
// Assess
7675
expect(result.headers?.['access-control-allow-origin']).toEqual('https://example.com');
@@ -89,18 +88,19 @@ describe('CORS Middleware', () => {
8988
expect(result.headers?.['access-control-max-age']).toEqual(
9089
'86400'
9190
);
92-
expect(executionOrder).toEqual([
93-
'middleware1-start',
94-
'handler',
95-
'middleware1-end',
96-
]);
91+
expect(corsHeaders).toMatchObject({
92+
"access-control-allow-credentials": "true",
93+
"access-control-allow-headers": "Authorization, Content-Type",
94+
"access-control-allow-methods": "GET, POST",
95+
"access-control-allow-origin": "https://example.com",
96+
});
9797
});
9898

9999
it('handles array origin with matching request', async () => {
100100
// Prepare
101101
const allowedOrigins = ['https://app.com', 'https://admin.app.com'];
102-
const application = new Router();
103-
application.get(
102+
const app = new Router();
103+
app.get(
104104
'/test',
105105
[
106106
cors({
@@ -117,7 +117,7 @@ describe('CORS Middleware', () => {
117117
});
118118

119119
// Act
120-
const result = await application.resolve(createTestEvent('/test', 'GET', {
120+
const result = await app.resolve(createTestEvent('/test', 'GET', {
121121
'Origin': 'https://app.com'
122122
}), context);
123123

@@ -128,8 +128,8 @@ describe('CORS Middleware', () => {
128128
it('handles array origin with non-matching request', async () => {
129129
// Prepare
130130
const allowedOrigins = ['https://app.com', 'https://admin.app.com'];
131-
const application = new Router();
132-
application.get(
131+
const app = new Router();
132+
app.get(
133133
'/test',
134134
[
135135
cors({
@@ -146,7 +146,7 @@ describe('CORS Middleware', () => {
146146
});
147147

148148
// Act
149-
const result = await application.resolve(createTestEvent('/test', 'GET', {
149+
const result = await app.resolve(createTestEvent('/test', 'GET', {
150150
'Origin': 'https://non-matching.com'
151151
}), context);
152152

@@ -173,23 +173,23 @@ describe('CORS Middleware', () => {
173173

174174
it('calls the next middleware if the Access-Control-Request-Method is not present', async () => {
175175
// Prepare
176-
const executionOrder: string[] = [];
176+
const corsHeaders: { [key: string]: string; } = {};
177177
app.options(
178178
'/test',
179-
[createTrackingMiddleware('middleware1', executionOrder)],
179+
[createHeaderCheckMiddleware(corsHeaders)],
180180
async () => {
181-
executionOrder.push('handler');
182181
return { success: true };
183182
});
184183

185184
// Act
186185
await app.resolve(optionsRequestEvent, context);
187186

188187
// Assess
189-
expect(executionOrder).toEqual([
190-
'middleware1-start',
191-
'handler',
192-
'middleware1-end',
193-
]);
188+
expect(corsHeaders).toMatchObject({
189+
"access-control-allow-credentials": "false",
190+
"access-control-allow-headers": "Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token",
191+
"access-control-allow-methods": "DELETE, GET, HEAD, PATCH, POST, PUT",
192+
"access-control-allow-origin": "*",
193+
});
194194
});
195195
});

0 commit comments

Comments
 (0)