xhrAbort_worker.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. function runTest() {
  6. var xhr = new XMLHttpRequest();
  7. var events = [];
  8. function pushEvent(event) {
  9. var readyState, responseText, status, statusText;
  10. try {
  11. readyState = xhr.readyState;
  12. }
  13. catch (e) {
  14. readyState = "[exception]";
  15. }
  16. try {
  17. responseText = xhr.responseText;
  18. }
  19. catch (e) {
  20. responseText = "[exception]";
  21. }
  22. try {
  23. status = xhr.status;
  24. }
  25. catch (e) {
  26. status = "[exception]";
  27. }
  28. try {
  29. statusText = xhr.statusText;
  30. }
  31. catch (e) {
  32. statusText = "[exception]";
  33. }
  34. var str = event.type + "(" + readyState + ", '" + responseText + "', " +
  35. status + ", '" + statusText + "'";
  36. if ((("ProgressEvent" in this) && event instanceof ProgressEvent) ||
  37. (("WorkerProgressEvent" in this) && event instanceof WorkerProgressEvent)) {
  38. str += ", progressEvent";
  39. }
  40. str += ")";
  41. events.push(str);
  42. }
  43. xhr.onerror = function(event) {
  44. throw new Error("Error: " + xhr.statusText);
  45. }
  46. xhr.onload = function(event) {
  47. throw new Error("Shouldn't have gotten load event!");
  48. };
  49. var seenAbort;
  50. xhr.onabort = function(event) {
  51. if (seenAbort) {
  52. throw new Error("Already seen the abort event!");
  53. }
  54. seenAbort = true;
  55. pushEvent(event);
  56. postMessage(events);
  57. };
  58. xhr.onreadystatechange = function(event) {
  59. pushEvent(event);
  60. if (xhr.readyState == xhr.HEADERS_RECEIVED) {
  61. xhr.abort();
  62. }
  63. };
  64. xhr.open("GET", "worker_testXHR.txt");
  65. xhr.overrideMimeType("text/plain");
  66. xhr.send(null);
  67. }
  68. function messageListener(event) {
  69. switch (event.data) {
  70. case "start":
  71. runTest();
  72. break;
  73. default:
  74. throw new Error("Bad message!");
  75. }
  76. }
  77. addEventListener("message", messageListener, false);