2015-02-28 22:04:46 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('../src/js/util');
|
|
|
|
|
|
|
|
describe('util', function () {
|
|
|
|
|
|
|
|
describe('sanitize', function () {
|
|
|
|
|
2015-02-28 23:54:34 +08:00
|
|
|
it('should leave valid JSON as is', function () {
|
|
|
|
assert.equal(util.sanitize('{"a":2}'), '{"a":2}');
|
|
|
|
});
|
|
|
|
|
2015-02-28 22:04:46 +08:00
|
|
|
it('should replace JavaScript with JSON', function () {
|
|
|
|
assert.equal(util.sanitize('{a:2}'), '{"a":2}');
|
|
|
|
assert.equal(util.sanitize('{\'a\':2}'), '{"a":2}');
|
|
|
|
assert.equal(util.sanitize('{a:\'foo\'}'), '{"a":"foo"}');
|
|
|
|
|
2015-02-28 23:54:34 +08:00
|
|
|
// should leave string content untouched
|
|
|
|
assert.equal(util.sanitize('"{a:b}"'), '"{a:b}"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add/remove escape characters', function () {
|
|
|
|
assert.equal(util.sanitize('"foo\'bar"'), '"foo\'bar"');
|
|
|
|
assert.equal(util.sanitize('"foo\\"bar"'), '"foo\\"bar"');
|
|
|
|
assert.equal(util.sanitize('\'foo"bar\''), '"foo\\"bar"');
|
|
|
|
assert.equal(util.sanitize('\'foo\\\'bar\''), '"foo\'bar"');
|
|
|
|
assert.equal(util.sanitize('"foo\\\'bar"'), '"foo\'bar"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('remove comments', function () {
|
|
|
|
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
|
|
|
|
|
|
|
|
// should not remove comments in string
|
|
|
|
assert.equal(util.sanitize('{"str":"/* foo */"}'), '{"str":"/* foo */"}');
|
2015-02-28 22:04:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should strip JSONP notation', function () {
|
|
|
|
// matching
|
|
|
|
assert.equal(util.sanitize('callback_123({});'), '{}');
|
|
|
|
assert.equal(util.sanitize('callback_123([]);'), '[]');
|
|
|
|
assert.equal(util.sanitize('callback_123(2);'), '2');
|
|
|
|
assert.equal(util.sanitize('callback_123("foo");'), '"foo"');
|
|
|
|
assert.equal(util.sanitize('callback_123(null);'), 'null');
|
|
|
|
assert.equal(util.sanitize('callback_123(true);'), 'true');
|
|
|
|
assert.equal(util.sanitize('callback_123(false);'), 'false');
|
|
|
|
assert.equal(util.sanitize('/* foo bar */ callback_123 ({})'), '{}');
|
|
|
|
assert.equal(util.sanitize('/* foo bar */ callback_123 ({})'), '{}');
|
|
|
|
assert.equal(util.sanitize('/* foo bar */\ncallback_123({})'), '{}');
|
|
|
|
assert.equal(util.sanitize('/* foo bar */ callback_123 ( {} )'), ' {} ');
|
|
|
|
|
|
|
|
// non-matching
|
|
|
|
assert.equal(util.sanitize('callback abc({});'), 'callback abc({});');
|
|
|
|
assert.equal(util.sanitize('callback {}'), 'callback {}');
|
|
|
|
assert.equal(util.sanitize('callback({}'), 'callback({}');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: thoroughly test all util methods
|
|
|
|
});
|