Skip to content

Commit f2fa687

Browse files
committed
optimized tests
1 parent 180ae68 commit f2fa687

File tree

1 file changed

+50
-149
lines changed
  • packages/event-handler/tests/unit/rest/middleware

1 file changed

+50
-149
lines changed

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

Lines changed: 50 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,53 @@ describe('CORS Middleware', () => {
1010
const optionsRequestEvent = createTestEvent('/test', 'OPTIONS');
1111
let app: Router;
1212

13+
const customCorsOptions = {
14+
origin: 'https://example.com',
15+
allowMethods: ['GET', 'POST'],
16+
allowHeaders: ['Authorization', 'Content-Type'],
17+
credentials: true,
18+
exposeHeaders: ['Authorization', 'X-Custom-Header'],
19+
maxAge: 86400,
20+
};
21+
22+
const expectedDefaultHeaders = {
23+
"access-control-allow-credentials": "false",
24+
"access-control-allow-headers": "Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token",
25+
"access-control-allow-methods": "DELETE, GET, HEAD, PATCH, POST, PUT",
26+
"access-control-allow-origin": "*",
27+
};
28+
1329
beforeEach(() => {
1430
app = new Router();
1531
app.use(cors());
1632
});
1733

1834
it('uses default configuration when no options are provided', async () => {
19-
// Prepare
20-
const corsHeaders: { [key: string]: string; } = {};
21-
app.get(
22-
'/test',
23-
[createHeaderCheckMiddleware(corsHeaders)],
24-
async () => {
25-
return { success: true };
26-
});
27-
28-
// Act
35+
const corsHeaders: { [key: string]: string } = {};
36+
app.get('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
37+
2938
const result = await app.resolve(getRequestEvent, context);
3039

31-
// Assess
3240
expect(result.headers?.['access-control-allow-origin']).toEqual(DEFAULT_CORS_OPTIONS.origin);
33-
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(
34-
DEFAULT_CORS_OPTIONS.allowMethods
35-
);
36-
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(
37-
DEFAULT_CORS_OPTIONS.allowHeaders
38-
);
39-
expect(result.headers?.['access-control-allow-credentials']).toEqual(
40-
DEFAULT_CORS_OPTIONS.credentials.toString()
41-
);
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-
});
41+
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(DEFAULT_CORS_OPTIONS.allowMethods);
42+
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(DEFAULT_CORS_OPTIONS.allowHeaders);
43+
expect(result.headers?.['access-control-allow-credentials']).toEqual(DEFAULT_CORS_OPTIONS.credentials.toString());
44+
expect(corsHeaders).toMatchObject(expectedDefaultHeaders);
4845
});
4946

5047
it('merges user options with defaults', async () => {
51-
// Prepare
52-
const corsHeaders: { [key: string]: string; } = {};
53-
const app = new Router();
54-
app.get(
55-
'/test',
56-
[
57-
cors({
58-
origin: 'https://example.com',
59-
allowMethods: ['GET', 'POST'],
60-
allowHeaders: ['Authorization', 'Content-Type'],
61-
credentials: true,
62-
exposeHeaders: ['Authorization', 'X-Custom-Header'],
63-
maxAge: 86400,
64-
}),
65-
createHeaderCheckMiddleware(corsHeaders)
66-
],
67-
async () => {
68-
return { success: true };
69-
});
70-
71-
// Act
72-
const result = await app.resolve(getRequestEvent, context);
48+
const corsHeaders: { [key: string]: string } = {};
49+
const customApp = new Router();
50+
customApp.get('/test', [cors(customCorsOptions), createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
51+
52+
const result = await customApp.resolve(getRequestEvent, context);
7353

74-
// Assess
7554
expect(result.headers?.['access-control-allow-origin']).toEqual('https://example.com');
76-
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(
77-
['GET', 'POST']
78-
);
79-
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(
80-
['Authorization', 'Content-Type']
81-
);
82-
expect(result.headers?.['access-control-allow-credentials']).toEqual(
83-
'true'
84-
);
85-
expect(result.multiValueHeaders?.['access-control-expose-headers']).toEqual(
86-
['Authorization', 'X-Custom-Header']
87-
);
88-
expect(result.headers?.['access-control-max-age']).toEqual(
89-
'86400'
90-
);
55+
expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(['GET', 'POST']);
56+
expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(['Authorization', 'Content-Type']);
57+
expect(result.headers?.['access-control-allow-credentials']).toEqual('true');
58+
expect(result.multiValueHeaders?.['access-control-expose-headers']).toEqual(['Authorization', 'X-Custom-Header']);
59+
expect(result.headers?.['access-control-max-age']).toEqual('86400');
9160
expect(corsHeaders).toMatchObject({
9261
"access-control-allow-credentials": "true",
9362
"access-control-allow-headers": "Authorization, Content-Type",
@@ -96,100 +65,32 @@ describe('CORS Middleware', () => {
9665
});
9766
});
9867

99-
it('handles array origin with matching request', async () => {
100-
// Prepare
101-
const allowedOrigins = ['https://app.com', 'https://admin.app.com'];
102-
const app = new Router();
103-
app.get(
104-
'/test',
105-
[
106-
cors({
107-
origin: allowedOrigins,
108-
allowMethods: ['GET', 'POST'],
109-
allowHeaders: ['Authorization', 'Content-Type'],
110-
credentials: true,
111-
exposeHeaders: ['Authorization', 'X-Custom-Header'],
112-
maxAge: 86400,
113-
}),
114-
],
115-
async () => {
116-
return { success: true };
117-
});
118-
119-
// Act
120-
const result = await app.resolve(createTestEvent('/test', 'GET', {
121-
'Origin': 'https://app.com'
122-
}), context);
123-
124-
// Assess
125-
expect(result.headers?.['access-control-allow-origin']).toEqual('https://app.com');
126-
});
68+
it.each([
69+
['matching', 'https://app.com', 'https://app.com'],
70+
['non-matching', 'https://non-matching.com', '']
71+
])('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 }));
12774

128-
it('handles array origin with non-matching request', async () => {
129-
// Prepare
130-
const allowedOrigins = ['https://app.com', 'https://admin.app.com'];
131-
const app = new Router();
132-
app.get(
133-
'/test',
134-
[
135-
cors({
136-
origin: allowedOrigins,
137-
allowMethods: ['GET', 'POST'],
138-
allowHeaders: ['Authorization', 'Content-Type'],
139-
credentials: true,
140-
exposeHeaders: ['Authorization', 'X-Custom-Header'],
141-
maxAge: 86400,
142-
}),
143-
],
144-
async () => {
145-
return { success: true };
146-
});
147-
148-
// Act
149-
const result = await app.resolve(createTestEvent('/test', 'GET', {
150-
'Origin': 'https://non-matching.com'
151-
}), context);
152-
153-
// Assess
154-
expect(result.headers?.['access-control-allow-origin']).toEqual('');
75+
const result = await customApp.resolve(createTestEvent('/test', 'GET', { 'Origin': origin }), context);
76+
77+
expect(result.headers?.['access-control-allow-origin']).toEqual(expected);
15578
});
15679

15780
it('handles OPTIONS preflight requests', async () => {
158-
// Prepare
159-
app.options(
160-
'/test',
161-
async () => {
162-
return { foo: 'bar' };
163-
});
164-
165-
// Act
166-
const result = await app.resolve(createTestEvent('/test', 'OPTIONS', {
167-
'Access-Control-Request-Method': 'GET'
168-
}), context);
169-
170-
// Assess
81+
app.options('/test', async () => ({ foo: 'bar' }));
82+
83+
const result = await app.resolve(createTestEvent('/test', 'OPTIONS', { 'Access-Control-Request-Method': 'GET' }), context);
84+
17185
expect(result.statusCode).toBe(204);
17286
});
17387

17488
it('calls the next middleware if the Access-Control-Request-Method is not present', async () => {
175-
// Prepare
176-
const corsHeaders: { [key: string]: string; } = {};
177-
app.options(
178-
'/test',
179-
[createHeaderCheckMiddleware(corsHeaders)],
180-
async () => {
181-
return { success: true };
182-
});
183-
184-
// Act
89+
const corsHeaders: { [key: string]: string } = {};
90+
app.options('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true }));
91+
18592
await app.resolve(optionsRequestEvent, context);
18693

187-
// Assess
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-
});
94+
expect(corsHeaders).toMatchObject(expectedDefaultHeaders);
19495
});
19596
});

0 commit comments

Comments
 (0)