test_worker_xhr_headers.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <!DOCTYPE HTML>
  6. <html>
  7. <head>
  8. <title>Test for XHR Headers</title>
  9. <script src="/tests/SimpleTest/SimpleTest.js">
  10. </script>
  11. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
  12. </head>
  13. <body>
  14. <p id="display"></p>
  15. <div id="content" style="display: none"></div>
  16. <pre id="test">
  17. <script class="testbody">
  18. "use strict";
  19. SimpleTest.waitForExplicitFinish();
  20. var path =
  21. location.pathname.substring(0, location.pathname.lastIndexOf("/") + 1);
  22. var filenamePrefix = "worker_xhr_headers_";
  23. var serverFilename = filenamePrefix + "server.sjs";
  24. var workerFilename = filenamePrefix + "worker.js";
  25. var otherHost = "example.com";
  26. info("Informing server about the current host");
  27. var xhr = new XMLHttpRequest();
  28. xhr.open("POST", path + serverFilename);
  29. xhr.setRequestHeader("options-host", otherHost);
  30. xhr.setRequestHeader("empty", "");
  31. xhr.onreadystatechange = function() {
  32. if (xhr.readyState == 4) {
  33. info("Launching worker");
  34. var worker = new Worker(path + workerFilename);
  35. worker.postMessage("http://" + otherHost + path + serverFilename);
  36. worker.onmessage = function(event) {
  37. ok(event.data.response === "", "Worker responded, saw no response");
  38. var loopCount = 0;
  39. function checkServer() {
  40. var xhr2 = new XMLHttpRequest();
  41. xhr2.open("GET", path + serverFilename);
  42. xhr2.onreadystatechange = function() {
  43. if (xhr2.readyState == 4) {
  44. if (xhr2.responseText) {
  45. is(xhr2.responseText,
  46. "Success: expected OPTIONS request with '" +
  47. event.data.header + "' header",
  48. "Server saw expected requests");
  49. SimpleTest.finish();
  50. } else if (++loopCount < 30) {
  51. setTimeout(checkServer, 1000);
  52. } else {
  53. ok(false, "Server never saw any requests");
  54. SimpleTest.finish();
  55. }
  56. }
  57. };
  58. info("Checking server status (" + loopCount + ")");
  59. xhr2.send();
  60. }
  61. checkServer();
  62. };
  63. worker.onerror = function(event) {
  64. ok(false, "Worker had an error: '" + event.message + "'");
  65. event.preventDefault();
  66. SimpleTest.finish();
  67. };
  68. }
  69. };
  70. xhr.send();
  71. </script>
  72. </pre>
  73. </body>
  74. </html>