test-net-url.js 6.0 KB

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