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