escaper.js
2.27 KB
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
// Load modules
var Lab = require('lab');
var Hoek = require('../lib');
// Declare internals
var internals = {};
// Test shortcuts
var expect = Lab.expect;
var before = Lab.before;
var after = Lab.after;
var describe = Lab.experiment;
var it = Lab.test;
describe('Hoek', function () {
describe('#escapeJavaScript', function () {
it('encodes / characters', function (done) {
var encoded = Hoek.escapeJavaScript('<script>alert(1)</script>');
expect(encoded).to.equal('\\x3cscript\\x3ealert\\x281\\x29\\x3c\\x2fscript\\x3e');
done();
});
it('encodes \' characters', function (done) {
var encoded = Hoek.escapeJavaScript('something(\'param\')');
expect(encoded).to.equal('something\\x28\\x27param\\x27\\x29');
done();
});
it('encodes large unicode characters with the correct padding', function (done) {
var encoded = Hoek.escapeJavaScript(String.fromCharCode(500) + String.fromCharCode(1000));
expect(encoded).to.equal('\\u0500\\u1000');
done();
});
it('doesn\'t throw an exception when passed null', function (done) {
var encoded = Hoek.escapeJavaScript(null);
expect(encoded).to.equal('');
done();
});
});
describe('#escapeHtml', function () {
it('encodes / characters', function (done) {
var encoded = Hoek.escapeHtml('<script>alert(1)</script>');
expect(encoded).to.equal('<script>alert(1)</script>');
done();
});
it('encodes < and > as named characters', function (done) {
var encoded = Hoek.escapeHtml('<script><>');
expect(encoded).to.equal('<script><>');
done();
});
it('encodes large unicode characters', function (done) {
var encoded = Hoek.escapeHtml(String.fromCharCode(500) + String.fromCharCode(1000));
expect(encoded).to.equal('ǴϨ');
done();
});
it('doesn\'t throw an exception when passed null', function (done) {
var encoded = Hoek.escapeHtml(null);
expect(encoded).to.equal('');
done();
});
});
});