1
1
import { beforeEach , describe , expect , it } from 'vitest' ;
2
2
import context from '@aws-lambda-powertools/testing-utils/context' ;
3
3
import { cors } from '../../../../src/rest/middleware/cors.js' ;
4
- import { createTestEvent , createTrackingMiddleware } from '../helpers.js' ;
4
+ import { createTestEvent , createHeaderCheckMiddleware } from '../helpers.js' ;
5
5
import { Router } from '../../../../src/rest/Router.js' ;
6
6
import { DEFAULT_CORS_OPTIONS } from 'src/rest/constants.js' ;
7
7
@@ -17,12 +17,11 @@ describe('CORS Middleware', () => {
17
17
18
18
it ( 'uses default configuration when no options are provided' , async ( ) => {
19
19
// Prepare
20
- const executionOrder : string [ ] = [ ] ;
20
+ const corsHeaders : { [ key : string ] : string ; } = { } ;
21
21
app . get (
22
22
'/test' ,
23
- [ createTrackingMiddleware ( 'middleware1' , executionOrder ) ] ,
23
+ [ createHeaderCheckMiddleware ( corsHeaders ) ] ,
24
24
async ( ) => {
25
- executionOrder . push ( 'handler' ) ;
26
25
return { success : true } ;
27
26
} ) ;
28
27
@@ -40,18 +39,19 @@ describe('CORS Middleware', () => {
40
39
expect ( result . headers ?. [ 'access-control-allow-credentials' ] ) . toEqual (
41
40
DEFAULT_CORS_OPTIONS . credentials . toString ( )
42
41
) ;
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
+ } ) ;
48
48
} ) ;
49
49
50
50
it ( 'merges user options with defaults' , async ( ) => {
51
51
// 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 (
55
55
'/test' ,
56
56
[
57
57
cors ( {
@@ -62,15 +62,14 @@ describe('CORS Middleware', () => {
62
62
exposeHeaders : [ 'Authorization' , 'X-Custom-Header' ] ,
63
63
maxAge : 86400 ,
64
64
} ) ,
65
- createTrackingMiddleware ( 'middleware1' , executionOrder )
65
+ createHeaderCheckMiddleware ( corsHeaders )
66
66
] ,
67
67
async ( ) => {
68
- executionOrder . push ( 'handler' ) ;
69
68
return { success : true } ;
70
69
} ) ;
71
70
72
71
// Act
73
- const result = await application . resolve ( getRequestEvent , context ) ;
72
+ const result = await app . resolve ( getRequestEvent , context ) ;
74
73
75
74
// Assess
76
75
expect ( result . headers ?. [ 'access-control-allow-origin' ] ) . toEqual ( 'https://example.com' ) ;
@@ -89,18 +88,19 @@ describe('CORS Middleware', () => {
89
88
expect ( result . headers ?. [ 'access-control-max-age' ] ) . toEqual (
90
89
'86400'
91
90
) ;
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
+ } ) ;
97
97
} ) ;
98
98
99
99
it ( 'handles array origin with matching request' , async ( ) => {
100
100
// Prepare
101
101
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 (
104
104
'/test' ,
105
105
[
106
106
cors ( {
@@ -117,7 +117,7 @@ describe('CORS Middleware', () => {
117
117
} ) ;
118
118
119
119
// Act
120
- const result = await application . resolve ( createTestEvent ( '/test' , 'GET' , {
120
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' , {
121
121
'Origin' : 'https://app.com'
122
122
} ) , context ) ;
123
123
@@ -128,8 +128,8 @@ describe('CORS Middleware', () => {
128
128
it ( 'handles array origin with non-matching request' , async ( ) => {
129
129
// Prepare
130
130
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 (
133
133
'/test' ,
134
134
[
135
135
cors ( {
@@ -146,7 +146,7 @@ describe('CORS Middleware', () => {
146
146
} ) ;
147
147
148
148
// Act
149
- const result = await application . resolve ( createTestEvent ( '/test' , 'GET' , {
149
+ const result = await app . resolve ( createTestEvent ( '/test' , 'GET' , {
150
150
'Origin' : 'https://non-matching.com'
151
151
} ) , context ) ;
152
152
@@ -173,23 +173,23 @@ describe('CORS Middleware', () => {
173
173
174
174
it ( 'calls the next middleware if the Access-Control-Request-Method is not present' , async ( ) => {
175
175
// Prepare
176
- const executionOrder : string [ ] = [ ] ;
176
+ const corsHeaders : { [ key : string ] : string ; } = { } ;
177
177
app . options (
178
178
'/test' ,
179
- [ createTrackingMiddleware ( 'middleware1' , executionOrder ) ] ,
179
+ [ createHeaderCheckMiddleware ( corsHeaders ) ] ,
180
180
async ( ) => {
181
- executionOrder . push ( 'handler' ) ;
182
181
return { success : true } ;
183
182
} ) ;
184
183
185
184
// Act
186
185
await app . resolve ( optionsRequestEvent , context ) ;
187
186
188
187
// 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
+ } ) ;
194
194
} ) ;
195
195
} ) ;
0 commit comments