@@ -26,6 +26,7 @@ var parseUrl = utils.parseUrl;
2626var safeJoin = utils . safeJoin ;
2727var serializeException = utils . serializeException ;
2828var serializeKeysForMessage = utils . serializeKeysForMessage ;
29+ var sanitize = utils . sanitize ;
2930
3031describe ( 'utils' , function ( ) {
3132 describe ( 'isUndefined' , function ( ) {
@@ -696,4 +697,105 @@ describe('utils', function() {
696697 assert . equal ( serializeKeysForMessage ( 'foo' ) , 'foo' ) ;
697698 } ) ;
698699 } ) ;
700+
701+ describe ( 'sanitize' , function ( ) {
702+ var sanitizeMask = '********' ;
703+
704+ it ( 'should return simple values directly' , function ( ) {
705+ var actual = sanitize ( 'foo' ) ;
706+ var expected = 'foo' ;
707+ assert . deepEqual ( actual , expected ) ;
708+ } ) ;
709+
710+ it ( 'should return same value when no sanitizeKeys passed' , function ( ) {
711+ var actual = sanitize ( { foo : 42 } ) ;
712+ var expected = { foo : 42 } ;
713+ assert . deepEqual ( actual , expected ) ;
714+ } ) ;
715+
716+ it ( 'should return same value when empty sanitizeKeys array passed' , function ( ) {
717+ var actual = sanitize ( { foo : 42 } , [ ] ) ;
718+ var expected = { foo : 42 } ;
719+ assert . deepEqual ( actual , expected ) ;
720+ } ) ;
721+
722+ it ( 'should sanitize flat objects' , function ( ) {
723+ var actual = sanitize ( { foo : 42 } , [ 'foo' ] ) ;
724+ var expected = { foo : sanitizeMask } ;
725+ assert . deepEqual ( actual , expected ) ;
726+ } ) ;
727+
728+ it ( 'should sanitize flat objects with multiple keys' , function ( ) {
729+ var actual = sanitize ( { foo : 42 , bar : 'abc' , baz : 1337 } , [ 'foo' , 'baz' ] ) ;
730+ var expected = { foo : sanitizeMask , bar : 'abc' , baz : sanitizeMask } ;
731+ assert . deepEqual ( actual , expected ) ;
732+ } ) ;
733+
734+ it ( 'should sanitize flat objects when value is a plain object or array' , function ( ) {
735+ var actual = sanitize ( { foo : { bar : 42 } } , [ 'foo' ] ) ;
736+ var expected = { foo : sanitizeMask } ;
737+ assert . deepEqual ( actual , expected ) ;
738+
739+ actual = sanitize ( { foo : [ 42 , 'abc' ] } , [ 'foo' ] ) ;
740+ expected = { foo : sanitizeMask } ;
741+ assert . deepEqual ( actual , expected ) ;
742+ } ) ;
743+
744+ it ( 'should sanitize nested objects keys' , function ( ) {
745+ var actual = sanitize ( { foo : { bar : 42 } } , [ 'bar' ] ) ;
746+ var expected = { foo : { bar : sanitizeMask } } ;
747+ assert . deepEqual ( actual , expected ) ;
748+ } ) ;
749+
750+ it ( 'should sanitize objects nested in arrays' , function ( ) {
751+ var actual = sanitize ( { foo : [ { bar : 42 } , 42 ] } , [ 'bar' ] ) ;
752+ var expected = { foo : [ { bar : sanitizeMask } , 42 ] } ;
753+ assert . deepEqual ( actual , expected ) ;
754+ } ) ;
755+
756+ it ( 'should sanitize every object when array provided as input' , function ( ) {
757+ var actual = sanitize ( [ { foo : 42 } , { bar : 42 } , 42 ] , [ 'foo' , 'bar' ] ) ;
758+ var expected = [ { foo : sanitizeMask } , { bar : sanitizeMask } , 42 ] ;
759+ assert . deepEqual ( actual , expected ) ;
760+ } ) ;
761+
762+ it ( 'shouldnt break with cyclic references' , function ( ) {
763+ var input = {
764+ foo : { } ,
765+ baz : 42
766+ } ;
767+ input . foo . bar = input . foo ;
768+
769+ var actual = sanitize ( input , [ 'baz' ] ) ;
770+ var expected = { foo : { bar : '[Circular ~.foo]' } , baz : sanitizeMask } ;
771+ assert . deepEqual ( actual , expected ) ;
772+ } ) ;
773+
774+ it ( 'should work with keys as RegExps' , function ( ) {
775+ var actual = sanitize (
776+ {
777+ foo : {
778+ bar : 42 ,
779+ baz : 1337 ,
780+ qux : 'rick' ,
781+ forgotFifthWord : 'whoops' ,
782+ thisShouldMatch123_32 : 'hello' ,
783+ butThisNot123_42_X : 'morty'
784+ }
785+ } ,
786+ [ / ^ b a / i, 'forgotFifthWord' , / \d { 3 } _ \d { 2 } $ / i]
787+ ) ;
788+ var expected = {
789+ foo : {
790+ bar : sanitizeMask ,
791+ baz : sanitizeMask ,
792+ qux : 'rick' ,
793+ forgotFifthWord : sanitizeMask ,
794+ thisShouldMatch123_32 : sanitizeMask ,
795+ butThisNot123_42_X : 'morty'
796+ }
797+ } ;
798+ assert . deepEqual ( actual , expected ) ;
799+ } ) ;
800+ } ) ;
699801} ) ;
0 commit comments