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