From e7a0fb250f6cc69c93daffe0d043d073fd488c03 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 22 Dec 2010 13:19:26 -0800 Subject: fromJson delegation to native JSON parser if available - native parser delegation - $xhr change to use native parser --- test/JsonSpec.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/JsonSpec.js') diff --git a/test/JsonSpec.js b/test/JsonSpec.js index ba3366e5..6d8a40e4 100644 --- a/test/JsonSpec.js +++ b/test/JsonSpec.js @@ -116,6 +116,42 @@ describe('json', function(){ expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10}); }); + + //run these tests only in browsers that have native JSON parser + if (JSON && JSON.parse) { + + describe('native parser', function() { + + var nativeParser = JSON.parse; + + afterEach(function() { + JSON.parse = nativeParser; + }); + + + it('should delegate to native parser if available and boolean flag is passed', function() { + var spy = this.spyOn(JSON, 'parse').andCallThrough(); + + expect(fromJson('{}')).toEqual({}); + expect(spy).wasNotCalled(); + + expect(fromJson('{}', true)).toEqual({}); + expect(spy).wasCalled(); + }); + + + it('should convert timestamp strings to Date objects', function() { + expect(fromJson('"2010-12-22T17:23:17.974Z"', true) instanceof Date).toBe(true); + expect(fromJson('["2010-12-22T17:23:17.974Z"]', true)[0] instanceof Date).toBe(true); + expect(fromJson('{"t":"2010-12-22T17:23:17.974Z"}', true).t instanceof Date).toBe(true); + expect(fromJson('{"t":["2010-12-22T17:23:17.974Z"]}', true).t[0] instanceof Date).toBe(true); + expect(fromJson('{"t":{"t":"2010-12-22T17:23:17.974Z"}}', true).t.t instanceof Date).toBe(true); + }); + }); + + } + + describe('security', function(){ it('should not allow naked expressions', function(){ expect(function(){fromJson('1+2');}). -- cgit v1.2.3