toolkit/components/mediasniffer/test/unit/test_mediasniffer.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Ci = Components.interfaces;
     6 const Cu = Components.utils;
     8 Cu.import("resource://testing-common/httpd.js");
    10 const PATH = "/file.meh";
    11 var httpserver = new HttpServer();
    13 // Each time, the data consist in a string that should be sniffed as Ogg.
    14 const data = "OggS\0meeeh.";
    15 var testRan = 0;
    17 // If the content-type is not present, or if it's application/octet-stream, it
    18 // should be sniffed to application/ogg by the media sniffer. Otherwise, it
    19 // should not be changed.
    20 const tests = [
    21   // Those three first case are the case of a media loaded in a media element.
    22   // The first two should be sniffeed.
    23   { contentType: "",
    24     expectedContentType: "application/ogg",
    25     flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    26   { contentType: "application/octet-stream",
    27     expectedContentType: "application/ogg",
    28     flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    29   { contentType: "application/something",
    30     expectedContentType: "application/something",
    31     flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN },
    32   // This last case tests the case of a channel opened while allowing content
    33   // sniffers to override the content-type, like in the docshell.
    34   { contentType: "application/octet-stream",
    35     expectedContentType: "application/ogg",
    36     flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS },
    37   { contentType: "",
    38     expectedContentType: "application/ogg",
    39     flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS },
    40 ];
    42 // A basic listener that reads checks the if we sniffed properly.
    43 var listener = {
    44   onStartRequest: function(request, context) {
    45     do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType,
    46                 tests[testRan].expectedContentType);
    47   },
    49   onDataAvailable: function(request, context, stream, offset, count) {
    50     try {
    51       var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
    52                           .createInstance(Components.interfaces.nsIBinaryInputStream);
    53       bis.setInputStream(stream);
    54       bis.readByteArray(bis.available());
    55     } catch (ex) {
    56       do_throw("Error in onDataAvailable: " + ex);
    57     }
    58   },
    60   onStopRequest: function(request, context, status) {
    61     testRan++;
    62     runNext();
    63   }
    64 };
    66 function setupChannel(url, flags)
    67 {
    68   var ios = Components.classes["@mozilla.org/network/io-service;1"].
    69                        getService(Ci.nsIIOService);
    70   var chan = ios.newChannel("http://localhost:" +
    71                            httpserver.identity.primaryPort + url, "", null);
    72   chan.loadFlags |= flags;
    73   var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    74   return httpChan;
    75 }
    77 function runNext() {
    78   if (testRan == tests.length) {
    79     do_test_finished();
    80     return;
    81   }
    82   var channel = setupChannel(PATH, tests[testRan].flags);
    83   httpserver.registerPathHandler(PATH, function(request, response) {
    84     response.setHeader("Content-Type", tests[testRan].contentType, false);
    85     response.bodyOutputStream.write(data, data.length);
    86   });
    87   channel.asyncOpen(listener, channel, null);
    88 }
    90 function run_test() {
    91   httpserver.start(-1);
    92   do_test_pending();
    93   try {
    94     runNext();
    95   } catch (e) {
    96     print("ERROR - " + e + "\n");
    97   }
    98 }

mercurial