@@ -10,84 +10,53 @@ describe('CORS Middleware', () => {
10
10
const optionsRequestEvent = createTestEvent ( '/test' , 'OPTIONS' ) ;
11
11
let app : Router ;
12
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
+
13
29
beforeEach ( ( ) => {
14
30
app = new Router ( ) ;
15
31
app . use ( cors ( ) ) ;
16
32
} ) ;
17
33
18
34
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
+
29
38
const result = await app . resolve ( getRequestEvent , context ) ;
30
39
31
- // Assess
32
40
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 ) ;
48
45
} ) ;
49
46
50
47
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 ) ;
73
53
74
- // Assess
75
54
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' ) ;
91
60
expect ( corsHeaders ) . toMatchObject ( {
92
61
"access-control-allow-credentials" : "true" ,
93
62
"access-control-allow-headers" : "Authorization, Content-Type" ,
@@ -96,100 +65,32 @@ describe('CORS Middleware', () => {
96
65
} ) ;
97
66
} ) ;
98
67
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 } ) ) ;
127
74
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 ) ;
155
78
} ) ;
156
79
157
80
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
+
171
85
expect ( result . statusCode ) . toBe ( 204 ) ;
172
86
} ) ;
173
87
174
88
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
+
185
92
await app . resolve ( optionsRequestEvent , context ) ;
186
93
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 ) ;
194
95
} ) ;
195
96
} ) ;
0 commit comments