You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Access to fetch at '${response.url}' from origin '${origin}' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.`,
123
+
thrownewCorsError(
124
+
response.url,
125
+
origin,
126
+
"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."
103
127
);
104
128
}
105
129
106
130
if(originHeader!=="*"&&originHeader!==origin){
107
-
thrownewError(
108
-
`Access to fetch at '${response.url}' from origin '${origin}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value '${originHeader}' that is not equal to the supplied origin.`,
131
+
thrownewCorsError(
132
+
response.url,
133
+
origin,
134
+
`The 'Access-Control-Allow-Origin' header has a value '${originHeader}' that is not equal to the supplied origin.`,
109
135
);
110
136
}
111
137
@@ -234,29 +260,39 @@ export class CorsPreflightData {
234
260
235
261
/**
236
262
* Validates a method against the preflight data.
237
-
* @param {string} method The method to validate.
263
+
* @param {Request} request The request with the method to validate.
264
+
* @param {string} origin The origin of the request.
238
265
* @returns {void}
239
266
* @throws {Error} When the method is not allowed.
240
267
*/
241
-
#validateMethod(method){
268
+
#validateMethod(request,origin){
269
+
270
+
constmethod=request.method.toUpperCase();
271
+
242
272
if(
243
273
!this.allowAllMethods&&
244
274
!corsSafeMethods.has(method)&&
245
275
!this.allowedMethods.has(method)
246
276
){
247
-
thrownewError(
248
-
`Request is blocked by CORS policy: Method ${method} is not allowed.`,
277
+
thrownewCorsError(
278
+
request.url,
279
+
origin,
280
+
`Method ${method} is not allowed.`,
249
281
);
250
282
}
251
283
}
252
284
253
285
/**
254
286
* Validates a set of headers against the preflight data.
255
-
* @param {Headers} headers The headers to validate.
287
+
* @param {Request} request The request with headers to validate.
288
+
* @param {string} origin The origin of the request.
256
289
* @returns {void}
257
290
* @throws {Error} When the headers are not allowed.
258
291
*/
259
-
#validateHeaders(headers){
292
+
#validateHeaders(request,origin){
293
+
294
+
const{ headers }=request;
295
+
260
296
for(constheaderofheaders.keys()){
261
297
// simple headers are always allowed
262
298
if(corsSafeHeaders.has(header)){
@@ -268,14 +304,18 @@ export class CorsPreflightData {
268
304
header==="authorization"&&
269
305
!this.allowedHeaders.has(header)
270
306
){
271
-
thrownewError(
272
-
`Request is blocked by CORS policy: Header ${header} is not allowed.`,
// if the preflight response is successful, then we can make the actual request
317
318
if(!preflightResponse.ok){
318
-
thrownewError(
319
-
`Request to ${preflightRequest.url} from ${this.#baseUrl.origin} is blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.`,
320
-
);
319
+
thrownewCorsError(preflightRequest.url,this.#baseUrl.origin,"Response to preflight request doesn't pass access control check: It does not have HTTP ok status.");
Copy file name to clipboardExpand all lines: tests/fetch-mocker.test.js
+6-7
Original file line number
Diff line number
Diff line change
@@ -74,7 +74,7 @@ Partial matches:
74
74
❌ Headers do not match. Expected authorization=Bearer ABC but received authorization=Bearer XYZ.`.trim();
75
75
76
76
constPREFLIGHT_FAILED=`
77
-
Request to https://api.example.com/hello from https://api.example.org is blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
77
+
Access to fetch at 'https://api.example.com/hello' from origin 'https://api.example.org' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
message: `Access to fetch at '${url.href}' from origin '${origin}' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.`,
message: `Access to fetch at '${url.href}' from origin '${origin}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://api.example.com' that is not equal to the supplied origin.`,
message: `Access to fetch at '${url.href}' from origin '${origin}' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.`,
message: `Access to fetch at '${url.href}' from origin '${origin}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://api.example.com' that is not equal to the supplied origin.`,
0 commit comments