2015-02-28 22:04:46 +08:00
var assert = require ( 'assert' ) ;
var util = require ( '../src/js/util' ) ;
2016-04-05 03:28:23 +08:00
// console.log('TEST', util.parsePath('.items[3].name'));
// console.log('TEST', util.parsePath('.items[*].name'));
2015-02-28 22:04:46 +08:00
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"}' ) ;
2016-01-16 21:49:04 +08:00
assert . equal ( util . sanitize ( '{a:\'foo\',b:\'bar\'}' ) , '{"a":"foo","b":"bar"}' ) ;
2015-02-28 22:04:46 +08:00
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"' ) ;
} ) ;
2017-07-14 03:01:52 +08:00
it ( 'should escape unescaped control characters' , function ( ) {
assert . equal ( util . sanitize ( '"hello\bworld"' ) , '"hello\\bworld"' )
assert . equal ( util . sanitize ( '"hello\fworld"' ) , '"hello\\fworld"' )
assert . equal ( util . sanitize ( '"hello\nworld"' ) , '"hello\\nworld"' )
assert . equal ( util . sanitize ( '"hello\rworld"' ) , '"hello\\rworld"' )
assert . equal ( util . sanitize ( '"hello\tworld"' ) , '"hello\\tworld"' )
assert . equal ( util . sanitize ( '{"value\n": "dc=hcm,dc=com"}' ) , '{"value\\n": "dc=hcm,dc=com"}' )
} )
2015-02-28 23:54:34 +08:00
it ( 'remove comments' , function ( ) {
assert . equal ( util . sanitize ( '/* foo */ {}' ) , ' {}' ) ;
2016-01-16 21:49:04 +08:00
assert . equal ( util . sanitize ( '/* foo */ {}' ) , ' {}' ) ;
assert . equal ( util . sanitize ( '{a:\'foo\',/*hello*/b:\'bar\'}' ) , '{"a":"foo","b":"bar"}' ) ;
assert . equal ( util . sanitize ( '{\na:\'foo\',//hello\nb:\'bar\'\n}' ) , '{\n"a":"foo",\n"b":"bar"\n}' ) ;
2015-02-28 23:54:34 +08:00
// 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 ( {} )' ) , ' {} ' ) ;
2015-03-01 00:03:40 +08:00
assert . equal ( util . sanitize ( ' /* foo bar */ callback_123 ({}); ' ) , '{}' ) ;
assert . equal ( util . sanitize ( '\n/* foo\nbar */\ncallback_123 ({});\n\n' ) , '{}' ) ;
2015-02-28 22:04:46 +08:00
// non-matching
assert . equal ( util . sanitize ( 'callback abc({});' ) , 'callback abc({});' ) ;
assert . equal ( util . sanitize ( 'callback {}' ) , 'callback {}' ) ;
assert . equal ( util . sanitize ( 'callback({}' ) , 'callback({}' ) ;
} ) ;
} ) ;
2016-01-12 00:06:12 +08:00
describe ( 'jsonPath' , function ( ) {
it ( 'should parse a json path' , function ( ) {
assert . deepEqual ( util . parsePath ( '' ) , [ ] ) ;
assert . deepEqual ( util . parsePath ( '.foo' ) , [ 'foo' ] ) ;
assert . deepEqual ( util . parsePath ( '.foo.bar' ) , [ 'foo' , 'bar' ] ) ;
assert . deepEqual ( util . parsePath ( '.foo[2]' ) , [ 'foo' , 2 ] ) ;
assert . deepEqual ( util . parsePath ( '.foo[2].bar' ) , [ 'foo' , 2 , 'bar' ] ) ;
assert . deepEqual ( util . parsePath ( '.foo["prop with spaces"]' ) , [ 'foo' , 'prop with spaces' ] ) ;
2016-09-23 06:06:22 +08:00
assert . deepEqual ( util . parsePath ( '.foo[\'prop with single quotes as outputted by ajv library\']' ) , [ 'foo' , 'prop with single quotes as outputted by ajv library' ] ) ;
2016-04-05 03:28:23 +08:00
assert . deepEqual ( util . parsePath ( '.foo[*].bar' ) , [ 'foo' , '*' , 'bar' ] ) ;
2016-01-12 00:06:12 +08:00
} ) ;
it ( 'should throw an exception in case of an invalid path' , function ( ) {
assert . throws ( function ( ) { util . parsePath ( '.' ) } , /Error/ ) ;
assert . throws ( function ( ) { util . parsePath ( '[' ) } , /Error/ ) ;
assert . throws ( function ( ) { util . parsePath ( '[]' ) } , /Error/ ) ;
assert . throws ( function ( ) { util . parsePath ( '.[]' ) } , /Error/ ) ;
assert . throws ( function ( ) { util . parsePath ( '["23]' ) } , /Error/ ) ;
assert . throws ( function ( ) { util . parsePath ( '.foo bar' ) } , /Error/ ) ;
} ) ;
} ) ;
2015-02-28 22:04:46 +08:00
// TODO: thoroughly test all util methods
} ) ;