mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-02 12:07:52 +00:00
Bug 567077 - Tests for sniffing the types of media files that are served with no Content-Type. r=cpearce
This commit is contained in:
parent
eca50b7f5f
commit
8370333187
@ -214,6 +214,9 @@ MOCHITEST_FILES += \
|
|||||||
video-overhang.ogg \
|
video-overhang.ogg \
|
||||||
file_a4_tone.ogg \
|
file_a4_tone.ogg \
|
||||||
detodos.opus \
|
detodos.opus \
|
||||||
|
short.mp4 \
|
||||||
|
notags.mp3 \
|
||||||
|
id3tags.mp3 \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
# Wave sample files
|
# Wave sample files
|
||||||
|
77
content/media/test/contentType.sjs
Normal file
77
content/media/test/contentType.sjs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
// Parse the query string, and give us the value for a certain key, or false if
|
||||||
|
// it does not exist.
|
||||||
|
function parseQuery(request, key) {
|
||||||
|
var params = request.queryString.split('?')[0].split('&');
|
||||||
|
for (var j = 0; j < params.length; ++j) {
|
||||||
|
var p = params[j];
|
||||||
|
if (p == key)
|
||||||
|
return true;
|
||||||
|
if (p.indexOf(key + "=") == 0)
|
||||||
|
return p.substring(key.length + 1);
|
||||||
|
if (p.indexOf("=") < 0 && key == "")
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRequest(request, response) {
|
||||||
|
try {
|
||||||
|
// Get the filename to send back.
|
||||||
|
var filename = parseQuery(request, "file");
|
||||||
|
|
||||||
|
const CC = Components.Constructor;
|
||||||
|
const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
|
||||||
|
"nsIBinaryOutputStream",
|
||||||
|
"setOutputStream");
|
||||||
|
var file = Components.classes["@mozilla.org/file/directory_service;1"].
|
||||||
|
getService(Components.interfaces.nsIProperties).
|
||||||
|
get("CurWorkD", Components.interfaces.nsILocalFile);
|
||||||
|
var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
|
||||||
|
createInstance(Components.interfaces.nsIFileInputStream);
|
||||||
|
var bis = Components.classes["@mozilla.org/binaryinputstream;1"].
|
||||||
|
createInstance(Components.interfaces.nsIBinaryInputStream);
|
||||||
|
var paths = "tests/content/media/test/" + filename;
|
||||||
|
dump(paths + '\n');
|
||||||
|
var split = paths.split("/");
|
||||||
|
for(var i = 0; i < split.length; ++i) {
|
||||||
|
file.append(split[i]);
|
||||||
|
}
|
||||||
|
fis.init(file, -1, -1, false);
|
||||||
|
|
||||||
|
// handle range requests
|
||||||
|
var partialstart = 0,
|
||||||
|
partialend = file.fileSize - 1;
|
||||||
|
if (request.hasHeader("Range")) {
|
||||||
|
var range = request.getHeader("Range");
|
||||||
|
var parts = range.replace(/bytes=/, "").split("-");
|
||||||
|
var partialstart = parts[0];
|
||||||
|
var partialend = parts[1];
|
||||||
|
if (!partialend.length) {
|
||||||
|
partialend = file.fileSize - 1;
|
||||||
|
}
|
||||||
|
response.setStatusLine(request.httpVersion, 206, "Partial Content");
|
||||||
|
var contentRange = "bytes " + partialstart + "-" + partialend + "/" + file.fileSize;
|
||||||
|
response.setHeader("Content-Range", contentRange);
|
||||||
|
}
|
||||||
|
|
||||||
|
fis.seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, partialstart);
|
||||||
|
bis.setInputStream(fis);
|
||||||
|
|
||||||
|
var sendContentType = parseQuery(request, "nomime");
|
||||||
|
if (sendContentType == false) {
|
||||||
|
var contentType = parseQuery(request, "type");
|
||||||
|
if (contentType == false) {
|
||||||
|
// This should not happen.
|
||||||
|
dump("No type specified without having \'nomime\' in parameters.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
response.setHeader("Content-Type", contentType, false);
|
||||||
|
}
|
||||||
|
response.setHeader("Content-Length", ""+bis.available(), false);
|
||||||
|
|
||||||
|
var bytes = bis.readBytes(bis.available());
|
||||||
|
response.write(bytes, bytes.length);
|
||||||
|
} catch (e) {
|
||||||
|
dump ("ERROR : " + e + "\n");
|
||||||
|
}
|
||||||
|
}
|
BIN
content/media/test/id3tags.mp3
Normal file
BIN
content/media/test/id3tags.mp3
Normal file
Binary file not shown.
@ -156,6 +156,19 @@ var gPlayTests = [
|
|||||||
{ name:"bogus.duh", type:"bogus/duh", duration:Number.NaN }
|
{ name:"bogus.duh", type:"bogus/duh", duration:Number.NaN }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// A file for each type we can support.
|
||||||
|
var gSnifferTests = [
|
||||||
|
{ name:"big.wav", type:"audio/x-wav", duration:9.278981, size:102444 },
|
||||||
|
{ name:"320x240.ogv", type:"video/ogg", width:320, height:240, duration:0.233, size:28942 },
|
||||||
|
{ name:"seek.webm", type:"video/webm", duration:3.966, size:215529 },
|
||||||
|
{ name:"short.mp4", type:"video/mp4", duration:0.2, size:29435},
|
||||||
|
// A mp3 file without id3 tags.
|
||||||
|
{ name:"notags.mp3", type:"audio/mpeg", duration:0.28, size:2506},
|
||||||
|
// A mp3 file with id3 tags.
|
||||||
|
{ name:"id3tags.mp3", type:"audio/mpeg", duration:0.28, size:3530},
|
||||||
|
{ name:"bogus.duh", type:"bogus/duh" }
|
||||||
|
];
|
||||||
|
|
||||||
// Converts a path/filename to a file:// URI which we can load from disk.
|
// Converts a path/filename to a file:// URI which we can load from disk.
|
||||||
// Optionally checks whether the file actually exists on disk at the location
|
// Optionally checks whether the file actually exists on disk at the location
|
||||||
// we've specified.
|
// we've specified.
|
||||||
|
BIN
content/media/test/notags.mp3
Normal file
BIN
content/media/test/notags.mp3
Normal file
Binary file not shown.
BIN
content/media/test/short.mp4
Normal file
BIN
content/media/test/short.mp4
Normal file
Binary file not shown.
70
content/media/test/test_media_sniffer.html
Normal file
70
content/media/test/test_media_sniffer.html
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Media test: mozStopDownload</title>
|
||||||
|
<meta charset='utf-8'>
|
||||||
|
<script type="text/javascript" src="/MochiKit/Base.js"></script>
|
||||||
|
<script type="text/javascript" src="/MochiKit/DOM.js"></script>
|
||||||
|
<script type="text/javascript" src="/MochiKit/Style.js"></script>
|
||||||
|
<script type="text/javascript" src="/MochiKit/Signal.js"></script>
|
||||||
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||||
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
||||||
|
<script type="text/javascript" src="manifest.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre id="test">
|
||||||
|
<script class="testbody" type="text/javascript">
|
||||||
|
var manager = new MediaTestManager;
|
||||||
|
|
||||||
|
function finish_test(element) {
|
||||||
|
if (element.parentNode)
|
||||||
|
element.parentNode.removeChild(element);
|
||||||
|
manager.finished(element.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onApplicationOctetStreamLoaded(e) {
|
||||||
|
var t = e.target;
|
||||||
|
t.removeEventListener('loadedmetadata', onApplicationOctetStreamLoaded);
|
||||||
|
ok(true, "The media loads when served with application/octet-stream.");
|
||||||
|
finish_test(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkApplicationOctetStream(t) {
|
||||||
|
t.src = t.src.replace("&nomime", "&type=application/octet-stream");
|
||||||
|
t.addEventListener("loadedmetadata", onApplicationOctetStreamLoaded);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onmetadataloaded(e) {
|
||||||
|
var t = e.target;
|
||||||
|
t.removeEventListener('loadedmetadata', onmetadataloaded)
|
||||||
|
ok(true, "The media loads when served without a Content-Type.");
|
||||||
|
checkApplicationOctetStream(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onerror(e) {
|
||||||
|
var t = e.target;
|
||||||
|
t.removeEventListener('error', onerror);
|
||||||
|
ok(false, "The media could not be loaded." + t.src + "\n");
|
||||||
|
finish_test(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
function startTest(test, token) {
|
||||||
|
var elemType = /^audio/.test(test.type) ? "audio" : "video";
|
||||||
|
var element = document.createElement(elemType);
|
||||||
|
// This .sjs file serve the media file without Content-Type header, or with a
|
||||||
|
// specific Content-Type header.
|
||||||
|
element.src = 'contentType.sjs?file=' + test.name + "&nomime";
|
||||||
|
element.token = token;
|
||||||
|
element.controls = true;
|
||||||
|
element.preload = "metadata";
|
||||||
|
document.body.appendChild(element);
|
||||||
|
manager.started(token);
|
||||||
|
element.addEventListener("loadedmetadata", onmetadataloaded);
|
||||||
|
element.addEventListener("error", onerror);
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.runTests(gSnifferTests, startTest);
|
||||||
|
</script>
|
||||||
|
</pre>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user