|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var hessian = require('../'); |
| 5 | +var supportES6Map = require('../lib/utils').supportES6Map; |
| 6 | + |
| 7 | +describe('utils.test.js', function () { |
| 8 | + describe('v1.0', function () { |
| 9 | + it('should decode with custom handler', function () { |
| 10 | + hessian.registerDecodeHandler('java.math.BigDecimal', function (result) { |
| 11 | + return { |
| 12 | + $class: result.$class, |
| 13 | + $: result.$.value, |
| 14 | + }; |
| 15 | + }); |
| 16 | + var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } }; |
| 17 | + var buf = hessian.encode(o, '1.0'); |
| 18 | + var output = hessian.decode(buf, '1.0'); |
| 19 | + assert(output === '100.06'); |
| 20 | + hessian.deregisterDecodeHandler('java.math.BigDecimal'); |
| 21 | + output = hessian.decode(buf, '1.0'); |
| 22 | + assert.deepEqual(output, { value: '100.06' }); |
| 23 | + }); |
| 24 | + |
| 25 | + if (!supportES6Map) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + it('should decode map with custom handler', function () { |
| 30 | + hessian.registerDecodeHandler('java.util.HashMap', function (result) { |
| 31 | + return { |
| 32 | + $class: result.$class, |
| 33 | + $: result.$.$map, |
| 34 | + }; |
| 35 | + }); |
| 36 | + var map = new Map(); |
| 37 | + map.set(1, 'fee'); |
| 38 | + map.set(2, 'fie'); |
| 39 | + map.set(3, 'foe'); |
| 40 | + var buf = hessian.encode({ |
| 41 | + $class: 'java.util.HashMap', |
| 42 | + $: map |
| 43 | + }, '1.0'); |
| 44 | + var output = hessian.decode(buf, '1.0'); |
| 45 | + assert(output instanceof Map); |
| 46 | + assert(output.get(1) === 'fee'); |
| 47 | + assert(output.get(2) === 'fie'); |
| 48 | + assert(output.get(3) === 'foe'); |
| 49 | + hessian.deregisterDecodeHandler('java.util.HashMap'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('v2.0', function () { |
| 54 | + it('should decode with custom handler', function () { |
| 55 | + hessian.registerDecodeHandler('java.math.BigDecimal', function (result) { |
| 56 | + return { |
| 57 | + $class: result.$class, |
| 58 | + $: result.$.value, |
| 59 | + }; |
| 60 | + }); |
| 61 | + var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } }; |
| 62 | + var buf = hessian.encode(o, '2.0'); |
| 63 | + var output = hessian.decode(buf, '2.0'); |
| 64 | + assert(output === '100.06'); |
| 65 | + hessian.deregisterDecodeHandler('java.math.BigDecimal'); |
| 66 | + output = hessian.decode(buf, '2.0'); |
| 67 | + assert.deepEqual(output, { value: '100.06' }); |
| 68 | + }); |
| 69 | + |
| 70 | + if (!supportES6Map) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + it('should decode map with custom handler', function () { |
| 75 | + hessian.registerDecodeHandler('java.util.HashMap', function (result) { |
| 76 | + return { |
| 77 | + $class: result.$class, |
| 78 | + $: result.$.$map, |
| 79 | + }; |
| 80 | + }); |
| 81 | + var map = new Map(); |
| 82 | + map.set(1, 'fee'); |
| 83 | + map.set(2, 'fie'); |
| 84 | + map.set(3, 'foe'); |
| 85 | + var buf = hessian.encode({ |
| 86 | + $class: 'java.util.HashMap', |
| 87 | + $: map |
| 88 | + }, '2.0'); |
| 89 | + var output = hessian.decode(buf, '2.0'); |
| 90 | + assert(output instanceof Map); |
| 91 | + assert(output.get(1) === 'fee'); |
| 92 | + assert(output.get(2) === 'fie'); |
| 93 | + assert(output.get(3) === 'foe'); |
| 94 | + hessian.deregisterDecodeHandler('java.util.HashMap'); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments