Bug 1443942 - Fix dom/media/test/midflight-redirect.sjs. r=jya
authorChris Pearce <cpearce@mozilla.com>
Wed, 04 Apr 2018 14:30:15 +1200
changeset 779716 79bd527dd8c86844f14a0c2306223f948f5cfcc0
parent 779715 e02b7f9e296d7f2373d4e77ab2f3567975f4aae3
child 779717 f2306039790583a2c1f75888010d044600d75f82
push id105836
push userbmo:ato@sny.no
push dateTue, 10 Apr 2018 12:07:22 +0000
reviewersjya
bugs1443942
milestone61.0a1
Bug 1443942 - Fix dom/media/test/midflight-redirect.sjs. r=jya Problems here: * The variable `to` is undefined for byte range requests to the end of the resource, making the math fail. Firefox normally makes ranges requests like this. * The bytes.length/4 calculation may not be a whole number, so can result in a byte range header part of the way between two bytes. We need to round the length off. * Instead of re-calculating the contentLength, we can just use the length of the actual byterange substring being returned. That's clearer. * test_midflight_redirect_blocked needs the redirect to happen before metadata has completed loading, but other tests require the redirect to happen *after* metadata is loaded. So add a redirectAt query parameter for the requester to control when to redirect. MozReview-Commit-ID: I6n1NqK0Uze
dom/media/test/midflight-redirect.sjs
dom/media/test/test_midflight_redirect_blocked.html
dom/media/test/test_mixed_principals.html
--- a/dom/media/test/midflight-redirect.sjs
+++ b/dom/media/test/midflight-redirect.sjs
@@ -43,26 +43,31 @@ function handleRequest(request, response
     response.setStatusLine(request.httpVersion, 303, "See Other");
     let url = "http://" + origin +
               "/tests/dom/media/test/midflight-redirect.sjs?redirected&" + query;
     response.setHeader("Location", url);
     response.setHeader("Content-Type", "text/html");
     return;
   }
 
-  if (from == 0 && !redirected) {
-    to = Math.min(bytes.length / 4, 200);
-  } else {
-    to = to || Math.max(from, bytes.length - 1);
+  if (isNaN(to)) {
+    to = bytes.length - 1;
   }
 
+  if (from == 0 && !redirected) {
+    to = parseInt(parseQuery(query, "redirectAt")) || Math.floor(bytes.length / 4);
+  }
+  to = Math.min(to, bytes.length - 1);
+
+  // Note: 'to' is the first index *excluded*, so we need (to + 1)
+  // in the substring end here.
   byterange = bytes.substring(from, to + 1);
 
-  let contentRange = "bytes "+ from +"-"+ to +"/"+ bytes.length;
-  let contentLength = (to - from + 1).toString();
+  let contentRange = "bytes " + from + "-" + to + "/" + bytes.length;
+  let contentLength = byterange.length.toString();
 
   response.setStatusLine(request.httpVersion, 206, "Partial Content");
   response.setHeader("Content-Range", contentRange);
   response.setHeader("Content-Length", contentLength, false);
   response.setHeader("Content-Type", type, false);
   response.setHeader("Accept-Ranges", "bytes", false);
   response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
   if (redirected && useCors) {
--- a/dom/media/test/test_midflight_redirect_blocked.html
+++ b/dom/media/test/test_midflight_redirect_blocked.html
@@ -37,20 +37,22 @@
           }, false);
 
           element.addEventListener("error", ()=>{
             resolve(false);
             removeNodeAndSource(element);
           }, false);
 
           var noise = Math.floor(Math.random() * 100000000);
+          // Note: request redirect before the end of metadata, otherwise we won't
+          // error before metadata has loaded if mixed origins are allowed.
           element.src = "midflight-redirect.sjs?resource=" + test.name
                       + (useCors ? "&cors" : "")
                       + "&type=" + test.type
-                      + "&noise=" + noise;
+                      + "&redirectAt=200";
           element.preload = "metadata";
           document.body.appendChild(element);
           element.load()
         });
       }
 
       let v = document.createElement("video");
       const testCases = gSmallTests.filter(t => v.canPlayType(t.type));
--- a/dom/media/test/test_mixed_principals.html
+++ b/dom/media/test/test_mixed_principals.html
@@ -38,23 +38,21 @@ https://bugzilla.mozilla.org/show_bug.cg
         return false;
       }
     }
 
     function runTest(origin, shouldReadBackOnLoad) {
       return new Promise(function (resolve, reject) {
         // Load will redirect mid-flight, which will be detected and should error,
         // and we should no longer be able to readback.
-        var noise = Math.floor(Math.random() * 100000000);
         var video = document.createElement("video");
         video.preload = "metadata";
         video.controls = true;
-        var url = "http://" + origin + "/tests/dom/media/test/midflight-redirect.sjs?key="
-                  + noise + "&resource=pixel_aspect_ratio.mp4"
-                  + "&type=video/mp4";
+        var url = "http://" + origin + "/tests/dom/media/test/midflight-redirect.sjs"
+                  + "?resource=pixel_aspect_ratio.mp4&type=video/mp4";
         SimpleTest.info("Loading from " + url);
         video.src = url;
         document.body.appendChild(video);
 
         once(video, "loadeddata", () => {
           is(canReadBack(video), shouldReadBackOnLoad, "Should be able to readback");
           video.play();
         });