|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import context from '@aws-lambda-powertools/testing-utils/context'; |
| 3 | +import { cors } from '../../../../src/rest/middleware/cors.js'; |
| 4 | +import { createTestEvent, createHeaderCheckMiddleware } from '../helpers.js'; |
| 5 | +import { Router } from '../../../../src/rest/Router.js'; |
| 6 | +import { DEFAULT_CORS_OPTIONS } from 'src/rest/constants.js'; |
| 7 | + |
| 8 | +describe('CORS Middleware', () => { |
| 9 | + const getRequestEvent = createTestEvent('/test', 'GET'); |
| 10 | + const optionsRequestEvent = createTestEvent('/test', 'OPTIONS'); |
| 11 | + let app: Router; |
| 12 | + |
| 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 | + |
| 29 | + beforeEach(() => { |
| 30 | + app = new Router(); |
| 31 | + app.use(cors()); |
| 32 | + }); |
| 33 | + |
| 34 | + it('uses default configuration when no options are provided', async () => { |
| 35 | + // Prepare |
| 36 | + const corsHeaders: { [key: string]: string } = {}; |
| 37 | + app.get('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true })); |
| 38 | + |
| 39 | + // Act |
| 40 | + const result = await app.resolve(getRequestEvent, context); |
| 41 | + |
| 42 | + // Assess |
| 43 | + expect(result.headers?.['access-control-allow-origin']).toEqual(DEFAULT_CORS_OPTIONS.origin); |
| 44 | + expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(DEFAULT_CORS_OPTIONS.allowMethods); |
| 45 | + expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(DEFAULT_CORS_OPTIONS.allowHeaders); |
| 46 | + expect(result.headers?.['access-control-allow-credentials']).toEqual(DEFAULT_CORS_OPTIONS.credentials.toString()); |
| 47 | + expect(corsHeaders).toMatchObject(expectedDefaultHeaders); |
| 48 | + }); |
| 49 | + |
| 50 | + it('merges user options with defaults', async () => { |
| 51 | + // Prepare |
| 52 | + const corsHeaders: { [key: string]: string } = {}; |
| 53 | + const app = new Router(); |
| 54 | + app.get('/test', [cors(customCorsOptions), createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true })); |
| 55 | + |
| 56 | + // Act |
| 57 | + const result = await app.resolve(getRequestEvent, context); |
| 58 | + |
| 59 | + // Assess |
| 60 | + expect(result.headers?.['access-control-allow-origin']).toEqual('https://example.com'); |
| 61 | + expect(result.multiValueHeaders?.['access-control-allow-methods']).toEqual(['GET', 'POST']); |
| 62 | + expect(result.multiValueHeaders?.['access-control-allow-headers']).toEqual(['Authorization', 'Content-Type']); |
| 63 | + expect(result.headers?.['access-control-allow-credentials']).toEqual('true'); |
| 64 | + expect(result.multiValueHeaders?.['access-control-expose-headers']).toEqual(['Authorization', 'X-Custom-Header']); |
| 65 | + expect(result.headers?.['access-control-max-age']).toEqual('86400'); |
| 66 | + expect(corsHeaders).toMatchObject({ |
| 67 | + "access-control-allow-credentials": "true", |
| 68 | + "access-control-allow-headers": "Authorization, Content-Type", |
| 69 | + "access-control-allow-methods": "GET, POST", |
| 70 | + "access-control-allow-origin": "https://example.com", |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it.each([ |
| 75 | + ['matching', 'https://app.com', 'https://app.com'], |
| 76 | + ['non-matching', 'https://non-matching.com', ''] |
| 77 | + ])('handles array origin with %s request', async (_, origin, expected) => { |
| 78 | + // Prepare |
| 79 | + const app = new Router(); |
| 80 | + app.get('/test', [cors({ origin: ['https://app.com', 'https://admin.app.com'] })], async () => ({ success: true })); |
| 81 | + |
| 82 | + // Act |
| 83 | + const result = await app.resolve(createTestEvent('/test', 'GET', { 'Origin': origin }), context); |
| 84 | + |
| 85 | + // Assess |
| 86 | + expect(result.headers?.['access-control-allow-origin']).toEqual(expected); |
| 87 | + }); |
| 88 | + |
| 89 | + it('handles OPTIONS preflight requests', async () => { |
| 90 | + // Prepare |
| 91 | + app.options('/test', async () => ({ foo: 'bar' })); |
| 92 | + |
| 93 | + // Act |
| 94 | + const result = await app.resolve(createTestEvent('/test', 'OPTIONS', { 'Access-Control-Request-Method': 'GET' }), context); |
| 95 | + |
| 96 | + // Assess |
| 97 | + expect(result.statusCode).toBe(204); |
| 98 | + }); |
| 99 | + |
| 100 | + it('calls the next middleware if the Access-Control-Request-Method is not present', async () => { |
| 101 | + // Prepare |
| 102 | + const corsHeaders: { [key: string]: string } = {}; |
| 103 | + app.options('/test', [createHeaderCheckMiddleware(corsHeaders)], async () => ({ success: true })); |
| 104 | + |
| 105 | + // Act |
| 106 | + await app.resolve(optionsRequestEvent, context); |
| 107 | + |
| 108 | + // Assess |
| 109 | + expect(corsHeaders).toMatchObject(expectedDefaultHeaders); |
| 110 | + }); |
| 111 | +}); |
0 commit comments