Skip to content

Commit bec305c

Browse files
committed
Fixed test formats
1 parent f2fa687 commit bec305c

File tree

1 file changed

+21
-6
lines changed
  • packages/event-handler/tests/unit/rest/middleware

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ describe('CORS Middleware', () => {
3232
});
3333

3434
it('uses default configuration when no options are provided', async () => {
35+
// Prepare
3536
const corsHeaders: { [key: string]: string } = {};
3637
app.get('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
3738

39+
// Act
3840
const result = await app.resolve(getRequestEvent, context);
3941

42+
// Assess
4043
expect(result.headers?.['access-control-allow-origin']).toEqual(DEFAULT_CORS_OPTIONS.origin);
4144
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(DEFAULT_CORS_OPTIONS.allowMethods);
4245
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(DEFAULT_CORS_OPTIONS.allowHeaders);
@@ -45,12 +48,15 @@ describe('CORS Middleware', () => {
4548
});
4649

4750
it('merges user options with defaults', async () => {
51+
// Prepare
4852
const corsHeaders: { [key: string]: string } = {};
49-
const customApp = new Router();
50-
customApp.get('/test', [cors(customCorsOptions), createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
53+
const app = new Router();
54+
app.get('/test', [cors(customCorsOptions), createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
5155

52-
const result = await customApp.resolve(getRequestEvent, context);
56+
// Act
57+
const result = await app.resolve(getRequestEvent, context);
5358

59+
// Assess
5460
expect(result.headers?.['access-control-allow-origin']).toEqual('https://example.com');
5561
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(['GET', 'POST']);
5662
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(['Authorization', 'Content-Type']);
@@ -69,28 +75,37 @@ describe('CORS Middleware', () => {
6975
['matching', 'https://app.com', 'https://app.com'],
7076
['non-matching', 'https://non-matching.com', '']
7177
])('handles array origin with %s request', async (_, origin, expected) => {
72-
const customApp = new Router();
73-
customApp.get('/test', [cors({ origin: ['https://app.com', 'https://admin.app.com'] })], async () => ({ success: true }));
78+
// Prepare
79+
const app = new Router();
80+
app.get('/test', [cors({ origin: ['https://app.com', 'https://admin.app.com'] })], async () => ({ success: true }));
7481

75-
const result = await customApp.resolve(createTestEvent('/test', 'GET', { 'Origin': origin }), context);
82+
// Act
83+
const result = await app.resolve(createTestEvent('/test', 'GET', { 'Origin': origin }), context);
7684

85+
// Assess
7786
expect(result.headers?.['access-control-allow-origin']).toEqual(expected);
7887
});
7988

8089
it('handles OPTIONS preflight requests', async () => {
90+
// Prepare
8191
app.options('/test', async () => ({ foo: 'bar' }));
8292

93+
// Act
8394
const result = await app.resolve(createTestEvent('/test', 'OPTIONS', { 'Access-Control-Request-Method': 'GET' }), context);
8495

96+
// Assess
8597
expect(result.statusCode).toBe(204);
8698
});
8799

88100
it('calls the next middleware if the Access-Control-Request-Method is not present', async () => {
101+
// Prepare
89102
const corsHeaders: { [key: string]: string } = {};
90103
app.options('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
91104

105+
// Act
92106
await app.resolve(optionsRequestEvent, context);
93107

108+
// Assess
94109
expect(corsHeaders).toMatchObject(expectedDefaultHeaders);
95110
});
96111
});

0 commit comments

Comments
 (0)