test_CrossSiteXHR_cache.html (17496B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 5 <title>Test for Cross Site XMLHttpRequest</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body onload="gen.next()"> 10 <p id="display"> 11 <iframe id=loader></iframe> 12 </p> 13 <div id="content" style="display: none"> 14 15 </div> 16 <pre id="test"> 17 <script class="testbody" type="application/javascript"> 18 19 let gen; 20 SimpleTest.waitForExplicitFinish(); 21 SimpleTest.requestFlakyTimeout("This test needs to generate artificial pauses, hence it uses timeouts. There is no way around it, unfortunately. :("); 22 23 window.addEventListener("message", function(e) { 24 gen.next(e.data); 25 }); 26 27 gen = runTest(); 28 29 function* runTest() { 30 var loader = document.getElementById('loader'); 31 var loaderWindow = loader.contentWindow; 32 loader.onload = function () { gen.next() }; 33 34 loader.src = "http://example.org/tests/dom/security/test/cors/file_CrossSiteXHR_inner.html"; 35 origin = "http://example.org"; 36 yield undefined; 37 38 tests = [{ pass: 0, 39 method: "GET", 40 headers: { "x-my-header": "myValue" }, 41 }, 42 { pass: 1, 43 method: "GET", 44 headers: { "x-my-header": "myValue" }, 45 allowHeaders: "x-my-header", 46 cacheTime: 3600 47 }, 48 { pass: 1, 49 method: "GET", 50 headers: { "x-my-header": "myValue" }, 51 }, 52 { pass: 1, 53 method: "GET", 54 headers: { "x-my-header": "myValue" }, 55 }, 56 { pass: 0, 57 method: "GET", 58 headers: { "x-my-header": "myValue", 59 "y-my-header": "second" }, 60 }, 61 { pass: 1, 62 method: "GET", 63 headers: { "y-my-header": "hello" }, 64 allowHeaders: "y-my-header", 65 }, 66 { pass: 1, 67 method: "GET", 68 headers: { "y-my-header": "hello" }, 69 }, 70 { pass: 1, 71 method: "GET", 72 headers: { "y-my-header": "hello" }, 73 allowHeaders: "y-my-header,x-my-header", 74 cacheTime: 3600, 75 }, 76 { pass: 0, 77 method: "GET", 78 headers: { "x-my-header": "myValue", 79 "y-my-header": "second" }, 80 }, 81 { newTest: "*******" }, 82 { pass: 1, 83 method: "GET", 84 headers: { "y-my-header": "hello" }, 85 allowHeaders: "y-my-header,x-my-header", 86 }, 87 { pass: 1, 88 method: "GET", 89 headers: { "y-my-header": "hello" }, 90 }, 91 { pass: 1, 92 method: "GET", 93 headers: { "x-my-header": "myValue", 94 "y-my-header": "second" }, 95 }, 96 { newTest: "*******" }, 97 { pass: 0, 98 method: "GET", 99 headers: { "x-my-header": "myValue" }, 100 }, 101 { pass: 1, 102 method: "GET", 103 headers: { "x-my-header": "myValue" }, 104 allowHeaders: "x-my-header", 105 cacheTime: 2 106 }, 107 { pause: 2.1 }, 108 { pass: 0, 109 method: "GET", 110 headers: { "x-my-header": "myValue" }, 111 }, 112 { newTest: "*******" }, 113 { pass: 1, 114 method: "GET", 115 headers: { "x-my-header": "myValue" }, 116 allowHeaders: "x-my-header, y-my-header", 117 cacheTime: 3600 118 }, 119 { pass: 1, 120 method: "GET", 121 headers: { "x-my-header": "myValue" }, 122 }, 123 { pass: 1, 124 method: "GET", 125 headers: { "y-my-header": "myValue" }, 126 }, 127 { pass: 0, 128 method: "GET", 129 headers: { "z-my-header": "myValue" }, 130 }, 131 { newTest: "*******" }, 132 { pass: 1, 133 method: "GET", 134 headers: { "x-my-header": "myValue" }, 135 allowHeaders: "x-my-header", 136 cacheTime: "\t 3600 \t ", 137 }, 138 { pass: 1, 139 method: "GET", 140 headers: { "x-my-header": "myValue" }, 141 }, 142 { newTest: "*******" }, 143 { pass: 1, 144 method: "GET", 145 headers: { "x-my-header": "myValue" }, 146 allowHeaders: "x-my-header", 147 cacheTime: "3600 3", 148 }, 149 { pass: 0, 150 method: "GET", 151 headers: { "x-my-header": "myValue" }, 152 }, 153 { newTest: "*******" }, 154 { pass: 1, 155 method: "GET", 156 headers: { "x-my-header": "myValue" }, 157 allowHeaders: "x-my-header", 158 cacheTime: "asdf", 159 }, 160 { pass: 0, 161 method: "GET", 162 headers: { "x-my-header": "myValue" }, 163 }, 164 { newTest: "*******" }, 165 { pass: 1, 166 method: "GET", 167 headers: { "first-header": "myValue" }, 168 allowHeaders: "first-header", 169 cacheTime: 2, 170 }, 171 { pass: 1, 172 method: "GET", 173 headers: { "second-header": "myValue" }, 174 allowHeaders: "second-header", 175 cacheTime: 3600, 176 }, 177 { pass: 1, 178 method: "GET", 179 headers: { "third-header": "myValue" }, 180 allowHeaders: "third-header", 181 cacheTime: 2, 182 }, 183 { pause: 2.1 }, 184 { pass: 1, 185 method: "GET", 186 headers: { "second-header": "myValue" }, 187 }, 188 { pass: 0, 189 method: "GET", 190 headers: { "first-header": "myValue" }, 191 }, 192 { newTest: "*******" }, 193 { pass: 1, 194 method: "GET", 195 headers: { "first-header": "myValue" }, 196 allowHeaders: "first-header", 197 cacheTime: 2, 198 }, 199 { pass: 1, 200 method: "GET", 201 headers: { "second-header": "myValue" }, 202 allowHeaders: "second-header", 203 cacheTime: 3600, 204 }, 205 { pass: 1, 206 method: "GET", 207 headers: { "third-header": "myValue" }, 208 allowHeaders: "third-header", 209 cacheTime: 2, 210 }, 211 { pause: 2.1 }, 212 { pass: 1, 213 method: "GET", 214 headers: { "second-header": "myValue" }, 215 }, 216 { pass: 0, 217 method: "GET", 218 headers: { "third-header": "myValue" }, 219 }, 220 { newTest: "*******" }, 221 { pass: 0, 222 method: "DELETE", 223 }, 224 { pass: 1, 225 method: "DELETE", 226 allowMethods: "DELETE", 227 cacheTime: 3600 228 }, 229 { pass: 1, 230 method: "DELETE", 231 }, 232 { pass: 1, 233 method: "DELETE", 234 }, 235 { pass: 0, 236 method: "PATCH", 237 }, 238 { pass: 1, 239 method: "PATCH", 240 allowMethods: "PATCH", 241 }, 242 { pass: 1, 243 method: "PATCH", 244 }, 245 { pass: 1, 246 method: "PATCH", 247 allowMethods: "PATCH", 248 cacheTime: 3600, 249 }, 250 { pass: 1, 251 method: "PATCH", 252 }, 253 { pass: 0, 254 method: "DELETE", 255 }, 256 { pass: 0, 257 method: "PUT", 258 }, 259 { newTest: "*******" }, 260 { pass: 1, 261 method: "PATCH", 262 allowMethods: "PATCH", 263 cacheTime: 3600, 264 }, 265 { pass: 1, 266 method: "PATCH", 267 }, 268 { newTest: "*******" }, 269 { pass: 0, 270 method: "DELETE", 271 }, 272 { pass: 1, 273 method: "DELETE", 274 allowMethods: "DELETE", 275 cacheTime: 2 276 }, 277 { pause: 2.1 }, 278 { pass: 0, 279 method: "DELETE", 280 }, 281 { newTest: "*******" }, 282 { pass: 1, 283 method: "DELETE", 284 allowMethods: "DELETE, PUT", 285 cacheTime: 3600 286 }, 287 { pass: 1, 288 method: "DELETE", 289 }, 290 { pass: 1, 291 method: "PUT", 292 }, 293 { pass: 0, 294 method: "PATCH", 295 }, 296 { newTest: "*******" }, 297 { pass: 1, 298 method: "FIRST", 299 allowMethods: "FIRST", 300 cacheTime: 2, 301 }, 302 { pass: 1, 303 method: "SECOND", 304 allowMethods: "SECOND", 305 cacheTime: 3600, 306 }, 307 { pass: 1, 308 method: "THIRD", 309 allowMethods: "THIRD", 310 cacheTime: 2, 311 }, 312 { pause: 2.1 }, 313 { pass: 1, 314 method: "SECOND", 315 }, 316 { pass: 0, 317 method: "FIRST", 318 }, 319 { newTest: "*******" }, 320 { pass: 1, 321 method: "FIRST", 322 allowMethods: "FIRST", 323 cacheTime: 2, 324 }, 325 { pass: 1, 326 method: "SECOND", 327 allowMethods: "SECOND", 328 cacheTime: 3600, 329 }, 330 { pass: 1, 331 method: "THIRD", 332 allowMethods: "THIRD", 333 cacheTime: 2, 334 }, 335 { pause: 2.1 }, 336 { pass: 1, 337 method: "SECOND", 338 }, 339 { pass: 0, 340 method: "THIRD", 341 }, 342 { newTest: "*******" }, 343 { pass: 1, 344 method: "GET", 345 headers: { "x-my-header": "x-value" }, 346 allowHeaders: "x-my-header", 347 cacheTime: 3600, 348 }, 349 { pass: 1, 350 method: "GET", 351 headers: { "x-my-header": "x-value" } 352 }, 353 { pass: 0, 354 method: "GET", 355 headers: { "y-my-header": "y-value" } 356 }, 357 { pass: 0, 358 method: "GET", 359 headers: { "x-my-header": "x-value" } 360 }, 361 { newTest: "*******" }, 362 { pass: 1, 363 method: "GET", 364 headers: { "x-my-header": "x-value" }, 365 allowHeaders: "x-my-header", 366 cacheTime: 3600, 367 }, 368 { pass: 1, 369 method: "GET", 370 headers: { "x-my-header": "x-value" }, 371 }, 372 { pass: 0, 373 method: "PUT", 374 }, 375 { pass: 0, 376 method: "GET", 377 headers: { "x-my-header": "x-value" }, 378 }, 379 { newTest: "*******" }, 380 { pass: 1, 381 method: "GET", 382 headers: { "x-my-header": "x-value" }, 383 allowHeaders: "x-my-header", 384 cacheTime: 3600, 385 }, 386 { pass: 1, 387 method: "GET", 388 headers: { "x-my-header": "x-value" }, 389 }, 390 { pass: 0, 391 method: "GET", 392 noOrigin: 1, 393 }, 394 { pass: 0, 395 method: "GET", 396 headers: { "x-my-header": "x-value" }, 397 }, 398 { newTest: "*******" }, 399 { pass: 1, 400 method: "DELETE", 401 allowMethods: "DELETE", 402 cacheTime: 3600, 403 }, 404 { pass: 1, 405 method: "DELETE" 406 }, 407 { pass: 0, 408 method: "PUT" 409 }, 410 { pass: 0, 411 method: "DELETE" 412 }, 413 { newTest: "*******" }, 414 { pass: 1, 415 method: "DELETE", 416 allowMethods: "DELETE", 417 cacheTime: 3600, 418 }, 419 { pass: 1, 420 method: "DELETE" 421 }, 422 { pass: 0, 423 method: "DELETE", 424 headers: { "my-header": "value" }, 425 }, 426 { pass: 0, 427 method: "DELETE" 428 }, 429 { newTest: "*******" }, 430 { pass: 1, 431 method: "DELETE", 432 allowMethods: "DELETE", 433 cacheTime: 3600, 434 }, 435 { pass: 1, 436 method: "DELETE" 437 }, 438 { pass: 0, 439 method: "GET", 440 noOrigin: 1, 441 }, 442 { pass: 0, 443 method: "DELETE" 444 }, 445 { newTest: "*******" }, 446 { pass: 1, 447 method: "GET", 448 withCred: true, 449 headers: { "x-my-header": "myValue" }, 450 allowHeaders: "x-my-header", 451 cacheTime: 3600 452 }, 453 { pass: 1, 454 method: "GET", 455 withCred: true, 456 headers: { "x-my-header": "myValue" }, 457 }, 458 { pass: 0, 459 method: "GET", 460 headers: { "x-my-header": "myValue" }, 461 }, 462 { newTest: "*******" }, 463 { pass: 1, 464 method: "GET", 465 withCred: true, 466 headers: { "x-my-header": "myValue" }, 467 allowHeaders: "x-my-header", 468 cacheTime: 3600 469 }, 470 { pass: 1, 471 method: "GET", 472 headers: { "y-my-header": "myValue" }, 473 allowHeaders: "y-my-header", 474 cacheTime: 2 475 }, 476 { pass: 1, 477 method: "GET", 478 headers: { "y-my-header": "myValue" }, 479 }, 480 { pass: 1, 481 method: "GET", 482 withCred: true, 483 headers: { "x-my-header": "myValue" }, 484 }, 485 { pause: 2.1 }, 486 { pass: 1, 487 method: "GET", 488 withCred: true, 489 headers: { "x-my-header": "myValue" }, 490 }, 491 { pass: 0, 492 method: "GET", 493 headers: { "x-my-header": "myValue" }, 494 }, 495 { pass: 0, 496 method: "GET", 497 headers: { "y-my-header": "myValue" }, 498 }, 499 { pass: 0, 500 method: "GET", 501 withCred: true, 502 headers: { "y-my-header": "myValue" }, 503 }, 504 { newTest: "*******" }, 505 { pass: 1, 506 method: "DELETE", 507 allowMethods: "DELETE", 508 cacheTime: 3600 509 }, 510 { pass: 0, 511 method: "GET", 512 headers: { "DELETE": "myvalue" }, 513 }, 514 { newTest: "*******" }, 515 { pass: 1, 516 method: "GET", 517 headers: { "x-my-header": "myValue" }, 518 allowHeaders: "x-my-header", 519 cacheTime: 3600 520 }, 521 { pass: 0, 522 method: "3600", 523 headers: { "x-my-header": "myvalue" }, 524 }, 525 ]; 526 527 for (let i = 0; i < 110; i++) { 528 tests.push({ newTest: "*******" }, 529 { pass: 1, 530 method: "DELETE", 531 allowMethods: "DELETE", 532 cacheTime: 3600, 533 }); 534 } 535 536 baseURL = "http://example.com/tests/dom/security/test/cors/" + 537 "file_CrossSiteXHR_cache_server.sjs?"; 538 setStateURL = baseURL + "setState="; 539 540 var unique = Date.now(); 541 for (test of tests) { 542 if (test.newTest) { 543 unique++; 544 continue; 545 } 546 if (test.pause) { 547 setTimeout(function() { gen.next() }, test.pause * 1000); 548 yield undefined; 549 continue; 550 } 551 552 req = { 553 url: baseURL + "c=" + unique, 554 method: test.method, 555 headers: test.headers, 556 withCred: test.withCred, 557 }; 558 559 sec = { allowOrigin: test.noOrigin ? "" : origin, 560 allowHeaders: test.allowHeaders, 561 allowMethods: test.allowMethods, 562 cacheTime: test.cacheTime, 563 withCred: test.withCred }; 564 xhr = new XMLHttpRequest(); 565 xhr.open("POST", setStateURL + escape(JSON.stringify(sec)), true); 566 xhr.onloadend = function() { gen.next(); } 567 xhr.send(); 568 yield undefined; 569 570 loaderWindow.postMessage(JSON.stringify(req), origin); 571 572 res = JSON.parse(yield); 573 574 testName = JSON.stringify(test) + " (index " + tests.indexOf(test) + ")"; 575 576 if (test.pass) { 577 is(res.didFail, false, 578 "shouldn't have failed in test for " + testName); 579 is(res.status, 200, "wrong status in test for " + testName); 580 is(res.responseXML, "<res>hello pass</res>", 581 "wrong responseXML in test for " + testName); 582 is(res.responseText, "<res>hello pass</res>\n", 583 "wrong responseText in test for " + testName); 584 is(res.events.join(","), 585 "opening,rs1,sending,loadstart,rs2,rs3,rs4,load,loadend", 586 "wrong events in test for " + testName); 587 } 588 else { 589 is(res.didFail, true, 590 "should have failed in test for " + testName); 591 is(res.status, 0, "wrong status in test for " + testName); 592 is(res.responseXML, null, 593 "wrong responseXML in test for " + testName); 594 is(res.responseText, "", 595 "wrong responseText in test for " + testName); 596 is(res.events.join(","), 597 "opening,rs1,sending,loadstart,rs4,error,loadend", 598 "wrong events in test for " + testName); 599 is(res.progressEvents, 0, 600 "wrong events in test for " + testName); 601 } 602 } 603 604 SimpleTest.finish(); 605 } 606 607 </script> 608 </pre> 609 </body> 610 </html>