content/base/test/test_bug435425.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=435425
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 435425</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=435425">Mozilla Bug 435425</a>
michael@0 13 <p id="display"></p>
michael@0 14 <div id="content" style="display: none">
michael@0 15
michael@0 16 </div>
michael@0 17 <pre id="test">
michael@0 18 <script class="testbody" type="text/javascript">
michael@0 19
michael@0 20 /** Test for Bug 435425 **/
michael@0 21
michael@0 22 var xhr = null;
michael@0 23 var upload = null;
michael@0 24 var currentEvents = null;
michael@0 25 var expectedResponseText = null;
michael@0 26 var uploadTotal = 0;
michael@0 27
michael@0 28 function logEvent(evt) {
michael@0 29 var i = 0;
michael@0 30 while ((currentEvents.length != i) &&
michael@0 31 currentEvents[i].optional &&
michael@0 32 ((currentEvents[i].type != evt.type) ||
michael@0 33 !(evt.target instanceof currentEvents[i].target))) {
michael@0 34 ++i;
michael@0 35 }
michael@0 36 if (evt.target instanceof XMLHttpRequestUpload) {
michael@0 37 if (evt.type == "loadstart") {
michael@0 38 uploadTotal = evt.total
michael@0 39 } else {
michael@0 40 if (evt.type == "progress") {
michael@0 41 ok(evt.lengthComputable, "event(" + evt.type + ").lengthComputable should be true.");
michael@0 42 }
michael@0 43 is(evt.total, uploadTotal, "event(" + evt.type + ").total should not change during upload.");
michael@0 44 }
michael@0 45 }
michael@0 46 ok(i != currentEvents.length, "Extra or wrong event?");
michael@0 47 is(evt.type, currentEvents[i].type, "Wrong event!")
michael@0 48 ok(evt.target instanceof currentEvents[i].target,
michael@0 49 "Wrong event target [" + evt.target + "," + evt.type + "]!");
michael@0 50 // If we handled non-optional event, remove all optional events before the
michael@0 51 // handled event and then the non-optional event from the list.
michael@0 52 if (!currentEvents[i].optional) {
michael@0 53 for (;i != -1; --i) {
michael@0 54 currentEvents.shift();
michael@0 55 }
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 function hasPendingNonOptionalEvent(ev) {
michael@0 60 var i = 0;
michael@0 61 while (i < currentEvents.length) {
michael@0 62 if (!currentEvents[i].optional && currentEvents[i].type == ev)
michael@0 63 return true;
michael@0 64 ++i;
michael@0 65 }
michael@0 66 return false;
michael@0 67 }
michael@0 68
michael@0 69 function maybeStop(evt) {
michael@0 70 logEvent(evt);
michael@0 71 if (!hasPendingNonOptionalEvent("loadend"))
michael@0 72 nextTest();
michael@0 73 }
michael@0 74
michael@0 75 function openXHR(xhr, method, url, privileged) {
michael@0 76 if (privileged)
michael@0 77 SpecialPowers.wrap(xhr).open(method, url);
michael@0 78 else
michael@0 79 xhr.open(method, url);
michael@0 80 }
michael@0 81
michael@0 82 function start(obj) {
michael@0 83 xhr = new XMLHttpRequest();
michael@0 84 upload = xhr.upload;
michael@0 85 currentEvents = obj.expectedEvents;
michael@0 86 expectedResponseText = obj.withUpload;
michael@0 87 xhr.onload =
michael@0 88 function(evt) {
michael@0 89 if (expectedResponseText) {
michael@0 90 is(evt.target.responseText, expectedResponseText, "Wrong responseText");
michael@0 91 }
michael@0 92 logEvent(evt);
michael@0 93 }
michael@0 94 xhr.onerror =
michael@0 95 function(evt) {
michael@0 96 logEvent(evt);
michael@0 97 }
michael@0 98 xhr.onabort =
michael@0 99 function(evt) {
michael@0 100 logEvent(evt);
michael@0 101 }
michael@0 102 xhr.onloadend =
michael@0 103 function (evt) {
michael@0 104 maybeStop(evt);
michael@0 105 }
michael@0 106 xhr.onloadstart =
michael@0 107 function (evt) {
michael@0 108 logEvent(evt);
michael@0 109 }
michael@0 110 xhr.onprogress =
michael@0 111 function (evt) {
michael@0 112 logEvent(evt);
michael@0 113 }
michael@0 114
michael@0 115 if ("upload" in xhr) {
michael@0 116 xhr.upload.onloadstart =
michael@0 117 function (evt) {
michael@0 118 logEvent(evt);
michael@0 119 }
michael@0 120 xhr.upload.onprogress =
michael@0 121 function (evt) {
michael@0 122 logEvent(evt);
michael@0 123 }
michael@0 124 xhr.upload.onload =
michael@0 125 function (evt) {
michael@0 126 logEvent(evt);
michael@0 127 }
michael@0 128 xhr.upload.onerror =
michael@0 129 function (evt) {
michael@0 130 logEvent(evt);
michael@0 131 }
michael@0 132 xhr.upload.onabort =
michael@0 133 function (evt) {
michael@0 134 logEvent(evt);
michael@0 135 }
michael@0 136 xhr.upload.onloadend =
michael@0 137 function (evt) {
michael@0 138 maybeStop(evt);
michael@0 139 }
michael@0 140 }
michael@0 141
michael@0 142 try {
michael@0 143 var methodIsGet = (obj.method == "GET");
michael@0 144 var url;
michael@0 145 var privileged = false;
michael@0 146 if (obj.testRedirectError) {
michael@0 147 url = "bug435425_redirect.sjs";
michael@0 148 } else if (obj.testNetworkError) {
michael@0 149 url = "http://nosuchdomain.localhost";
michael@0 150 privileged = true;
michael@0 151 } else {
michael@0 152 url = "bug435425.sjs";
michael@0 153 if (obj.withUpload && methodIsGet) {
michael@0 154 url += "?" + obj.withUpload;
michael@0 155 }
michael@0 156 }
michael@0 157 openXHR(xhr, obj.method, url, privileged);
michael@0 158 xhr.send(!methodIsGet ? obj.withUpload : null);
michael@0 159 if (obj.testAbort) {
michael@0 160 xhr.abort();
michael@0 161 }
michael@0 162 } catch (ex) {
michael@0 163 ok(false, ex);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 var none = null;
michael@0 168 var small = "";
michael@0 169 var mid = "";
michael@0 170 var large = "";
michael@0 171
michael@0 172
michael@0 173 for (var smallLength = 0; smallLength < (0x00000000 + 2); ++smallLength) {
michael@0 174 small += "A";
michael@0 175 }
michael@0 176
michael@0 177 for (var midLength = 0; midLength < (0x00000FFF + 2); ++midLength) {
michael@0 178 mid += "A";
michael@0 179 }
michael@0 180
michael@0 181 for (var largeLength = 0; largeLength < (0x0000FFFF + 2); ++largeLength) {
michael@0 182 large += "A";
michael@0 183 }
michael@0 184
michael@0 185 const XHR = XMLHttpRequest;
michael@0 186 const UPLOAD = XMLHttpRequestUpload;
michael@0 187
michael@0 188 var tests =
michael@0 189 [
michael@0 190 { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 191 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 192 {target: XHR, type: "progress", optional: true},
michael@0 193 {target: XHR, type: "load", optional: false},
michael@0 194 {target: XHR, type: "loadend", optional: false}]},
michael@0 195 { method: "GET", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 196 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 197 {target: XHR, type: "abort", optional: false},
michael@0 198 {target: XHR, type: "loadend", optional: false}]},
michael@0 199 { method: "GET", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 200 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 201 {target: XHR, type: "error", optional: false},
michael@0 202 {target: XHR, type: "loadend", optional: false}]},
michael@0 203 { method: "GET", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 204 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 205 {target: XHR, type: "error", optional: false},
michael@0 206 {target: XHR, type: "loadend", optional: false}]},
michael@0 207
michael@0 208 { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 209 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 210 {target: XHR, type: "progress", optional: true},
michael@0 211 {target: XHR, type: "load", optional: false},
michael@0 212 {target: XHR, type: "loadend", optional: false}]},
michael@0 213 { method: "GET", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 214 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 215 {target: XHR, type: "abort", optional: false},
michael@0 216 {target: XHR, type: "loadend", optional: false}]},
michael@0 217 { method: "GET", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 218 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 219 {target: XHR, type: "error", optional: false},
michael@0 220 {target: XHR, type: "loadend", optional: false}]},
michael@0 221 { method: "GET", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 222 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 223 {target: XHR, type: "error", optional: false},
michael@0 224 {target: XHR, type: "loadend", optional: false}]},
michael@0 225
michael@0 226 { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 227 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 228 {target: XHR, type: "progress", optional: true},
michael@0 229 {target: XHR, type: "load", optional: false},
michael@0 230 {target: XHR, type: "loadend", optional: false}]},
michael@0 231 { method: "GET", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 232 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 233 {target: XHR, type: "abort", optional: false},
michael@0 234 {target: XHR, type: "loadend", optional: false}]},
michael@0 235 { method: "GET", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 236 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 237 {target: XHR, type: "error", optional: false},
michael@0 238 {target: XHR, type: "loadend", optional: false}]},
michael@0 239 { method: "GET", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 240 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 241 {target: XHR, type: "error", optional: false},
michael@0 242 {target: XHR, type: "loadend", optional: false}]},
michael@0 243
michael@0 244 { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 245 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 246 {target: XHR, type: "progress", optional: true},
michael@0 247 {target: XHR, type: "load", optional: false},
michael@0 248 {target: XHR, type: "loadend", optional: false}]},
michael@0 249 { method: "GET", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 250 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 251 {target: XHR, type: "abort", optional: false},
michael@0 252 {target: XHR, type: "loadend", optional: false}]},
michael@0 253 { method: "GET", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 254 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 255 {target: XHR, type: "error", optional: false},
michael@0 256 {target: XHR, type: "loadend", optional: false}]},
michael@0 257 { method: "GET", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 258 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 259 {target: XHR, type: "error", optional: false},
michael@0 260 {target: XHR, type: "loadend", optional: false}]},
michael@0 261
michael@0 262 { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 263 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 264 {target: XHR, type: "progress", optional: true},
michael@0 265 {target: XHR, type: "load", optional: false},
michael@0 266 {target: XHR, type: "loadend", optional: false}]},
michael@0 267 { method: "POST", withUpload: none, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 268 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 269 {target: XHR, type: "abort", optional: false},
michael@0 270 {target: XHR, type: "loadend", optional: false}]},
michael@0 271 { method: "POST", withUpload: none, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 272 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 273 {target: XHR, type: "error", optional: false},
michael@0 274 {target: XHR, type: "loadend", optional: false}]},
michael@0 275 { method: "POST", withUpload: none, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 276 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 277 {target: XHR, type: "error", optional: false},
michael@0 278 {target: XHR, type: "loadend", optional: false}]},
michael@0 279
michael@0 280 { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 281 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 282 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 283 {target: UPLOAD, type: "progress", optional: true},
michael@0 284 {target: UPLOAD, type: "load", optional: false},
michael@0 285 {target: UPLOAD, type: "loadend", optional: false},
michael@0 286 {target: XHR, type: "progress", optional: true},
michael@0 287 {target: XHR, type: "load", optional: false},
michael@0 288 {target: XHR, type: "loadend", optional: false}]},
michael@0 289 { method: "POST", withUpload: small, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 290 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 291 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 292 {target: XHR, type: "abort", optional: false},
michael@0 293 {target: XHR, type: "loadend", optional: false},
michael@0 294 {target: UPLOAD, type: "abort", optional: false},
michael@0 295 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 296 { method: "POST", withUpload: small, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 297 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 298 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 299 {target: UPLOAD, type: "progress", optional: true},
michael@0 300 {target: XHR, type: "error", optional: false},
michael@0 301 {target: XHR, type: "loadend", optional: false},
michael@0 302 {target: UPLOAD, type: "error", optional: false},
michael@0 303 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 304 { method: "POST", withUpload: small, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 305 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 306 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 307 {target: UPLOAD, type: "progress", optional: true},
michael@0 308 {target: XHR, type: "error", optional: false},
michael@0 309 {target: XHR, type: "loadend", optional: false},
michael@0 310 {target: UPLOAD, type: "error", optional: false},
michael@0 311 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 312
michael@0 313 { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 314 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 315 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 316 {target: UPLOAD, type: "progress", optional: true},
michael@0 317 {target: UPLOAD, type: "load", optional: false},
michael@0 318 {target: UPLOAD, type: "loadend", optional: false},
michael@0 319 {target: XHR, type: "progress", optional: true},
michael@0 320 {target: XHR, type: "load", optional: false},
michael@0 321 {target: XHR, type: "loadend", optional: false}]},
michael@0 322 { method: "POST", withUpload: mid, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 323 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 324 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 325 {target: XHR, type: "abort", optional: false},
michael@0 326 {target: XHR, type: "loadend", optional: false},
michael@0 327 {target: UPLOAD, type: "abort", optional: false},
michael@0 328 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 329 { method: "POST", withUpload: mid, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 330 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 331 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 332 {target: UPLOAD, type: "progress", optional: true},
michael@0 333 {target: XHR, type: "error", optional: false},
michael@0 334 {target: XHR, type: "loadend", optional: false},
michael@0 335 {target: UPLOAD, type: "error", optional: false},
michael@0 336 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 337 { method: "POST", withUpload: mid, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 338 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 339 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 340 {target: UPLOAD, type: "progress", optional: true},
michael@0 341 {target: XHR, type: "error", optional: false},
michael@0 342 {target: XHR, type: "loadend", optional: false},
michael@0 343 {target: UPLOAD, type: "error", optional: false},
michael@0 344 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 345
michael@0 346 { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: false,
michael@0 347 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 348 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 349 {target: UPLOAD, type: "progress", optional: true},
michael@0 350 {target: UPLOAD, type: "load", optional: false},
michael@0 351 {target: UPLOAD, type: "loadend", optional: false},
michael@0 352 {target: XHR, type: "progress", optional: true},
michael@0 353 {target: XHR, type: "load", optional: false},
michael@0 354 {target: XHR, type: "loadend", optional: false}]},
michael@0 355 { method: "POST", withUpload: large, testAbort: true, testRedirectError: false, testNetworkError: false,
michael@0 356 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 357 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 358 {target: XHR, type: "abort", optional: false},
michael@0 359 {target: XHR, type: "loadend", optional: false},
michael@0 360 {target: UPLOAD, type: "abort", optional: false},
michael@0 361 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 362 { method: "POST", withUpload: large, testAbort: false, testRedirectError: true, testNetworkError: false,
michael@0 363 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 364 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 365 {target: UPLOAD, type: "progress", optional: true},
michael@0 366 {target: XHR, type: "error", optional: false},
michael@0 367 {target: XHR, type: "loadend", optional: false},
michael@0 368 {target: UPLOAD, type: "error", optional: false},
michael@0 369 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 370 { method: "POST", withUpload: large, testAbort: false, testRedirectError: false, testNetworkError: true,
michael@0 371 expectedEvents: [{target: XHR, type: "loadstart", optional: false},
michael@0 372 {target: UPLOAD, type: "loadstart", optional: false},
michael@0 373 {target: UPLOAD, type: "progress", optional: true},
michael@0 374 {target: XHR, type: "error", optional: false},
michael@0 375 {target: XHR, type: "loadend", optional: false},
michael@0 376 {target: UPLOAD, type: "error", optional: false},
michael@0 377 {target: UPLOAD, type: "loadend", optional: false}]},
michael@0 378 ];
michael@0 379
michael@0 380 function runTest() {
michael@0 381 var test = tests.shift();
michael@0 382 start(test);
michael@0 383 }
michael@0 384
michael@0 385 function nextTest() {
michael@0 386 if (tests.length > 1) {
michael@0 387 setTimeout("runTest()", 0);
michael@0 388 } else {
michael@0 389 SimpleTest.finish();
michael@0 390 }
michael@0 391 }
michael@0 392
michael@0 393 ok("upload" in (new XMLHttpRequest()), "XMLHttpRequest.upload isn't supported!");
michael@0 394 SimpleTest.waitForExplicitFinish();
michael@0 395 addLoadEvent(runTest);
michael@0 396
michael@0 397 </script>
michael@0 398 </pre>
michael@0 399 </body>
michael@0 400 </html>
michael@0 401

mercurial