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