File tree Expand file tree Collapse file tree 2 files changed +16
-27
lines changed
tests/unit/rest/middleware Expand file tree Collapse file tree 2 files changed +16
-27
lines changed Original file line number Diff line number Diff line change @@ -64,19 +64,26 @@ export const cors = (options?: CorsOptions): Middleware => {
64
64
const isOptions = reqCtx . request . method === HttpVerbs . OPTIONS ;
65
65
// Handle preflight OPTIONS request
66
66
if ( isOptions ) {
67
- const requestMethod = reqCtx . request . headers . get (
68
- 'Access-Control-Request-Method'
69
- ) ;
70
- const requestHeaders = reqCtx . request . headers . get (
71
- 'Access-Control-Request-Headers'
72
- ) ;
67
+ const requestMethod = reqCtx . request . headers
68
+ . get ( 'Access-Control-Request-Method' )
69
+ ?. toUpperCase ( ) ;
70
+ const requestHeaders = reqCtx . request . headers
71
+ . get ( 'Access-Control-Request-Headers' )
72
+ ?. toLowerCase ( ) ;
73
73
if (
74
74
! requestMethod ||
75
- ! config . allowMethods . includes ( requestMethod ) ||
75
+ ! config . allowMethods
76
+ . map ( ( m ) => m . toUpperCase ( ) )
77
+ . includes ( requestMethod ) ||
76
78
! requestHeaders ||
77
79
requestHeaders
78
80
. split ( ',' )
79
- . some ( ( header ) => ! config . allowHeaders . includes ( header . trim ( ) ) )
81
+ . some (
82
+ ( header ) =>
83
+ ! config . allowHeaders
84
+ . map ( ( h ) => h . toLowerCase ( ) )
85
+ . includes ( header . trim ( ) )
86
+ )
80
87
) {
81
88
await next ( ) ;
82
89
return ;
Original file line number Diff line number Diff line change @@ -126,15 +126,6 @@ describe('CORS Middleware', () => {
126
126
it ( 'does not set CORS headers when preflight request method does not match allowed method' , async ( ) => {
127
127
// Prepare
128
128
const app = new Router ( ) ;
129
- app . options (
130
- '/test' ,
131
- [
132
- cors ( {
133
- allowMethods : [ 'POST' ] ,
134
- } ) ,
135
- ] ,
136
- async ( ) => ( { foo : 'bar' } )
137
- ) ;
138
129
139
130
// Act
140
131
const result = await app . resolve (
@@ -152,15 +143,6 @@ describe('CORS Middleware', () => {
152
143
it ( 'does not set CORS headers when preflight request header does not match allowed header' , async ( ) => {
153
144
// Prepare
154
145
const app = new Router ( ) ;
155
- app . options (
156
- '/test' ,
157
- [
158
- cors ( {
159
- allowHeaders : [ 'Authorization' ] ,
160
- } ) ,
161
- ] ,
162
- async ( ) => ( { foo : 'bar' } )
163
- ) ;
164
146
165
147
// Act
166
148
const result = await app . resolve (
@@ -184,7 +166,7 @@ describe('CORS Middleware', () => {
184
166
allowHeaders : [ 'Authorization' , 'Content-Type' ] ,
185
167
maxAge : 3600 ,
186
168
} ;
187
- app . options ( '/test' , [ cors ( corsConfig ) ] , async ( ) => ( { foo : 'bar' } ) ) ;
169
+ app . use ( cors ( corsConfig ) ) ;
188
170
189
171
// Act
190
172
const result = await app . resolve (
You can’t perform that action at this time.
0 commit comments