|
| 1 | +/* global describe, it, beforeEach */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var serialize = require('../../'), |
| 5 | + expect = require('chai').expect; |
| 6 | + |
| 7 | +describe('serialize( obj )', function () { |
| 8 | + it('should be a function', function () { |
| 9 | + expect(serialize).to.be.a('function'); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('undefined', function () { |
| 13 | + it('should serialize `undefined` to a string', function () { |
| 14 | + expect(serialize()).to.be.a('string').equal('undefined'); |
| 15 | + expect(serialize(undefined)).to.be.a('string').equal('undefined'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should deserialize "undefined" to `undefined`', function () { |
| 19 | + expect(eval(serialize())).to.equal(undefined); |
| 20 | + expect(eval(serialize(undefined))).to.equal(undefined); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('null', function () { |
| 25 | + it('should serialize `null` to a string', function () { |
| 26 | + expect(serialize(null)).to.be.a('string').equal('null'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should deserialize "null" to `null`', function () { |
| 30 | + expect(eval(serialize(null))).to.equal(null); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('JSON', function () { |
| 35 | + var data; |
| 36 | + |
| 37 | + beforeEach(function () { |
| 38 | + data = { |
| 39 | + str : 'string', |
| 40 | + num : 0, |
| 41 | + obj : {foo: 'foo'}, |
| 42 | + arr : [1, 2, 3], |
| 43 | + bool: true, |
| 44 | + nil : null |
| 45 | + }; |
| 46 | + }); |
| 47 | + |
| 48 | + it('should serialize JSON to a JSON string', function () { |
| 49 | + expect(serialize(data)).to.equal(JSON.stringify(data)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should deserialize a JSON string to a JSON object', function () { |
| 53 | + expect(JSON.parse(serialize(data))).to.deep.equal(data); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should serialize weird whitespace characters correctly', function () { |
| 57 | + var ws = String.fromCharCode(8232); |
| 58 | + expect(eval(serialize(ws))).to.equal(ws); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('functions', function () { |
| 63 | + it('should serialize annonymous functions', function () { |
| 64 | + var fn = function () {}; |
| 65 | + expect(serialize(fn)).to.be.a('string').equal('function () {}'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should deserialize annonymous functions', function () { |
| 69 | + var fn; eval('fn = ' + serialize(function () {})); |
| 70 | + expect(fn).to.be.a('function'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should serialize named functions', function () { |
| 74 | + function fn() {} |
| 75 | + expect(serialize(fn)).to.be.a('string').equal('function fn() {}'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should deserialize named functions', function () { |
| 79 | + var fn; eval('fn = ' + serialize(function fn() {})); |
| 80 | + expect(fn).to.be.a('function'); |
| 81 | + expect(fn.name).to.equal('fn'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should serialize functions with arguments', function () { |
| 85 | + function fn(arg1, arg2) {} |
| 86 | + expect(serialize(fn)).to.equal('function fn(arg1, arg2) {}'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should deserialize functions with arguments', function () { |
| 90 | + var fn; eval('fn = ' + serialize(function (arg1, arg2) {})); |
| 91 | + expect(fn).to.be.a('function'); |
| 92 | + expect(fn.length).to.equal(2); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should serialize functions with bodies', function () { |
| 96 | + function fn() { return true; } |
| 97 | + expect(serialize(fn)).to.equal('function fn() { return true; }'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should deserialize functions with bodies', function () { |
| 101 | + var fn; eval('fn = ' + serialize(function () { return true; })); |
| 102 | + expect(fn).to.be.a('function'); |
| 103 | + expect(fn()).to.equal(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw a TypeError when serializing native built-ins', function () { |
| 107 | + var err; |
| 108 | + expect(Number.toString()).to.equal('function Number() { [native code] }'); |
| 109 | + try { serialize(Number); } catch (e) { err = e; } |
| 110 | + expect(err).to.be.an.instanceOf(TypeError); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('regexps', function () { |
| 115 | + it('should serialize constructed regexps', function () { |
| 116 | + var re = new RegExp('asdf'); |
| 117 | + expect(serialize(re)).to.be.a('string').equal('/asdf/'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should deserialize constructed regexps', function () { |
| 121 | + var re = eval(serialize(new RegExp('asdf'))); |
| 122 | + expect(re).to.be.a('RegExp'); |
| 123 | + expect(re.source).to.equal('asdf'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should serialize literal regexps', function () { |
| 127 | + var re = /asdf/; |
| 128 | + expect(serialize(re)).to.be.a('string').equal('/asdf/'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should deserialize literal regexps', function () { |
| 132 | + var re = eval(serialize(/asdf/)); |
| 133 | + expect(re).to.be.a('RegExp'); |
| 134 | + expect(re.source).to.equal('asdf'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should serialize regexps with flags', function () { |
| 138 | + var re = /^asdf$/gi; |
| 139 | + expect(serialize(re)).to.equal('/^asdf$/gi'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should deserialize regexps with flags', function () { |
| 143 | + var re = eval(serialize(/^asdf$/gi)); |
| 144 | + expect(re).to.be.a('RegExp'); |
| 145 | + expect(re.global).to.equal(true); |
| 146 | + expect(re.ignoreCase).to.equal(true); |
| 147 | + expect(re.multiline).to.equal(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should serialize regexps with escaped chars', function () { |
| 151 | + expect(serialize(/\..*/)).to.equal('/\\..*/'); |
| 152 | + expect(serialize(new RegExp('\\..*'))).to.equal('/\\..*/'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should deserialize regexps with escaped chars', function () { |
| 156 | +
97AE
var re = eval(serialize(/\..*/)); |
| 157 | + expect(re).to.be.a('RegExp'); |
| 158 | + expect(re.source).to.equal('\\..*'); |
| 159 | + re = eval(serialize(new RegExp('\\..*'))); |
| 160 | + expect(re).to.be.a('RegExp'); |
| 161 | + expect(re.source).to.equal('\\..*'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('XSS', function () { |
| 166 | + it('should encode unsafe HTML chars to Unicode', function () { |
| 167 | + expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"'); |
| 168 | + expect(JSON.parse(serialize('</script>'))).to.equal('</script>'); |
| 169 | + expect(eval(serialize('</script>'))).to.equal('</script>'); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments