|
2 | 2 | * @fileoverview Tests for the CORS utilities.
|
3 | 3 | * @autor Nicholas C. Zakas
|
4 | 4 | */
|
5 |
| -/* global Request, Headers */ |
| 5 | +/* global Request, Headers, Response */ |
6 | 6 |
|
7 | 7 | //-----------------------------------------------------------------------------
|
8 | 8 | // Imports
|
9 | 9 | //-----------------------------------------------------------------------------
|
10 | 10 |
|
11 | 11 | import assert from "node:assert";
|
12 |
| -import { isCorsSimpleRequest, getUnsafeHeaders } from "../src/cors.js"; |
| 12 | +import { isCorsSimpleRequest, getUnsafeHeaders, assertCorsResponse } from "../src/cors.js"; |
13 | 13 |
|
14 | 14 | //-----------------------------------------------------------------------------
|
15 | 15 | // Tests
|
@@ -251,4 +251,98 @@ describe("http", () => {
|
251 | 251 | );
|
252 | 252 | });
|
253 | 253 | });
|
| 254 | + |
| 255 | + describe("assertCorsResponse()", () => { |
| 256 | + it("should not throw when Access-Control-Allow-Origin is *", () => { |
| 257 | + const headers = new Headers({ |
| 258 | + "Access-Control-Allow-Origin": "*" |
| 259 | + }); |
| 260 | + const response = new Response(null, { headers }); |
| 261 | + |
| 262 | + assert.doesNotThrow(() => { |
| 263 | + assertCorsResponse(response, "https://example.com"); |
| 264 | + }); |
| 265 | + }); |
| 266 | + |
| 267 | + it("should not throw when Access-Control-Allow-Origin matches origin", () => { |
| 268 | + const origin = "https://example.com"; |
| 269 | + const headers = new Headers({ |
| 270 | + "Access-Control-Allow-Origin": origin |
| 271 | + }); |
| 272 | + const response = new Response(null, { headers }); |
| 273 | + |
| 274 | + assert.doesNotThrow(() => { |
| 275 | + assertCorsResponse(response, origin); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it("should throw when Access-Control-Allow-Origin header is missing", () => { |
| 280 | + const response = new Response(); |
| 281 | + const origin = "https://example.com"; |
| 282 | + |
| 283 | + assert.throws(() => { |
| 284 | + assertCorsResponse(response, origin); |
| 285 | + }, { |
| 286 | + name: "CorsError", |
| 287 | + message: `Access to fetch at '${response.url}' from origin '${origin}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.` |
| 288 | + }); |
| 289 | + }); |
| 290 | + |
| 291 | + it("should throw when Access-Control-Allow-Origin doesn't match origin", () => { |
| 292 | + const headers = new Headers({ |
| 293 | + "Access-Control-Allow-Origin": "https://example.com" |
| 294 | + }); |
| 295 | + const response = new Response(null, { headers }); |
| 296 | + const origin = "https://other.com"; |
| 297 | + |
| 298 | + assert.throws(() => { |
| 299 | + assertCorsResponse(response, origin); |
| 300 | + }, { |
| 301 | + name: "CorsError", |
| 302 | + message: `Access to fetch at '${response.url}' from origin '${origin}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://example.com' that is not equal to the supplied origin.` |
| 303 | + }); |
| 304 | + }); |
| 305 | + |
| 306 | + it("should throw when Access-Control-Allow-Origin contains multiple values", () => { |
| 307 | + const headers = new Headers({ |
| 308 | + "Access-Control-Allow-Origin": "https://example.com, https://other.com" |
| 309 | + }); |
| 310 | + const response = new Response(null, { headers }); |
| 311 | + const origin = "https://example.com"; |
| 312 | + |
| 313 | + assert.throws(() => { |
| 314 | + assertCorsResponse(response, origin); |
| 315 | + }, { |
| 316 | + name: "CorsError", |
| 317 | + message: `Access to fetch at '${response.url}' from origin '${origin}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://example.com, https://other.com', but only one is allowed.` |
| 318 | + }); |
| 319 | + }); |
| 320 | + |
| 321 | + it("should throw CorsPreflightError when Access-Control-Allow-Origin header is missing in preflight", () => { |
| 322 | + const response = new Response(); |
| 323 | + const origin = "https://example.com"; |
| 324 | + |
| 325 | + assert.throws(() => { |
| 326 | + assertCorsResponse(response, origin, true); |
| 327 | + }, { |
| 328 | + name: "CorsPreflightError", |
| 329 | + message: `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.` |
| 330 | + }); |
| 331 | + }); |
| 332 | + |
| 333 | + it("should throw CorsPreflightError when Access-Control-Allow-Origin doesn't match origin in preflight", () => { |
| 334 | + const headers = new Headers({ |
| 335 | + "Access-Control-Allow-Origin": "https://example.com" |
| 336 | + }); |
| 337 | + const response = new Response(null, { headers }); |
| 338 | + const origin = "https://other.com"; |
| 339 | + |
| 340 | + assert.throws(() => { |
| 341 | + assertCorsResponse(response, origin, true); |
| 342 | + }, { |
| 343 | + name: "CorsPreflightError", |
| 344 | + message: `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: The 'Access-Control-Allow-Origin' header has a value 'https://example.com' that is not equal to the supplied origin.` |
| 345 | + }); |
| 346 | + }); |
| 347 | + }); |
254 | 348 | });
|
0 commit comments