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: "use strict"; michael@0: michael@0: const { readURI, readURISync } = require("sdk/net/url"); michael@0: const data = require("./fixtures"); michael@0: michael@0: const utf8text = "Hello, ゼロ!"; michael@0: const latin1text = "Hello, ゼロ!"; michael@0: michael@0: const dataURIutf8 = "data:text/plain;charset=utf-8," + encodeURIComponent(utf8text); michael@0: const dataURIlatin1 = "data:text/plain;charset=ISO-8859-1," + escape(latin1text); michael@0: const chromeURI = "chrome://global-platform/locale/accessible.properties"; michael@0: michael@0: exports["test async readURI"] = function(assert, done) { michael@0: let content = ""; michael@0: michael@0: readURI(data.url("test-net-url.txt")).then(function(data) { michael@0: content = data; michael@0: assert.equal(content, utf8text, "The URL content is loaded properly"); michael@0: done(); michael@0: }, function() { michael@0: assert.fail("should not reject"); michael@0: done(); michael@0: }) michael@0: michael@0: assert.equal(content, "", "The URL content is not load yet"); michael@0: } michael@0: michael@0: exports["test readURISync"] = function(assert) { michael@0: let content = readURISync(data.url("test-net-url.txt")); michael@0: michael@0: assert.equal(content, utf8text, "The URL content is loaded properly"); michael@0: } michael@0: michael@0: exports["test async readURI with ISO-8859-1 charset"] = function(assert, done) { michael@0: let content = ""; michael@0: michael@0: readURI(data.url("test-net-url.txt"), { charset : "ISO-8859-1"}).then(function(data) { michael@0: content = data; michael@0: assert.equal(content, latin1text, "The URL content is loaded properly"); michael@0: done(); michael@0: }, function() { michael@0: assert.fail("should not reject"); michael@0: done(); michael@0: }) michael@0: michael@0: assert.equal(content, "", "The URL content is not load yet"); michael@0: } michael@0: michael@0: exports["test readURISync with ISO-8859-1 charset"] = function(assert) { michael@0: let content = readURISync(data.url("test-net-url.txt"), "ISO-8859-1"); michael@0: michael@0: assert.equal(content, latin1text, "The URL content is loaded properly"); michael@0: } michael@0: michael@0: exports["test async readURI with not existing file"] = function(assert, done) { michael@0: readURI(data.url("test-net-url-fake.txt")).then(function(data) { michael@0: assert.fail("should not resolve"); michael@0: done(); michael@0: }, function(reason) { michael@0: assert.ok(reason.indexOf("Failed to read:") === 0); michael@0: done(); michael@0: }) michael@0: } michael@0: michael@0: exports["test readURISync with not existing file"] = function(assert) { michael@0: assert.throws(function() { michael@0: readURISync(data.url("test-net-url-fake.txt")); michael@0: }, /NS_ERROR_FILE_NOT_FOUND/); michael@0: } michael@0: michael@0: exports["test async readURI with data URI"] = function(assert, done) { michael@0: let content = ""; michael@0: michael@0: readURI(dataURIutf8).then(function(data) { michael@0: content = data; michael@0: assert.equal(content, utf8text, "The URL content is loaded properly"); michael@0: done(); michael@0: }, function() { michael@0: assert.fail("should not reject"); michael@0: done(); michael@0: }) michael@0: michael@0: assert.equal(content, "", "The URL content is not load yet"); michael@0: } michael@0: michael@0: exports["test readURISync with data URI"] = function(assert) { michael@0: let content = readURISync(dataURIutf8); michael@0: michael@0: assert.equal(content, utf8text, "The URL content is loaded properly"); michael@0: } michael@0: michael@0: exports["test async readURI with data URI and ISO-8859-1 charset"] = function(assert, done) { michael@0: let content = ""; michael@0: michael@0: readURI(dataURIlatin1, { charset : "ISO-8859-1"}).then(function(data) { michael@0: content = unescape(data); michael@0: assert.equal(content, latin1text, "The URL content is loaded properly"); michael@0: done(); michael@0: }, function() { michael@0: assert.fail("should not reject"); michael@0: done(); michael@0: }) michael@0: michael@0: assert.equal(content, "", "The URL content is not load yet"); michael@0: } michael@0: michael@0: exports["test readURISync with data URI and ISO-8859-1 charset"] = function(assert) { michael@0: let content = unescape(readURISync(dataURIlatin1, "ISO-8859-1")); michael@0: michael@0: assert.equal(content, latin1text, "The URL content is loaded properly"); michael@0: } michael@0: michael@0: exports["test readURISync with chrome URI"] = function(assert) { michael@0: let content = readURISync(chromeURI); michael@0: michael@0: assert.ok(content, "The URL content is loaded properly"); michael@0: } michael@0: michael@0: exports["test async readURI with chrome URI"] = function(assert, done) { michael@0: let content = ""; michael@0: michael@0: readURI(chromeURI).then(function(data) { michael@0: content = data; michael@0: assert.equal(content, readURISync(chromeURI), "The URL content is loaded properly"); michael@0: done(); michael@0: }, function() { michael@0: assert.fail("should not reject"); michael@0: done(); michael@0: }) michael@0: michael@0: assert.equal(content, "", "The URL content is not load yet"); michael@0: } michael@0: michael@0: require("test").run(exports)