summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-net-url.js
blob: 9e463b798c821df67996915afca0628df4541092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"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 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 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 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 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 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");
}

require("sdk/test").run(exports)