From 7ca65cca32ca2a4057b77a146a9cd3e7bc3ecf0e Mon Sep 17 00:00:00 2001 From: Shawn Wilsher Date: Tue, 15 Dec 2009 23:33:06 -0800 Subject: [PATCH] Bug 532146 - NetUtil.jsm needs a helper for creating new channels Adds a new method for creating an nsIChannel from a string spec, an nsIURI, or an nsIFile. r=bz sr=vlad --- netwerk/base/src/NetUtil.jsm | 35 +++++++++++++++++ netwerk/test/unit/test_NetUtil.js | 62 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/netwerk/base/src/NetUtil.jsm b/netwerk/base/src/NetUtil.jsm index d620d2324cc9..7710a2cfd97e 100644 --- a/netwerk/base/src/NetUtil.jsm +++ b/netwerk/base/src/NetUtil.jsm @@ -204,6 +204,41 @@ const NetUtil = { return this.ioService.newURI(aTarget, aOriginCharset, aBaseURI); }, + /** + * Constructs a new channel for the given spec, character set, and base URI, + * or nsIURI, or nsIFile. + * + * @param aWhatToLoad + * The string spec for the desired URI, an nsIURI, or an nsIFile. + * @param aOriginCharset [optional] + * The character set for the URI. Only used if aWhatToLoad is a + * string. + * @param aBaseURI [optional] + * The base URI for the spec. Only used if aWhatToLoad is a string. + * + * @return an nsIChannel object. + */ + newChannel: function NetUtil_newChannel(aWhatToLoad, aOriginCharset, + aBaseURI) + { + if (!aWhatToLoad) { + let exception = new Components.Exception( + "Must have a non-null string spec, nsIURI, or nsIFile object", + Cr.NS_ERROR_INVALID_ARG, + Components.stack.caller + ); + throw exception; + } + + let uri = aWhatToLoad; + if (!(aWhatToLoad instanceof Ci.nsIURI)) { + // We either have a string or an nsIFile that we'll need a URI for. + uri = this.newURI(aWhatToLoad, aOriginCharset, aBaseURI); + } + + return this.ioService.newChannelFromURI(uri); + }, + /** * Returns a reference to nsIIOService. * diff --git a/netwerk/test/unit/test_NetUtil.js b/netwerk/test/unit/test_NetUtil.js index 670e112cb41e..c35a85623e9a 100644 --- a/netwerk/test/unit/test_NetUtil.js +++ b/netwerk/test/unit/test_NetUtil.js @@ -286,6 +286,64 @@ function test_asyncFetch_does_not_block() }); } +function test_newChannel_no_specifier() +{ + try { + NetUtil.newChannel(); + do_throw("should throw!"); + } + catch (e) { + do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); + } + + run_next_test(); +} + +function test_newChannel_with_string() +{ + const TEST_SPEC = "http://mozilla.org"; + + // Check that we get the same URI back from channel the IO service creates and + // the channel the utility method creates. + let ios = NetUtil.ioService; + let iosChannel = ios.newChannel(TEST_SPEC, null, null); + let NetUtilChannel = NetUtil.newChannel(TEST_SPEC); + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); + + run_next_test(); +} + +function test_newChannel_with_nsIURI() +{ + const TEST_SPEC = "http://mozilla.org"; + + // Check that we get the same URI back from channel the IO service creates and + // the channel the utility method creates. + let uri = NetUtil.newURI(TEST_SPEC); + let iosChannel = NetUtil.ioService.newChannelFromURI(uri); + let NetUtilChannel = NetUtil.newChannel(uri); + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); + + run_next_test(); +} + +function test_newChannel_with_nsIFile() +{ + let file = Cc["@mozilla.org/file/directory_service;1"]. + getService(Ci.nsIProperties). + get("TmpD", Ci.nsIFile); + file.append("NetUtil-test-file.tmp"); + + // Check that we get the same URI back from channel the IO service creates and + // the channel the utility method creates. + let uri = NetUtil.newURI(file); + let iosChannel = NetUtil.ioService.newChannelFromURI(uri); + let NetUtilChannel = NetUtil.newChannel(uri); + do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); + + run_next_test(); +} + //////////////////////////////////////////////////////////////////////////////// //// Test Runner @@ -300,6 +358,10 @@ let tests = [ test_asyncFetch_no_callback, test_asyncFetch, test_asyncFetch_does_not_block, + test_newChannel_no_specifier, + test_newChannel_with_string, + test_newChannel_with_nsIURI, + test_newChannel_with_nsIFile, ]; let index = 0;