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