michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 et 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: /** michael@0: * This file tests the methods on NetUtil.jsm. michael@0: */ michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: Cu.import("resource://gre/modules/NetUtil.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: // We need the profile directory so the test harness will clean up our test michael@0: // files. michael@0: do_get_profile(); michael@0: michael@0: const OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/file-output-stream;1"; michael@0: const SAFE_OUTPUT_STREAM_CONTRACT_ID = "@mozilla.org/network/safe-file-output-stream;1"; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Helper Methods michael@0: michael@0: /** michael@0: * Reads the contents of a file and returns it as a string. michael@0: * michael@0: * @param aFile michael@0: * The file to return from. michael@0: * @return the contents of the file in the form of a string. michael@0: */ michael@0: function getFileContents(aFile) michael@0: { michael@0: "use strict"; michael@0: michael@0: let fstream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: fstream.init(aFile, -1, 0, 0); michael@0: michael@0: let cstream = Cc["@mozilla.org/intl/converter-input-stream;1"]. michael@0: createInstance(Ci.nsIConverterInputStream); michael@0: cstream.init(fstream, "UTF-8", 0, 0); michael@0: michael@0: let string = {}; michael@0: cstream.readString(-1, string); michael@0: cstream.close(); michael@0: return string.value; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Tests asynchronously writing a file using NetUtil.asyncCopy. michael@0: * michael@0: * @param aContractId michael@0: * The contract ID to use for the output stream michael@0: * @param aDeferOpen michael@0: * Whether to use DEFER_OPEN in the output stream. michael@0: */ michael@0: function async_write_file(aContractId, aDeferOpen) michael@0: { michael@0: do_test_pending(); michael@0: michael@0: // First, we need an output file to write to. michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-async-test-file.tmp"); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: // Then, we need an output stream to our output file. michael@0: let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0); michael@0: michael@0: // Finally, we need an input stream to take data from. michael@0: const TEST_DATA = "this is a test string"; michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: istream.setData(TEST_DATA, TEST_DATA.length); michael@0: michael@0: NetUtil.asyncCopy(istream, ostream, function(aResult) { michael@0: // Make sure the copy was successful! michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check the file contents. michael@0: do_check_eq(TEST_DATA, getFileContents(file)); michael@0: michael@0: // Finish the test. michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Tests michael@0: michael@0: // Test NetUtil.asyncCopy for all possible buffering scenarios michael@0: function test_async_copy() michael@0: { michael@0: // Create a data sample michael@0: function make_sample(text) { michael@0: let data = []; michael@0: for (let i = 0; i <= 100; ++i) { michael@0: data.push(text); michael@0: } michael@0: return data.join(); michael@0: } michael@0: michael@0: // Create an input buffer holding some data michael@0: function make_input(isBuffered, data) { michael@0: if (isBuffered) { michael@0: // String input streams are buffered michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: istream.setData(data, data.length); michael@0: return istream; michael@0: } michael@0: michael@0: // File input streams are not buffered, so let's create a file michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-asyncFetch-test-file.tmp"); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, -1, -1, 0); michael@0: ostream.write(data, data.length); michael@0: ostream.close(); michael@0: michael@0: let istream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: istream.init(file, -1, 0, 0); michael@0: michael@0: return istream; michael@0: } michael@0: michael@0: // Create an output buffer holding some data michael@0: function make_output(isBuffered) { michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-asyncFetch-test-file.tmp"); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, -1, -1, 0); michael@0: michael@0: if (!isBuffered) { michael@0: return {file: file, sink: ostream}; michael@0: } michael@0: michael@0: let bstream = Cc["@mozilla.org/network/buffered-output-stream;1"]. michael@0: createInstance(Ci.nsIBufferedOutputStream); michael@0: bstream.init(ostream, 256); michael@0: return {file: file, sink: bstream}; michael@0: } michael@0: Task.spawn(function*() { michael@0: do_test_pending(); michael@0: for (let bufferedInput of [true, false]) { michael@0: for (let bufferedOutput of [true, false]) { michael@0: let text = "test_async_copy with " michael@0: + (bufferedInput?"buffered input":"unbuffered input") michael@0: + ", " michael@0: + (bufferedOutput?"buffered output":"unbuffered output"); michael@0: do_print(text); michael@0: let TEST_DATA = "[" + make_sample(text) + "]"; michael@0: let source = make_input(bufferedInput, TEST_DATA); michael@0: let {file, sink} = make_output(bufferedOutput); michael@0: let deferred = Promise.defer(); michael@0: NetUtil.asyncCopy(source, sink, deferred.resolve); michael@0: let result = yield deferred.promise; michael@0: michael@0: // Make sure the copy was successful! michael@0: if (!Components.isSuccessCode(result)) { michael@0: do_throw(new Components.Exception("asyncCopy error", result)); michael@0: } michael@0: michael@0: // Check the file contents. michael@0: do_check_eq(TEST_DATA, getFileContents(file)); michael@0: } michael@0: } michael@0: michael@0: do_test_finished(); michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function test_async_write_file() { michael@0: async_write_file(OUTPUT_STREAM_CONTRACT_ID); michael@0: } michael@0: michael@0: function test_async_write_file_deferred() { michael@0: async_write_file(OUTPUT_STREAM_CONTRACT_ID, true); michael@0: } michael@0: michael@0: function test_async_write_file_safe() { michael@0: async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID); michael@0: } michael@0: michael@0: function test_async_write_file_safe_deferred() { michael@0: async_write_file(SAFE_OUTPUT_STREAM_CONTRACT_ID, true); michael@0: } michael@0: michael@0: function test_newURI_no_spec_throws() michael@0: { michael@0: try { michael@0: NetUtil.newURI(); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_newURI() michael@0: { michael@0: let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: michael@0: // Check that we get the same URI back from the IO service and the utility michael@0: // method. michael@0: const TEST_URI = "http://mozilla.org"; michael@0: let iosURI = ios.newURI(TEST_URI, null, null); michael@0: let NetUtilURI = NetUtil.newURI(TEST_URI); michael@0: do_check_true(iosURI.equals(NetUtilURI)); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_newURI_takes_nsIFile() michael@0: { michael@0: let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: michael@0: // Create a test file that we can pass into NetUtil.newURI michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-test-file.tmp"); michael@0: michael@0: // Check that we get the same URI back from the IO service and the utility michael@0: // method. michael@0: let iosURI = ios.newFileURI(file); michael@0: let NetUtilURI = NetUtil.newURI(file); michael@0: do_check_true(iosURI.equals(NetUtilURI)); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_ioService() michael@0: { michael@0: do_check_true(NetUtil.ioService instanceof Ci.nsIIOService); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_asyncFetch_no_channel() michael@0: { michael@0: try { michael@0: NetUtil.asyncFetch(null, function() { }); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_asyncFetch_no_callback() michael@0: { michael@0: try { michael@0: NetUtil.asyncFetch({ }); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_asyncFetch_with_nsIChannel() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: michael@0: // Start the http server, and register our handler. michael@0: let server = new HttpServer(); michael@0: server.registerPathHandler("/test", function(aRequest, aResponse) { michael@0: aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); michael@0: aResponse.setHeader("Content-Type", "text/plain", false); michael@0: aResponse.write(TEST_DATA); michael@0: }); michael@0: server.start(-1); michael@0: michael@0: // Create our channel. michael@0: let channel = NetUtil.ioService. michael@0: newChannel("http://localhost:" + michael@0: server.identity.primaryPort + "/test", null, null); michael@0: michael@0: // Open our channel asynchronously. michael@0: NetUtil.asyncFetch(channel, function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that we got the right data. michael@0: do_check_eq(aInputStream.available(), TEST_DATA.length); michael@0: let is = Cc["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(Ci.nsIScriptableInputStream); michael@0: is.init(aInputStream); michael@0: let result = is.read(TEST_DATA.length); michael@0: do_check_eq(TEST_DATA, result); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: } michael@0: michael@0: function test_asyncFetch_with_nsIURI() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: michael@0: // Start the http server, and register our handler. michael@0: let server = new HttpServer(); michael@0: server.registerPathHandler("/test", function(aRequest, aResponse) { michael@0: aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); michael@0: aResponse.setHeader("Content-Type", "text/plain", false); michael@0: aResponse.write(TEST_DATA); michael@0: }); michael@0: server.start(-1); michael@0: michael@0: // Create our URI. michael@0: let uri = NetUtil.newURI("http://localhost:" + michael@0: server.identity.primaryPort + "/test"); michael@0: michael@0: // Open our URI asynchronously. michael@0: NetUtil.asyncFetch(uri, function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that we got the right data. michael@0: do_check_eq(aInputStream.available(), TEST_DATA.length); michael@0: let is = Cc["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(Ci.nsIScriptableInputStream); michael@0: is.init(aInputStream); michael@0: let result = is.read(TEST_DATA.length); michael@0: do_check_eq(TEST_DATA, result); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: } michael@0: michael@0: function test_asyncFetch_with_string() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: michael@0: // Start the http server, and register our handler. michael@0: let server = new HttpServer(); michael@0: server.registerPathHandler("/test", function(aRequest, aResponse) { michael@0: aResponse.setStatusLine(aRequest.httpVersion, 200, "OK"); michael@0: aResponse.setHeader("Content-Type", "text/plain", false); michael@0: aResponse.write(TEST_DATA); michael@0: }); michael@0: server.start(-1); michael@0: michael@0: // Open our location asynchronously. michael@0: NetUtil.asyncFetch("http://localhost:" + michael@0: server.identity.primaryPort + "/test", michael@0: function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that we got the right data. michael@0: do_check_eq(aInputStream.available(), TEST_DATA.length); michael@0: let is = Cc["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(Ci.nsIScriptableInputStream); michael@0: is.init(aInputStream); michael@0: let result = is.read(TEST_DATA.length); michael@0: do_check_eq(TEST_DATA, result); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: } michael@0: michael@0: function test_asyncFetch_with_nsIFile() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: michael@0: // First we need a file to read from. michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-asyncFetch-test-file.tmp"); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: // Write the test data to the file. michael@0: let ostream = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(file, -1, -1, 0); michael@0: ostream.write(TEST_DATA, TEST_DATA.length); michael@0: michael@0: // Sanity check to make sure the data was written. michael@0: do_check_eq(TEST_DATA, getFileContents(file)); michael@0: michael@0: // Open our file asynchronously. michael@0: NetUtil.asyncFetch(file, function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that we got the right data. michael@0: do_check_eq(aInputStream.available(), TEST_DATA.length); michael@0: let is = Cc["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(Ci.nsIScriptableInputStream); michael@0: is.init(aInputStream); michael@0: let result = is.read(TEST_DATA.length); michael@0: do_check_eq(TEST_DATA, result); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function test_asyncFetch_with_nsIInputString() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: istream.setData(TEST_DATA, TEST_DATA.length); michael@0: michael@0: // Read the input stream asynchronously. michael@0: NetUtil.asyncFetch(istream, function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that we got the right data. michael@0: do_check_eq(aInputStream.available(), TEST_DATA.length); michael@0: do_check_eq(NetUtil.readInputStreamToString(aInputStream, TEST_DATA.length), michael@0: TEST_DATA); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function test_asyncFetch_does_not_block() michael@0: { michael@0: // Create our channel that has no data. michael@0: let channel = NetUtil.ioService. michael@0: newChannel("data:text/plain,", null, null); michael@0: michael@0: // Open our channel asynchronously. michael@0: NetUtil.asyncFetch(channel, function(aInputStream, aResult) { michael@0: // Check that we had success. michael@0: do_check_true(Components.isSuccessCode(aResult)); michael@0: michael@0: // Check that reading a byte throws that the stream was closed (as opposed michael@0: // saying it would block). michael@0: let is = Cc["@mozilla.org/scriptableinputstream;1"]. michael@0: createInstance(Ci.nsIScriptableInputStream); michael@0: is.init(aInputStream); michael@0: try { michael@0: is.read(1); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_BASE_STREAM_CLOSED); michael@0: } michael@0: michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function test_newChannel_no_specifier() michael@0: { michael@0: try { michael@0: NetUtil.newChannel(); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_newChannel_with_string() michael@0: { michael@0: const TEST_SPEC = "http://mozilla.org"; michael@0: michael@0: // Check that we get the same URI back from channel the IO service creates and michael@0: // the channel the utility method creates. michael@0: let ios = NetUtil.ioService; michael@0: let iosChannel = ios.newChannel(TEST_SPEC, null, null); michael@0: let NetUtilChannel = NetUtil.newChannel(TEST_SPEC); michael@0: do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_newChannel_with_nsIURI() michael@0: { michael@0: const TEST_SPEC = "http://mozilla.org"; michael@0: michael@0: // Check that we get the same URI back from channel the IO service creates and michael@0: // the channel the utility method creates. michael@0: let uri = NetUtil.newURI(TEST_SPEC); michael@0: let iosChannel = NetUtil.ioService.newChannelFromURI(uri); michael@0: let NetUtilChannel = NetUtil.newChannel(uri); michael@0: do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_newChannel_with_nsIFile() michael@0: { michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("ProfD", Ci.nsIFile); michael@0: file.append("NetUtil-test-file.tmp"); michael@0: michael@0: // Check that we get the same URI back from channel the IO service creates and michael@0: // the channel the utility method creates. michael@0: let uri = NetUtil.newURI(file); michael@0: let iosChannel = NetUtil.ioService.newChannelFromURI(uri); michael@0: let NetUtilChannel = NetUtil.newChannel(uri); michael@0: do_check_true(iosChannel.URI.equals(NetUtilChannel.URI)); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString() michael@0: { michael@0: const TEST_DATA = "this is a test string\0 with an embedded null"; michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsISupportsCString); michael@0: istream.data = TEST_DATA; michael@0: michael@0: do_check_eq(NetUtil.readInputStreamToString(istream, TEST_DATA.length), michael@0: TEST_DATA); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_no_input_stream() michael@0: { michael@0: try { michael@0: NetUtil.readInputStreamToString("hi", 2); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_no_bytes_arg() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: istream.setData(TEST_DATA, TEST_DATA.length); michael@0: michael@0: try { michael@0: NetUtil.readInputStreamToString(istream); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_blocking_stream() michael@0: { michael@0: let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe); michael@0: pipe.init(true, true, 0, 0, null); michael@0: michael@0: try { michael@0: NetUtil.readInputStreamToString(pipe.inputStream, 10); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_BASE_STREAM_WOULD_BLOCK); michael@0: } michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_too_many_bytes() michael@0: { michael@0: const TEST_DATA = "this is a test string"; michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: istream.setData(TEST_DATA, TEST_DATA.length); michael@0: michael@0: try { michael@0: NetUtil.readInputStreamToString(istream, TEST_DATA.length + 10); michael@0: do_throw("should throw!"); michael@0: } michael@0: catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_with_charset() michael@0: { michael@0: const TEST_DATA = "\uff10\uff11\uff12\uff13"; michael@0: const TEST_DATA_UTF8 = "\xef\xbc\x90\xef\xbc\x91\xef\xbc\x92\xef\xbc\x93"; michael@0: const TEST_DATA_SJIS = "\x82\x4f\x82\x50\x82\x51\x82\x52"; michael@0: michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: michael@0: istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); michael@0: do_check_eq(NetUtil.readInputStreamToString(istream, michael@0: TEST_DATA_UTF8.length, michael@0: { charset: "UTF-8"}), michael@0: TEST_DATA); michael@0: michael@0: istream.setData(TEST_DATA_SJIS, TEST_DATA_SJIS.length); michael@0: do_check_eq(NetUtil.readInputStreamToString(istream, michael@0: TEST_DATA_SJIS.length, michael@0: { charset: "Shift_JIS"}), michael@0: TEST_DATA); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_readInputStreamToString_invalid_sequence() michael@0: { michael@0: const TEST_DATA = "\ufffd\ufffd\ufffd\ufffd"; michael@0: const TEST_DATA_UTF8 = "\xaa\xaa\xaa\xaa"; michael@0: michael@0: let istream = Cc["@mozilla.org/io/string-input-stream;1"]. michael@0: createInstance(Ci.nsIStringInputStream); michael@0: michael@0: istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); michael@0: try { michael@0: NetUtil.readInputStreamToString(istream, michael@0: TEST_DATA_UTF8.length, michael@0: { charset: "UTF-8" }); michael@0: do_throw("should throw!"); michael@0: } catch (e) { michael@0: do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_INPUT); michael@0: } michael@0: michael@0: istream.setData(TEST_DATA_UTF8, TEST_DATA_UTF8.length); michael@0: do_check_eq(NetUtil.readInputStreamToString(istream, michael@0: TEST_DATA_UTF8.length, { michael@0: charset: "UTF-8", michael@0: replacement: Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER}), michael@0: TEST_DATA); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Runner michael@0: michael@0: [ michael@0: test_async_copy, michael@0: test_async_write_file, michael@0: test_async_write_file_deferred, michael@0: test_async_write_file_safe, michael@0: test_async_write_file_safe_deferred, michael@0: test_newURI_no_spec_throws, michael@0: test_newURI, michael@0: test_newURI_takes_nsIFile, michael@0: test_ioService, michael@0: test_asyncFetch_no_channel, michael@0: test_asyncFetch_no_callback, michael@0: test_asyncFetch_with_nsIChannel, michael@0: test_asyncFetch_with_nsIURI, michael@0: test_asyncFetch_with_string, michael@0: test_asyncFetch_with_nsIFile, michael@0: test_asyncFetch_with_nsIInputString, michael@0: test_asyncFetch_does_not_block, michael@0: test_newChannel_no_specifier, michael@0: test_newChannel_with_string, michael@0: test_newChannel_with_nsIURI, michael@0: test_newChannel_with_nsIFile, michael@0: test_readInputStreamToString, michael@0: test_readInputStreamToString_no_input_stream, michael@0: test_readInputStreamToString_no_bytes_arg, michael@0: test_readInputStreamToString_blocking_stream, michael@0: test_readInputStreamToString_too_many_bytes, michael@0: test_readInputStreamToString_with_charset, michael@0: test_readInputStreamToString_invalid_sequence, michael@0: ].forEach(add_test); michael@0: let index = 0; michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: