Initial commit
[yaffs-website] / node_modules / hoek / test / escaper.js
1 // Load modules
2
3 var Code = require('code');
4 var Hoek = require('../lib');
5 var Lab = require('lab');
6
7
8 // Declare internals
9
10 var internals = {};
11
12
13 // Test shortcuts
14
15 var lab = exports.lab = Lab.script();
16 var describe = lab.experiment;
17 var it = lab.test;
18 var expect = Code.expect;
19
20
21 describe('escapeJavaScript()', function () {
22
23     it('encodes / characters', function (done) {
24
25         var encoded = Hoek.escapeJavaScript('<script>alert(1)</script>');
26         expect(encoded).to.equal('\\x3cscript\\x3ealert\\x281\\x29\\x3c\\x2fscript\\x3e');
27         done();
28     });
29
30     it('encodes \' characters', function (done) {
31
32         var encoded = Hoek.escapeJavaScript('something(\'param\')');
33         expect(encoded).to.equal('something\\x28\\x27param\\x27\\x29');
34         done();
35     });
36
37     it('encodes large unicode characters with the correct padding', function (done) {
38
39         var encoded = Hoek.escapeJavaScript(String.fromCharCode(500) + String.fromCharCode(1000));
40         expect(encoded).to.equal('\\u0500\\u1000');
41         done();
42     });
43
44     it('doesn\'t throw an exception when passed null', function (done) {
45
46         var encoded = Hoek.escapeJavaScript(null);
47         expect(encoded).to.equal('');
48         done();
49     });
50 });
51
52 describe('escapeHtml()', function () {
53
54     it('encodes / characters', function (done) {
55
56         var encoded = Hoek.escapeHtml('<script>alert(1)</script>');
57         expect(encoded).to.equal('&lt;script&gt;alert&#x28;1&#x29;&lt;&#x2f;script&gt;');
58         done();
59     });
60
61     it('encodes < and > as named characters', function (done) {
62
63         var encoded = Hoek.escapeHtml('<script><>');
64         expect(encoded).to.equal('&lt;script&gt;&lt;&gt;');
65         done();
66     });
67
68     it('encodes large unicode characters', function (done) {
69
70         var encoded = Hoek.escapeHtml(String.fromCharCode(500) + String.fromCharCode(1000));
71         expect(encoded).to.equal('&#500;&#1000;');
72         done();
73     });
74
75     it('doesn\'t throw an exception when passed null', function (done) {
76
77         var encoded = Hoek.escapeHtml(null);
78         expect(encoded).to.equal('');
79         done();
80     });
81
82     it('encodes {} characters', function (done) {
83
84         var encoded = Hoek.escapeHtml('{}');
85         expect(encoded).to.equal('&#x7b;&#x7d;');
86         done();
87     });
88 });