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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-net-url.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const { readURI, readURISync } = require("sdk/net/url");
    1.11 +const data = require("./fixtures");
    1.12 +
    1.13 +const utf8text = "Hello, ゼロ!";
    1.14 +const latin1text = "Hello, ゼロ!";
    1.15 +
    1.16 +const dataURIutf8 = "data:text/plain;charset=utf-8," + encodeURIComponent(utf8text);
    1.17 +const dataURIlatin1 = "data:text/plain;charset=ISO-8859-1," + escape(latin1text);
    1.18 +const chromeURI = "chrome://global-platform/locale/accessible.properties";
    1.19 +
    1.20 +exports["test async readURI"] = function(assert, done) {
    1.21 +  let content = "";
    1.22 +
    1.23 +  readURI(data.url("test-net-url.txt")).then(function(data) {
    1.24 +    content = data;
    1.25 +    assert.equal(content, utf8text, "The URL content is loaded properly");
    1.26 +    done();
    1.27 +  }, function() {
    1.28 +    assert.fail("should not reject");
    1.29 +    done();
    1.30 +  })
    1.31 +
    1.32 +  assert.equal(content, "", "The URL content is not load yet");
    1.33 +}
    1.34 +
    1.35 +exports["test readURISync"] = function(assert) {
    1.36 +  let content = readURISync(data.url("test-net-url.txt"));
    1.37 +
    1.38 +  assert.equal(content, utf8text, "The URL content is loaded properly");
    1.39 +}
    1.40 +
    1.41 +exports["test async readURI with ISO-8859-1 charset"] = function(assert, done) {
    1.42 +  let content = "";
    1.43 +
    1.44 +  readURI(data.url("test-net-url.txt"), { charset : "ISO-8859-1"}).then(function(data) {
    1.45 +    content = data;
    1.46 +    assert.equal(content, latin1text, "The URL content is loaded properly");
    1.47 +    done();
    1.48 +  }, function() {
    1.49 +    assert.fail("should not reject");
    1.50 +    done();
    1.51 +  })
    1.52 +
    1.53 +  assert.equal(content, "", "The URL content is not load yet");
    1.54 +}
    1.55 +
    1.56 +exports["test readURISync with ISO-8859-1 charset"] = function(assert) {
    1.57 +  let content = readURISync(data.url("test-net-url.txt"), "ISO-8859-1");
    1.58 +
    1.59 +  assert.equal(content, latin1text, "The URL content is loaded properly");
    1.60 +}
    1.61 +
    1.62 +exports["test async readURI with not existing file"] = function(assert, done) {
    1.63 +  readURI(data.url("test-net-url-fake.txt")).then(function(data) {
    1.64 +    assert.fail("should not resolve");
    1.65 +    done();
    1.66 +  }, function(reason) {
    1.67 +    assert.ok(reason.indexOf("Failed to read:") === 0);
    1.68 +    done();
    1.69 +  })
    1.70 +}
    1.71 +
    1.72 +exports["test readURISync with not existing file"] = function(assert) {
    1.73 +  assert.throws(function() {
    1.74 +    readURISync(data.url("test-net-url-fake.txt"));
    1.75 +  }, /NS_ERROR_FILE_NOT_FOUND/);
    1.76 +}
    1.77 +
    1.78 +exports["test async readURI with data URI"] = function(assert, done) {
    1.79 +  let content = "";
    1.80 +
    1.81 +  readURI(dataURIutf8).then(function(data) {
    1.82 +    content = data;
    1.83 +    assert.equal(content, utf8text, "The URL content is loaded properly");
    1.84 +    done();
    1.85 +  }, function() {
    1.86 +    assert.fail("should not reject");
    1.87 +    done();
    1.88 +  })
    1.89 +
    1.90 +  assert.equal(content, "", "The URL content is not load yet");
    1.91 +}
    1.92 +
    1.93 +exports["test readURISync with data URI"] = function(assert) {
    1.94 +  let content = readURISync(dataURIutf8);
    1.95 +
    1.96 +  assert.equal(content, utf8text, "The URL content is loaded properly");
    1.97 +}
    1.98 +
    1.99 +exports["test async readURI with data URI and ISO-8859-1 charset"] = function(assert, done) {
   1.100 +  let content = "";
   1.101 +
   1.102 +  readURI(dataURIlatin1, { charset : "ISO-8859-1"}).then(function(data) {
   1.103 +    content = unescape(data);
   1.104 +    assert.equal(content, latin1text, "The URL content is loaded properly");
   1.105 +    done();
   1.106 +  }, function() {
   1.107 +    assert.fail("should not reject");
   1.108 +    done();
   1.109 +  })
   1.110 +
   1.111 +  assert.equal(content, "", "The URL content is not load yet");
   1.112 +}
   1.113 +
   1.114 +exports["test readURISync with data URI and ISO-8859-1 charset"] = function(assert) {
   1.115 +  let content = unescape(readURISync(dataURIlatin1, "ISO-8859-1"));
   1.116 +
   1.117 +  assert.equal(content, latin1text, "The URL content is loaded properly");
   1.118 +}
   1.119 +
   1.120 +exports["test readURISync with chrome URI"] = function(assert) {
   1.121 +  let content = readURISync(chromeURI);
   1.122 +
   1.123 +  assert.ok(content, "The URL content is loaded properly");
   1.124 +}
   1.125 +
   1.126 +exports["test async readURI with chrome URI"] = function(assert, done) {
   1.127 +  let content = "";
   1.128 +
   1.129 +  readURI(chromeURI).then(function(data) {
   1.130 +    content = data;
   1.131 +    assert.equal(content, readURISync(chromeURI), "The URL content is loaded properly");
   1.132 +    done();
   1.133 +  }, function() {
   1.134 +    assert.fail("should not reject");
   1.135 +    done();
   1.136 +  })
   1.137 +
   1.138 +  assert.equal(content, "", "The URL content is not load yet");
   1.139 +}
   1.140 +
   1.141 +require("test").run(exports)

mercurial