michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: const PATH = "/file.meh"; michael@0: var httpserver = new HttpServer(); michael@0: michael@0: // Each time, the data consist in a string that should be sniffed as Ogg. michael@0: const data = "OggS\0meeeh."; michael@0: var testRan = 0; michael@0: michael@0: // If the content-type is not present, or if it's application/octet-stream, it michael@0: // should be sniffed to application/ogg by the media sniffer. Otherwise, it michael@0: // should not be changed. michael@0: const tests = [ michael@0: // Those three first case are the case of a media loaded in a media element. michael@0: // The first two should be sniffeed. michael@0: { contentType: "", michael@0: expectedContentType: "application/ogg", michael@0: flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN }, michael@0: { contentType: "application/octet-stream", michael@0: expectedContentType: "application/ogg", michael@0: flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN }, michael@0: { contentType: "application/something", michael@0: expectedContentType: "application/something", michael@0: flags: Ci.nsIChannel.LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN }, michael@0: // This last case tests the case of a channel opened while allowing content michael@0: // sniffers to override the content-type, like in the docshell. michael@0: { contentType: "application/octet-stream", michael@0: expectedContentType: "application/ogg", michael@0: flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS }, michael@0: { contentType: "", michael@0: expectedContentType: "application/ogg", michael@0: flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS }, michael@0: ]; michael@0: michael@0: // A basic listener that reads checks the if we sniffed properly. michael@0: var listener = { michael@0: onStartRequest: function(request, context) { michael@0: do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, michael@0: tests[testRan].expectedContentType); michael@0: }, michael@0: michael@0: onDataAvailable: function(request, context, stream, offset, count) { michael@0: try { michael@0: var bis = Components.classes["@mozilla.org/binaryinputstream;1"] michael@0: .createInstance(Components.interfaces.nsIBinaryInputStream); michael@0: bis.setInputStream(stream); michael@0: bis.readByteArray(bis.available()); michael@0: } catch (ex) { michael@0: do_throw("Error in onDataAvailable: " + ex); michael@0: } michael@0: }, michael@0: michael@0: onStopRequest: function(request, context, status) { michael@0: testRan++; michael@0: runNext(); michael@0: } michael@0: }; michael@0: michael@0: function setupChannel(url, flags) michael@0: { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel("http://localhost:" + michael@0: httpserver.identity.primaryPort + url, "", null); michael@0: chan.loadFlags |= flags; michael@0: var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: return httpChan; michael@0: } michael@0: michael@0: function runNext() { michael@0: if (testRan == tests.length) { michael@0: do_test_finished(); michael@0: return; michael@0: } michael@0: var channel = setupChannel(PATH, tests[testRan].flags); michael@0: httpserver.registerPathHandler(PATH, function(request, response) { michael@0: response.setHeader("Content-Type", tests[testRan].contentType, false); michael@0: response.bodyOutputStream.write(data, data.length); michael@0: }); michael@0: channel.asyncOpen(listener, channel, null); michael@0: } michael@0: michael@0: function run_test() { michael@0: httpserver.start(-1); michael@0: do_test_pending(); michael@0: try { michael@0: runNext(); michael@0: } catch (e) { michael@0: print("ERROR - " + e + "\n"); michael@0: } michael@0: }