blob: 3ca665a0209a2ca676be8072c61495f05d94df85 [file] [log] [blame] [edit]
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests for range requests on a Blob URL, using sync XHR.");
const payload1 = "Suffix range";
const payload2 = " ";
const payload3 = "test";
const blob = new Blob([payload1, payload2, payload3]);
const blobURL = URL.createObjectURL(blob);
const emptyBlob = new Blob();
const emptyBlobURL = URL.createObjectURL(emptyBlob);
function finish() {
URL.revokeObjectURL(blobURL);
URL.revokeObjectURL(emptyBlobURL);
}
function runXHRTest(url, rangeHeaderValue, expectedText, _expectedStatusCode, expectError = false)
{
expectedStatusCode = _expectedStatusCode;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false); // false = synchronous
xhr.setRequestHeader("Range", rangeHeaderValue);
try {
xhr.send();
if (expectError) {
testFailed("Load should have failed but did not");
return;
}
response = xhr;
shouldBe("response.status", "" + expectedStatusCode);
text = xhr.responseText;
shouldBeEqualToString("text", "" + expectedText);
} catch (error) {
if (expectError)
testPassed("XHR threw " + error);
else
testFailed("XHR threw " + error);
}
}
function runTests()
{
debug ("* Valid range: [0-3]");
runXHRTest(blobURL, "bytes=0-3", "Suff", 206);
debug ("* Valid range: [2-13]");
runXHRTest(blobURL, "bytes=2-13", "ffix range t", 206);
debug ("* Valid range: [-3]"); // Last 3 bytes
runXHRTest(blobURL, "bytes=-3", "est", 206);
debug ("* Valid range: [-7]"); // Last 7 bytes
runXHRTest(blobURL, "bytes=-7", "ge test", 206);
debug ("* Valid range: [3-]");
runXHRTest(blobURL, "bytes=3-", "fix range test", 206);
debug("* Invalid range (last x bytes, too large): [-1000000]");
runXHRTest(blobURL, "bytes=-1000000", "Suffix range test", 206); // We return all content but still 206 status code (so does Chrome).
debug("* Invalid range (end too large): [2-1000000]");
runXHRTest(blobURL, "bytes=2-1000000", "ffix range test", 206); // We clamp the end in this case (so does Chrome).
debug("* Invalid range (start too large): [1000-]");
let expectError = true;
runXHRTest(blobURL, "bytes=1000-", "", 0, expectError);
debug("* Empty Blob: [0-]");
runXHRTest(emptyBlobURL, "bytes=0-", "", 0, expectError);
debug("* Empty Blob: [-1]");
runXHRTest(emptyBlobURL, "bytes=-1", "", 206);
debug("* Empty Blob: [-100]");
runXHRTest(emptyBlobURL, "bytes=-100", "", 206);
debug("* Empty Blob: [100-]");
runXHRTest(emptyBlobURL, "bytes=0-", "", 0, expectError);
finish();
}
onload = () => {
runTests();
};
</script>
</body>
</html>