|
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 base64 = require("sdk/base64"); |
|
8 |
|
9 const text = "Awesome!"; |
|
10 const b64text = "QXdlc29tZSE="; |
|
11 |
|
12 const utf8text = "✓ à la mode"; |
|
13 const b64utf8text = "4pyTIMOgIGxhIG1vZGU="; |
|
14 |
|
15 exports["test base64.encode"] = function (assert) { |
|
16 assert.equal(base64.encode(text), b64text, "encode correctly") |
|
17 } |
|
18 |
|
19 exports["test base64.decode"] = function (assert) { |
|
20 assert.equal(base64.decode(b64text), text, "decode correctly") |
|
21 } |
|
22 |
|
23 exports["test base64.encode Unicode"] = function (assert) { |
|
24 |
|
25 assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text, |
|
26 "encode correctly Unicode strings.") |
|
27 } |
|
28 |
|
29 exports["test base64.decode Unicode"] = function (assert) { |
|
30 |
|
31 assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text, |
|
32 "decode correctly Unicode strings.") |
|
33 } |
|
34 |
|
35 exports["test base64.encode with wrong charset"] = function (assert) { |
|
36 |
|
37 assert.throws(function() { |
|
38 base64.encode(utf8text, "utf-16"); |
|
39 }, "The charset argument can be only 'utf-8'"); |
|
40 |
|
41 assert.throws(function() { |
|
42 base64.encode(utf8text, ""); |
|
43 }, "The charset argument can be only 'utf-8'"); |
|
44 |
|
45 assert.throws(function() { |
|
46 base64.encode(utf8text, 8); |
|
47 }, "The charset argument can be only 'utf-8'"); |
|
48 |
|
49 } |
|
50 |
|
51 exports["test base64.decode with wrong charset"] = function (assert) { |
|
52 |
|
53 assert.throws(function() { |
|
54 base64.decode(utf8text, "utf-16"); |
|
55 }, "The charset argument can be only 'utf-8'"); |
|
56 |
|
57 assert.throws(function() { |
|
58 base64.decode(utf8text, ""); |
|
59 }, "The charset argument can be only 'utf-8'"); |
|
60 |
|
61 assert.throws(function() { |
|
62 base64.decode(utf8text, 8); |
|
63 }, "The charset argument can be only 'utf-8'"); |
|
64 |
|
65 } |
|
66 |
|
67 exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) { |
|
68 |
|
69 assert.notEqual(base64.decode(base64.encode(utf8text)), utf8text, |
|
70 "Unicode strings needs 'utf-8' charset" |
|
71 ); |
|
72 |
|
73 } |
|
74 |
|
75 require("test").run(exports); |