addon-sdk/source/test/test-net-url.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "use strict";
michael@0 6
michael@0 7 const { readURI, readURISync } = require("sdk/net/url");
michael@0 8 const data = require("./fixtures");
michael@0 9
michael@0 10 const utf8text = "Hello, ゼロ!";
michael@0 11 const latin1text = "Hello, ゼロ!";
michael@0 12
michael@0 13 const dataURIutf8 = "data:text/plain;charset=utf-8," + encodeURIComponent(utf8text);
michael@0 14 const dataURIlatin1 = "data:text/plain;charset=ISO-8859-1," + escape(latin1text);
michael@0 15 const chromeURI = "chrome://global-platform/locale/accessible.properties";
michael@0 16
michael@0 17 exports["test async readURI"] = function(assert, done) {
michael@0 18 let content = "";
michael@0 19
michael@0 20 readURI(data.url("test-net-url.txt")).then(function(data) {
michael@0 21 content = data;
michael@0 22 assert.equal(content, utf8text, "The URL content is loaded properly");
michael@0 23 done();
michael@0 24 }, function() {
michael@0 25 assert.fail("should not reject");
michael@0 26 done();
michael@0 27 })
michael@0 28
michael@0 29 assert.equal(content, "", "The URL content is not load yet");
michael@0 30 }
michael@0 31
michael@0 32 exports["test readURISync"] = function(assert) {
michael@0 33 let content = readURISync(data.url("test-net-url.txt"));
michael@0 34
michael@0 35 assert.equal(content, utf8text, "The URL content is loaded properly");
michael@0 36 }
michael@0 37
michael@0 38 exports["test async readURI with ISO-8859-1 charset"] = function(assert, done) {
michael@0 39 let content = "";
michael@0 40
michael@0 41 readURI(data.url("test-net-url.txt"), { charset : "ISO-8859-1"}).then(function(data) {
michael@0 42 content = data;
michael@0 43 assert.equal(content, latin1text, "The URL content is loaded properly");
michael@0 44 done();
michael@0 45 }, function() {
michael@0 46 assert.fail("should not reject");
michael@0 47 done();
michael@0 48 })
michael@0 49
michael@0 50 assert.equal(content, "", "The URL content is not load yet");
michael@0 51 }
michael@0 52
michael@0 53 exports["test readURISync with ISO-8859-1 charset"] = function(assert) {
michael@0 54 let content = readURISync(data.url("test-net-url.txt"), "ISO-8859-1");
michael@0 55
michael@0 56 assert.equal(content, latin1text, "The URL content is loaded properly");
michael@0 57 }
michael@0 58
michael@0 59 exports["test async readURI with not existing file"] = function(assert, done) {
michael@0 60 readURI(data.url("test-net-url-fake.txt")).then(function(data) {
michael@0 61 assert.fail("should not resolve");
michael@0 62 done();
michael@0 63 }, function(reason) {
michael@0 64 assert.ok(reason.indexOf("Failed to read:") === 0);
michael@0 65 done();
michael@0 66 })
michael@0 67 }
michael@0 68
michael@0 69 exports["test readURISync with not existing file"] = function(assert) {
michael@0 70 assert.throws(function() {
michael@0 71 readURISync(data.url("test-net-url-fake.txt"));
michael@0 72 }, /NS_ERROR_FILE_NOT_FOUND/);
michael@0 73 }
michael@0 74
michael@0 75 exports["test async readURI with data URI"] = function(assert, done) {
michael@0 76 let content = "";
michael@0 77
michael@0 78 readURI(dataURIutf8).then(function(data) {
michael@0 79 content = data;
michael@0 80 assert.equal(content, utf8text, "The URL content is loaded properly");
michael@0 81 done();
michael@0 82 }, function() {
michael@0 83 assert.fail("should not reject");
michael@0 84 done();
michael@0 85 })
michael@0 86
michael@0 87 assert.equal(content, "", "The URL content is not load yet");
michael@0 88 }
michael@0 89
michael@0 90 exports["test readURISync with data URI"] = function(assert) {
michael@0 91 let content = readURISync(dataURIutf8);
michael@0 92
michael@0 93 assert.equal(content, utf8text, "The URL content is loaded properly");
michael@0 94 }
michael@0 95
michael@0 96 exports["test async readURI with data URI and ISO-8859-1 charset"] = function(assert, done) {
michael@0 97 let content = "";
michael@0 98
michael@0 99 readURI(dataURIlatin1, { charset : "ISO-8859-1"}).then(function(data) {
michael@0 100 content = unescape(data);
michael@0 101 assert.equal(content, latin1text, "The URL content is loaded properly");
michael@0 102 done();
michael@0 103 }, function() {
michael@0 104 assert.fail("should not reject");
michael@0 105 done();
michael@0 106 })
michael@0 107
michael@0 108 assert.equal(content, "", "The URL content is not load yet");
michael@0 109 }
michael@0 110
michael@0 111 exports["test readURISync with data URI and ISO-8859-1 charset"] = function(assert) {
michael@0 112 let content = unescape(readURISync(dataURIlatin1, "ISO-8859-1"));
michael@0 113
michael@0 114 assert.equal(content, latin1text, "The URL content is loaded properly");
michael@0 115 }
michael@0 116
michael@0 117 exports["test readURISync with chrome URI"] = function(assert) {
michael@0 118 let content = readURISync(chromeURI);
michael@0 119
michael@0 120 assert.ok(content, "The URL content is loaded properly");
michael@0 121 }
michael@0 122
michael@0 123 exports["test async readURI with chrome URI"] = function(assert, done) {
michael@0 124 let content = "";
michael@0 125
michael@0 126 readURI(chromeURI).then(function(data) {
michael@0 127 content = data;
michael@0 128 assert.equal(content, readURISync(chromeURI), "The URL content is loaded properly");
michael@0 129 done();
michael@0 130 }, function() {
michael@0 131 assert.fail("should not reject");
michael@0 132 done();
michael@0 133 })
michael@0 134
michael@0 135 assert.equal(content, "", "The URL content is not load yet");
michael@0 136 }
michael@0 137
michael@0 138 require("test").run(exports)

mercurial